Advertisement
Guest User

UDP Trial

a guest
Jun 5th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <ogcsys.h>
  6. #include <gccore.h>
  7. #include <network.h>
  8. #include <debug.h>
  9. #include <errno.h>
  10. #include <wiiuse/wpad.h>
  11.  
  12. static void *xfb = NULL;
  13. static GXRModeObj *rmode = NULL;
  14.  
  15. void *initialise();
  16. void *httpd (void *arg);
  17.  
  18. static  lwp_t httd_handle = (lwp_t)NULL;
  19.  
  20. //---------------------------------------------------------------------------------
  21. int main(int argc, char **argv) {
  22. //---------------------------------------------------------------------------------
  23.     s32 ret;
  24.  
  25.     char localip[16] = {0};
  26.     char gateway[16] = {0};
  27.     char netmask[16] = {0};
  28.    
  29.     xfb = initialise();
  30.  
  31.     printf ("\nlibogc network demo\n");
  32.     printf("Configuring network ...\n");
  33.  
  34.     // Configure the network interface
  35.     ret = if_config ( localip, netmask, gateway, TRUE);
  36.     if (ret>=0) {
  37.         printf ("network configured, ip: %s, gw: %s, mask %s\n", localip, gateway, netmask);
  38.  
  39.         LWP_CreateThread(   &httd_handle,   /* thread handle */
  40.                             httpd,          /* code */
  41.                             localip,        /* arg pointer for thread */
  42.                             NULL,           /* stack base */
  43.                             16*1024,        /* stack size */
  44.                             50              /* thread priority */ );
  45.     } else {
  46.         printf ("network configuration failed!\n");
  47.     }
  48.  
  49.     while(1) {
  50.  
  51.         VIDEO_WaitVSync();
  52.         WPAD_ScanPads();
  53.  
  54.         int buttonsDown = WPAD_ButtonsDown(0);
  55.  
  56.         if (buttonsDown & WPAD_BUTTON_HOME) {
  57.             exit(0);
  58.         }
  59.     }
  60.  
  61.     return 0;
  62. }
  63.  
  64. #define SRV_IP "192.168.1.130"
  65. #define BUFLEN 512
  66. #define NPACK 10
  67. #define PORT 9930
  68.  
  69. //---------------------------------------------------------------------------------
  70. void *httpd (void *arg) {
  71. //---------------------------------------------------------------------------------
  72.  
  73.     s32 sock;
  74.     struct sockaddr_in si_other;
  75.     int  i, slen=sizeof(si_other);
  76.     char buf[BUFLEN];
  77.  
  78.     sock = net_socket (AF_INET, SOCK_DGRAM, IPPROTO_IP);
  79.  
  80.     if (sock == INVALID_SOCKET) {
  81.             printf ("Cannot create a socket!\n");
  82.         } else {
  83.         memset((char *) &si_other, 0, sizeof(si_other));
  84.         si_other.sin_family = AF_INET;
  85.         si_other.sin_port = htons(PORT);
  86.         si_other.sin_len = 8;
  87.         if ((si_other.sin_addr.s_addr = inet_addr(SRV_IP)) == 0) {
  88.             fprintf(stderr, "inet_aton() failed\n");
  89.         }else{
  90.             for (i=0; i<NPACK; i++) {
  91.                 printf("Sending packet %d\n", i);
  92.                 sprintf(buf, "This is packet %d\n", i);
  93.                 int n;
  94.                 if((n = net_sendto(sock, buf, BUFLEN, 0, &si_other, 8)) < 0)
  95.                     printf("Failed. %d.",n);
  96.             }
  97.             net_close (sock);
  98.         }
  99.     }
  100.     return NULL;
  101. }
  102.  
  103. //---------------------------------------------------------------------------------
  104. void *initialise() {
  105. //---------------------------------------------------------------------------------
  106.  
  107.     void *framebuffer;
  108.  
  109.     VIDEO_Init();
  110.     WPAD_Init();
  111.    
  112.     rmode = VIDEO_GetPreferredMode(NULL);
  113.     framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  114.     console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  115.    
  116.     VIDEO_Configure(rmode);
  117.     VIDEO_SetNextFramebuffer(framebuffer);
  118.     VIDEO_SetBlack(FALSE);
  119.     VIDEO_Flush();
  120.     VIDEO_WaitVSync();
  121.     if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  122.  
  123.     return framebuffer;
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement