Environment: VS2013
#include <Winsock2.h>#include <stdio.h>#include <stdlib.h>#pragma comment (lib, "Ws2_32.lib")#define DEFAULT_PORT 8888#define BUFFER_LENGTH 1024x768voidMain () {intIport = Default_port; Wsadata Wsadata;//protocol stackSOCKET Ssocket;//socket on the server intIlen;//Client address length intIsend;//Length of data sent intIRECV;buffer length for data received// CharSendbuf[] ="hello! I am Server ";//The string to be sent by the server CharRecvbuf[buffer_length];//buffer for server-side accept data structSockaddr_in ser, CLI;//Local address and customer address printf("-----------------\ n");printf("Server waiting\n");printf("-----------------\ n");//Load protocol stack if(WSAStartup (Makeword (2,2), &wsadata)! =0) {printf("Failed to load winsock.\n");return; }//Create a server-side socket socket InterfaceSsocket = socket (af_inet, SOCK_DGRAM,0);if(Ssocket = = Invalid_socket) {printf("socket (failed:%d\n)", WSAGetLastError);return; }//Initialize the server-side address and port numberser.sin_family = af_inet; Ser.sin_port = htons (8888);//server-side monitoring port numberSER.SIN_ADDR.S_ADDR = htonl (Inaddr_any);//port address on the server //Bind the server-side socket and server's end IP address and port number, if(Bind (Ssocket, (LPSOCKADDR) &ser,sizeof(Ser)) = = Socket_error) {printf("bind () failed:%d\n", WSAGetLastError ());return; } Ilen =sizeof(CLI);memset(Recvbuf,0,sizeof(RECVBUF));//Initialize the receive buffer contents to 0 while(1) {//Receive data from the server-side socket interface and save the client's address to the CLIIRECV = Recvfrom (Ssocket, Recvbuf, Buffer_length,0, (sockaddr*) &cli,&ilen);if(Irecv = = socket_error) {printf("Recvfrom () faild;%d\n", WSAGetLastError ()); Break; }Else if(Irecv = =0) { Break; }Else{printf("Recvfrom ():%s\n", RECVBUF);printf("accepted client ip[%s], port[%d]\n", Inet_ntoa (CLI.SIN_ADDR), Ntohs (Cli.sin_port)); }//Send data to clientIsend = SendTo (Ssocket, SendBuf,sizeof(SENDBUF),0, (sockaddr*) &CLI,sizeof(CLI));if(Isend = = socket_error) {printf("sendto () faild:%d\n", WSAGetLastError ());printf("----------\ n"); Break; }Else if(Isend = =0) Break;Else{printf("sendto () succeeded\n");printf("-=---------\ n"); }} closesocket (Ssocket);//Closed Socket interfaceWSACleanup ();//Empty protocol stack}
Client.c
#include <Winsock2.h>#include <stdio.h>#pragma comment (lib, "Ws2_32.lib")voidMain () {wsadata wsadata;//protocol stackSOCKET sclient;//Client Socket Interface structSockaddr_in ser;//Client address intIport =8888;intIlen;//Client address length intIsend;intIRECV;Charsendbuf[1024x768];//Send data buffer Charrecvbuf[1024x768];//Receive data buffers memset(Recvbuf,0,sizeof(RECVBUF));//Initialize the receive data buffer to 0 //Load protocol stack if(WSAStartup (Makeword (2,2), &wsadata)! =0) {printf("Failed to load winsock\n");return; }//Initialize client address conditionser.sin_family = af_inet; Ser.sin_port = htons (8888); SER.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");//Create client Socket interfaceSclient = socket (af_inet, SOCK_DGRAM,0);if(sclient = = Invalid_socket) {printf("socket () faild%d\n", WSAGetLastError ());return; } Ilen =sizeof(Ser); while(1) {printf_s ("Please enter a message to send:"); gets_s (SendBuf,1024x768);//Start sending dataIsend = SendTo (sclient, SendBuf,sizeof(SENDBUF),0, (structsockaddr*) &ser, Ilen);if(Isend = = socket_error) {printf("SendTo () faild%d", WSAGetLastError ());return; }Else if(Isend =0) {return; }Else{printf("sendto () succeeded\n"); }//Receive DataIRECV = Recvfrom (sclient, Recvbuf,sizeof(RECVBUF),0, (structsockaddr*) &ser, &ilen);if(Irecv = = socket_error) {printf("Recvfrom () faild%d\n", WSAGetLastError ());return; }Else if(Irecv = =0)return;Else{printf("Recvfrom ()%s", RECVBUF);printf("-----------------\ n"); }} closesocket (Sclient);//Closed Socket interfaceWSACleanup ();//Empty protocol stack}
"Tcp-ip Learning Summary" Basic UDP implementation of simple communication function applet