












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This lecture was delivered by Dr. Mstan Veer at Gautam Buddha University for Network Programming course. It includes: Network, Programming, Primer, Client, Server, Application, Wait, COnnect, Print, Strings
Typology: Slides
1 / 20
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