/* UDP ClientSide Console App */ /* Bill Tompkins 4/2012 */ /* Use code at your own risk */ #include "stdafx.h" #include #include #pragma comment(lib,"ws2_32.lib") //Windows Socket Library #define SERVER "127.0.0.1" //ip address of udp server #define BUFLEN 512 //Max length of buffer #define PORT 8887 //The port on which to listen for incoming data int main(void) { struct sockaddr_in si_other; int s, slen=sizeof(si_other); char buf[BUFLEN]; char message[BUFLEN]; WSADATA wsa; //Initialize winsock printf("\nUDP Text Demo (ClientSide)"); printf("\nInitializing Winsock..."); if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) { printf("Failed. Error Code: %d",WSAGetLastError()); exit(EXIT_FAILURE); } printf("Initialized.\n"); //create socket if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) { printf("Socket Routine Failed. Error Code: %d" , WSAGetLastError()); exit(EXIT_FAILURE); } //setup address structure memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); si_other.sin_addr.S_un.S_addr = inet_addr(SERVER); //start talking while(1) { printf("\nEnter Data: "); gets_s(message); //send the message if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR) { printf("Sendto Routine Failed. Error Code: %d" , WSAGetLastError()); exit(EXIT_FAILURE); } //receive a reply and print it //clear the buffer by filling null, it might have previously received data memset(buf,'\0', BUFLEN); //try to receive some data, this is a blocking call if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR) { printf("Recvfrom Routine Failed. Error Code: %d" , WSAGetLastError()); exit(EXIT_FAILURE); } puts(buf); } closesocket(s); WSACleanup(); return 0; }