#include #include #include #include #include #define SERVER_PORT 1966 #define MAX_PENDING 5 #define MAX_LINE 256 int main() { struct sockaddr_in sin; char buf[MAX_LINE]; int len; int s, new_s; /*build address data structure of INADDR(all interfaces) paired with SERVER PORT */ bzero( (char*)&sin, sizeof(sin) ); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons( SERVER_PORT ); /*setup passive open */ if( (s = socket(PF_INET, SOCK_STREAM, 0) ) < 0) { perror( "simplex-talk: socket" ); exit(1); } /*binds the address structure(actually the local port) behind the process*/ if ((bind( s, (struct sockaddr *)&sin, sizeof(sin) )) < 0 ) { perror( "simplex-talk; bind" ); exit(1); } /*specifies a queue(of maximum size MAX_PENDING) for incoming connections on socket s*/ listen( s, MAX_PENDING ); fputs("I am listening:\n", stdout); /*waits for and accepts connection, then receive and print text */ while(1) { if( (new_s = accept( s, (struct sockaddr *)&sin, &len )) < 0 ) { perror( "simplex-talk: accept" ); exit(1); } while( len = recv( new_s, buf, sizeof(buf), 0 ) ) { fputs(buf, stdout); } close( new_s ); } }//end main