












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); WSACleanup( ); exit(1); }
their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr.s_addr = inet_addr(argv[1]); memset(&(their_addr.sin_zero), 0, 8); // zero the rest of the struct
if ( (numbytes=sendto(sockfd, "Hello, world!\n", 14, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr) ) ) == -1) { perror("send"); errorVal=WSAGetLastError(); }
if ( (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); errorVal=WSAGetLastError(); }
my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(MYPORT); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), 0, 8); // zero the rest of the struct
if ( bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr) ) == -1) { perror("bind"); WSACleanup( ); exit(1); }
if ( (numBytes=sendto(sockfd, buf, numBytes, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr) ) ) == -1) { perror("send"); errorVal=WSAGetLastError(); } fprintf(stderr, "%d bytes sent to client\n",numBytes);
closesocket(new_fd); WSACleanup( ); return 0; } //main ends here