在上一篇随笔中,对Winsock中基于tcp面向连接的Server和Client通信进行了说明,但是,Winsock中,Server和Client间还可以通过无连接通信,也就是采用UDP协议。。
因此,这一篇随笔也简单的列举基于UDP的Server和Client的实现。。
和基于TCP的实现相比,其主要的不同点包括:
①接收端(简单地说就是服务器)/发送端(简单地说就是客户端)在创建Socket时候,参数要选择SOCK_DGRAM, IPPROTO_UDP;
②接收端不需要调用listen和accept函数,而是通过recvfrom/WSArecvfrom函数接收数据报;
③发送端通过sendto/WSAsendto接收数据报;
接收端和发送端的代码如下:
#include"winsock2.h"
#include<iostream>
using namespace std;
//This line is very important #pragma comment(lib,"ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET ReceivingSocket;
SOCKADDR_IN ReceiverAddr;
int Port = ;
char ReceiveBuf[];
int BufLength = ;
SOCKADDR_IN SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
int Ret; if ((Ret = WSAStartup(MAKEWORD(,), &wsaData)) != )
{
cout<<"WSAStartup failed with error "<<Ret<<endl;
//here no WSACleanup,because we do not create anything;
return -;
} // Create a new socket to listening for client connections.
//Note the difference with TCP
ReceivingSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if ( INVALID_SOCKET == ReceivingSocket)
{
cout<<"Socket failed with error "<<WSAGetLastError()<<endl;
WSACleanup();
return -;
} ReceiverAddr.sin_family = AF_INET;
ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
ReceiverAddr.sin_port = htons(Port); //to bind
if (bind(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR)
{
cout<<"Binding failed with error "<<WSAGetLastError()<<endl;
closesocket(ReceivingSocket);
WSACleanup();
return -;
} // Listen for client connections. We used a backlog of 5 which is
// normal for many applications. cout<<"** We are ready to receive 1 datagram from any interface on port "<<Port<<"**"<<endl; //accep a connection when one arrives cout<<"** We are waiting for data...**\n";
//SenderAddrSize = sizeof(SenderAddr);
Ret = recvfrom(ReceivingSocket,ReceiveBuf,BufLength,,(SOCKADDR *)&SenderAddr,&SenderAddrSize);
if (SOCKET_ERROR == Ret)
{
cout<<"Recvfrom failed with error "<<WSAGetLastError()<<endl;
closesocket(ReceivingSocket);
WSACleanup();
return -;
}
cout<<"**We have successfully recieve "<<Ret<<" Byte(s) data!**\n"; cout<<"**We are going to close the client connection...**\n"; closesocket(ReceivingSocket);
WSACleanup(); return ;
}
Receiver Code
#include"winsock2.h"
#include<iostream>
#include <time.h>
using namespace std;
//This line is very important #pragma comment(lib,"ws2_32.lib")
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET SendingSocket;
SOCKADDR_IN ReceiverAddr;
int Port = ;
int Ret; if (argc <= )
{
cout<<"USAGE: udpclient <Server IP address>.\n";
return -;
} // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(,), &wsaData)) != )
{
cout<<"WSAStartup failed with error "<<Ret<<endl;
return -;
} // Create a new socket to make a client connection. SendingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);//Note the difference with TCP
if (INVALID_SOCKET == SendingSocket)
{
cout << "socket failed with error " << WSAGetLastError()<<endl;
WSACleanup();
return -;
} ReceiverAddr.sin_family = AF_INET;
ReceiverAddr.sin_port = htons(Port);
ReceiverAddr.sin_addr.s_addr = inet_addr(argv[]); // Make a connection to the server with socket s. cout<< "We are trying to connect to " << inet_ntoa(ReceiverAddr.sin_addr)
<< ":" << htons(ReceiverAddr.sin_port) << "...\n"; cout << "We will now try to send a hello message.\n"; if ((Ret = sendto(SendingSocket, "Hello", , , (SOCKADDR *)&ReceiverAddr,sizeof(ReceiverAddr))) == SOCKET_ERROR)
{
cout << "Sendto failed with error " << WSAGetLastError()<<endl;
closesocket(SendingSocket);
WSACleanup();
return -;
} cout << "We successfully sent " << Ret << " byte(s).\n"; // When you are finished sending and receiving data on socket s,
// you should close the socket. cout << "We are closing the connection.\n"; closesocket(SendingSocket); // When your application is finished handling the connection, call
// WSACleanup. WSACleanup();
return ;
}
Sender Code