#include #include #include #include #include /* htohs */ #include /* htohs */ #include /* read , sleep */ #include #include #include /* memset */ #define PORT 8888 void *thr_main(); int value; int main(int argc, char *argv[]){ pthread_t thrID; int sock, size, padSize; char rbuf[12], *sbuf = "Hi"; struct sockaddr_in my_addr, paddr; if( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) perror("socket"); memset(&my_addr, 0, sizeof(struct sockaddr_in)); my_addr.sin_addr.s_addr = htons(INADDR_ANY); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(PORT); bind(sock, (struct sockaddr *)&my_addr, sizeof(my_addr)); pthread_create(&thrID, NULL, thr_main, NULL); padSize = sizeof(paddr); recvfrom(sock, rbuf, 11, 0, (struct sockaddr *)&paddr, &padSize); rbuf[11] = '\0'; printf("Client said: %s\n",rbuf); fflush(stdout); sendto(sock, sbuf, sizeof(sbuf), 0, (struct sockaddr *)&paddr, padSize); pthread_join(thrID, NULL); return(0); } void *thr_main(int *arg){ int sock, sadSize; struct hostent *hstr; struct sockaddr_in sad; char *buf = "How are you"; char rbuf[3]; sleep(1); sock = socket(AF_INET, SOCK_DGRAM, 0); if( (hstr = gethostbyname("ren.eecis.udel.edu")) == NULL ) perror("gethostbyname"); memcpy((char *)&(sad.sin_addr.s_addr), hstr->h_addr, hstr->h_length); sad.sin_family = AF_INET; sad.sin_port = htons(PORT); sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)&sad, sizeof(sad) ); sadSize = sizeof(sad); recvfrom(sock, rbuf, 2, 0, (struct sockaddr *)&sad, &sadSize); rbuf[2] = '\0'; printf("Server said: %s\n",rbuf); fflush(stdout); close(sock); }