#include #include #include #include #include #include #include #define MYPORT 3900 //#define MYPORT 5432 main() { int sd,ns; char buf[256]; struct sockaddr sockaddr; int fromlen; char myname[32]; struct servent *sp; struct hostent *hp; struct sockaddr_in sin; sd = socket(AF_INET,SOCK_DGRAM,0); /*proper war to access gethostbyname. */ if (gethostname(myname,sizeof(myname)-1)<0) { perror("gethosbyname problem"); exit(1); } printf("My hostname is: %s\n",myname); hp = gethostbyname(myname); bzero((char *)&sin,sizeof(sin)); bcopy(hp->h_addr,(char *)&sin.sin_addr,hp->h_length); // forgot the following two lines, no wonder it didn't work!! sin.sin_port = htons(MYPORT); sin.sin_family = hp->h_addrtype; if (bind(sd,(struct sockaddr *)&sin,sizeof(sin)) == -1) { perror("bind error"); exit(1); } for(;;) { recvfrom(sd,buf,sizeof(buf),0,&sockaddr,&fromlen); printf("Server received %s\n",buf); } }