Advertisement

Async. Sockets not working?

Started by April 14, 2002 01:22 PM
0 comments, last by SimDemon 22 years, 9 months ago
Well, I have tried plenty of Async. Socket programming. None of them work. I was wondering, could anyone post some code for an Async. Socket program? If thispost = 0 Then GoBack() Else Read() End If
I'll have a link to the TriFaze website as soon as possible. It's still currently being designed, by myself of course! =) I'll update the URL and my signature as soon as possible.Feel free to send me a message on Yahoo! or AOL IM™. =)
UNIX Asynchronous UDP, you''ll need to add the error checking at each step (I''m too lazy to figure it out here). Plus the code may be full of bugs, missing headers...


  #include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <sys/file.h>#include <arpa/inet.h>#include <fnctl.h>#include <signal.h>#define MAX_BUFFER 1024int global_socket; // Global, the socket, used in asynch handlertypedef struct sockaddr SA;void SIGIO_handler( int type ){  struct sockaddr_in address;  unsigned int addr_length;  int size;  char buffer[MAX_BUFFER];  do  {    addr_length = sizeof( address );    size = recvfrom( global_socket, buffer, MAX_BUFFER, 0 , (SA*) &address, &addr_length );    if (size < 0)    {      // Handle errors, but (obviously) just ignore EWOULDBLOCK    }    else    {      // Do something with the data.    }  } while( size >= 0 )}int setup( int port ){  struct sigaction handler;  struct sockaddr_in address;    // Setup socket  int sock = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Add error check    memset( &address, 0, sizeof( address ));  address.sin_family = AF_INET;  address.sin_addr.s_addr = htonl( INADDR_ANY );  address.sin_port = htons( port );  bind( sock, (SA*) &address, sizeof( address ) ); // Add error check  // Setup asynchronous handler  handler.sa_handler = SIGIO_handler;  sigfillset(&handler.sa_mask); // Add error check  handler.sa_flags = 0;  // register signal handler  sigaction( SIGIO, &handler, 0 ); // Add error check  // "own" the socket  fcntl( sock, F_SETOWN, getpid() ); // Add error check  // make it non-blocking and asynchronous  fcntl( sock, F_SETFL, O_NONBLOCK | FASYNC ); // Add error check  return sock;}void main(){  global_socket = setup( 4000 );    GameLoop();}  


[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement