The UDP-based Winsock programming is only missing a step compared to the TCP-based Winsock programming. For server, the process of accepting connections (accept () function calls) is missing, and for client, the process of requesting a connection (connect () function call) is missing.
In addition to the TCP difference, in UDP, the data transceiver function is: SendTo (), and the Recvfrom () function.
The function prototypes are:
int sendto (SOCKET s,const char far *buf,int len,int flags,const struct sockaddr far *to,int tolen);
int Recvfrom (SOCKET s,char far *buf,int len,int flags,struct sockaddr far *to,int far *fromlen);
The parameter flags are generally set to 0;
The parameter to is the target address structure information;
The parameter Tolen is the target address structure size;
Say less nonsense, on the code!
Code:
Server
/*udpserver.cpp*/#include <winsock2.h>#include <stdio.h>#include <windows.h>#pragma comment (lib, "Ws2_32.lib")intMain () {//Initialize socket fontWsadata data;//define struct-body variablesWORD W=makeword (2,0);//define Socket version:: WSAStartup (W,&data);//Initialize socket font Charsztext[]="welcome you \ r \ n";//Create socket handleSOCKET s; S=::socket (Af_inet,sock_dgram,0);//Create a UDP socket //Address structure settings and byte conversionSockaddr_in ADDR,ADDR2;//Create socket address structure variable intn=sizeof(ADDR2);//Address structure variable size Charbuff[ One]={0};//Receive data buffersAddr.sin_family=af_inet; Addr.sin_port=htons ( the); Addr.sin_addr. S_un. S_addr=inaddr_any;//Bound sockets:: Bind (S, (sockaddr*) &addr,sizeof(addr));printf("UDP server is started \ r \ n");//Tips while(true){//Accept data from client if(:: Recvfrom (S,buff,Ten,0, (sockaddr*) &addr2,&n)! =0){printf("%s is already connected \ r \ n", Inet_ntoa (ADDR2.SIN_ADDR));printf("%s\r\n", buff);//Send data to client:: SendTo (S,sztext,sizeof(Sztext),0, (sockaddr*) &addr2,n); Break; } }//Close socket:: Closesocket (s);//Close socket font:: WSACleanup ();if(GetChar ()) {return 0; }Else{:: Sleep ( -); }}
Client
/*udpclient.cpp*/#include <winsock2.h>#include <stdio.h>#include <windows.h>#pragma comment (lib, "Ws2_32.lib")//Connection socket font intMain () {//Initialize socket fontWsadata data;//Parameter 1WORD W=makeword (2,0);//Parameter 2, indicating that the socket version is 2.0:: WSAStartup (W,&data);Charsztext[]="Server Hello!\r\n";//Create socket handleSOCKET s; S=::socket (Af_inet,sock_dgram,0);//Create a UDP socket //Address structure settings and byte conversionSockaddr_in ADDR,ADDR2;intn=sizeof(ADDR2);Charbuff[Ten]={0}; Addr.sin_family=af_inet; Addr.sin_port=htons ( the); Addr.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");printf("UDP client has started \ r \ n");//Send a message to the server if(:: SendTo (S,sztext,sizeof(Sztext),0, (sockaddr*) &addr,n)! =0){//Receive information from the server:: Recvfrom (S,buff,Ten,0, (sockaddr*) &addr2,&n);printf("server says:%s\r\n", buff);//Close socket:: Closesocket (s);//Close socket font:: WSACleanup (); }if(GetChar ()) {return 0; }Else{:: Sleep ( -); }}
UDP-based Winsock programming (C + +)