Client.cpp
//注释的为UDP的代码,方便对比
//TCP::WSAStartup socket bind listen accept send/recv closesocket WSACleanup
#include <winsock2.h>
#include <stdio.h>
#pragma comment (lib,"ws2_32")
int main(int argc , char* grgv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
//SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKET s = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(827);
addr.sin_family = PF_INET;
bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
//listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
//SOCKET clientSock;
char buf[MAXBYTE] = {0};
int len = sizeof(SOCKADDR);
recvfrom(s,buf,MAXBYTE,0,(SOCKADDR*)&clientAddr,&len);
printf("%s \r\n",buf);
//clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
//send(clientSock,"hello client \r\n",strlen("hello client \r\n" + sizeof(char)),NULL);
sendto(s,"hello world client UDP",strlen("hello world client UDP") + sizeof(char),
0,(SOCKADDR*)&clientAddr,sizeof(SOCKADDR));
//closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}
#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib,"ws2_32")
//注释的为UDP的代码,方便对比
//流程
//TCP:WSAStarup socket connect recv/send closesocket WSACleanup
int main(int argc,char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
//SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKET s = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_family = PF_INET;
addr.sin_port = htons(827);
//connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
sendto(s,"hello world server UDP",strlen("hello world server UDP") + sizeof(char),
0,(SOCKADDR*)&addr,sizeof(SOCKADDR));
sockaddr_in clientAddrUDP;
char szBuffer[MAXBYTE] = {0};
int len = sizeof(SOCKADDR);
//recv(s,szBuffer,MAXBYTE,NULL);
recvfrom(s,szBuffer,MAXBYTE,0,(SOCKADDR*)&clientAddrUDP,&len);
printf("szBuffer = %s \r\n",szBuffer);
closesocket(s);
WSACleanup();
return 0;
}
#include <winsock2.h>
#pragma comment (lib,"ws2_32")
//注释的为UDP的代码,方便对比
//流程
//TCP:WSAStarup socket connect recv/send closesocket WSACleanup
int main(int argc,char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
//SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKET s = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_family = PF_INET;
addr.sin_port = htons(827);
//connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
sendto(s,"hello world server UDP",strlen("hello world server UDP") + sizeof(char),
0,(SOCKADDR*)&addr,sizeof(SOCKADDR));
sockaddr_in clientAddrUDP;
char szBuffer[MAXBYTE] = {0};
int len = sizeof(SOCKADDR);
//recv(s,szBuffer,MAXBYTE,NULL);
recvfrom(s,szBuffer,MAXBYTE,0,(SOCKADDR*)&clientAddrUDP,&len);
printf("szBuffer = %s \r\n",szBuffer);
closesocket(s);
WSACleanup();
return 0;
}
Server.cpp
//注释的为UDP的代码,方便对比
//TCP::WSAStartup socket bind listen accept send/recv closesocket WSACleanup
#include <winsock2.h>
#include <stdio.h>
#pragma comment (lib,"ws2_32")
int main(int argc , char* grgv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
//SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKET s = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(827);
addr.sin_family = PF_INET;
bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR));
//listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
//SOCKET clientSock;
char buf[MAXBYTE] = {0};
int len = sizeof(SOCKADDR);
recvfrom(s,buf,MAXBYTE,0,(SOCKADDR*)&clientAddr,&len);
printf("%s \r\n",buf);
//clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
//send(clientSock,"hello client \r\n",strlen("hello client \r\n" + sizeof(char)),NULL);
sendto(s,"hello world client UDP",strlen("hello world client UDP") + sizeof(char),
0,(SOCKADDR*)&clientAddr,sizeof(SOCKADDR));
//closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}