以下代码是服务器端与客户端简单进行TCP socket通信过程。
以下代码是服务器端与客户端简单进行UDP socket通信过程。
1. 在服务器端的函数调用时,如果需要客户端的地址长度,则都是通过取地址的形式来使用的。
2. 非阻塞socket编程,需要在socket创建之后,调用
将socket设置为非阻塞模式。
3.
Server.cpp
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define BUFSIZE 64
#define PORT 9999
int main()
{
WSADATA wsadata;
SOCKET sServer;
SOCKET sClient;
int retVal;
char buf[BUFSIZE];
if(WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
{
cout << "WSAStartup失败!" << endl;
return -1;
}
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* ******************** 1 **************** */
if (INVALID_SOCKET == sServer)
{
cout << "socket() 错误!" << endl;
WSACleanup();
return -1;
}
SOCKADDR_IN addrServ;
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(PORT);
addrServ.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
retVal = bind(sServer, (const struct sockaddr*)&addrServ, sizeof(addrServ)); /* ******************** 2 **************** */
if (SOCKET_ERROR == retVal)
{
cout << "bind 错误!" << endl;
closesocket(sServer);
WSACleanup();
return -1;
}
retVal = listen(sServer,10); /* ******************** 3 **************** */
if (SOCKET_ERROR == retVal)
{
cout << "listen 错误!" << endl;
closesocket(sServer);
WSACleanup();
return -1;
}
cout << "TCP服务器开始监听" << PORT << "端口" << endl;
SOCKADDR_IN addrClient;
int addrClientLen = sizeof(addrClient);
sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientLen); /* ******************** 4 **************** */
if (INVALID_SOCKET == sClient)
{
cout << "accept() 错误!" << endl;
WSACleanup();
return -1;
}
while (true)
{
ZeroMemory(buf, BUFSIZE);
retVal = recv(sClient, buf, BUFSIZE, 0); /* ******************** 5 **************** */
if (SOCKET_ERROR == retVal)
{
cout << "recv()错误!" << endl;
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;
}
SYSTEMTIME time;
GetLocalTime(&time);
char datetime[30];
sprintf(datetime,"%4d-%2d-%2d %2d:%2d:%2d",time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
cout << datetime << "从" << inet_ntoa(addrClient.sin_addr) << ":" << addrClient.sin_port << "接受了如下内容: " << buf << endl;
if (strcmp(buf, "quit") == 0)
{
retVal = send(sClient, "quit", strlen("quit"), 0);
break;
}
else
{
retVal = send(sClient, buf, strlen(buf), 0); /* ******************** 6 **************** */
if (SOCKET_ERROR == retVal)
{
cout << "send错误!" << endl;
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;
}
}
}
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return 0;
}
Client.cpp
#include <Winsock.h>
#include <string>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define BUFSIZE 64
#define PORT 9999
int main()
{
WSAData wsaData;
SOCKET sHost;
sockaddr_in addrServ;
char buf[BUFSIZE];
int retVal;
if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
cout << "WSAStartup失败!" << endl;
return -1;
}
sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sHost)
{
cout << "socket() 错误!" << endl;
WSACleanup();
return -1;
}
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(PORT);
addrServ.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
retVal = connect(sHost, (LPSOCKADDR)&addrServ, sizeof(addrServ));
if (SOCKET_ERROR == retVal)
{
cout << "connect 错误!" << endl;
closesocket(sHost);
WSACleanup();
return -1;
}
while (true)
{
cout << "输入要发给服务器的内容" << endl;
// string msg;
// getline(cin, msg);
char msg[BUFSIZE];
cin.getline(msg, BUFSIZE);
ZeroMemory(buf, BUFSIZE);
strcpy(buf, msg);
retVal = send(sHost, buf, strlen(buf), 0);
if (SOCKET_ERROR == retVal)
{
cout << "发送失败" << endl;
closesocket(sHost);
WSACleanup();
return -1;
}
retVal = recv(sHost, buf, sizeof(buf)+1, 0);
cout << "从服务器端接收:" << buf << endl;
if (strcmp(buf, "quit") == 0)
{
cout << "quit" << endl;
break;
}
}
closesocket(sHost);
WSACleanup();
return 0;
}
以下代码是服务器端与客户端简单进行UDP socket通信过程。
Server.cpp
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define BUFSIZE 1024
#define PORT 9999
int main()
{
WSADATA wsadata;
SOCKET sServer;
sockaddr_in addrClient, addrServer;
int addrClientLen = sizeof(addrClient);
char buf[BUFSIZE];
if(WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
{
cout << "WSAStartup失败!" << endl;
return -1;
}
sServer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(PORT);
addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
bind(sServer, (SOCKADDR*) &addrServer, sizeof(addrServer));
cout << "从客户端接收:" << endl;
recvfrom(sServer, buf, BUFSIZE, 0, (SOCKADDR *)&addrClient, &addrClientLen);
sendto(sServer, buf, BUFSIZE, 0, (SOCKADDR *)&addrClient, sizeof(addrClient));
cout << "完成接收:" << buf << endl;
closesocket(sServer);
WSACleanup();
return 0;
}
Client.cpp
#include <winsock2.h>
#include <iostream>
#include <string>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define BUFSIZE 1024
#define PORT 9999
int main()
{
WSADATA wsadata;
SOCKET sClient;
sockaddr_in addrServer;
int addrServerLen = sizeof(addrServer);
char buf[BUFSIZE];
if(WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
{
cout << "WSAStartup失败!" << endl;
return -1;
}
sClient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(PORT);
addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
cout << "请输入要发送给服务器的内容:" << endl;
cin.getline(buf, BUFSIZE);
sendto(sClient, buf, BUFSIZE, 0, (SOCKADDR *)&addrServer, addrServerLen);
memset(buf, 0, BUFSIZE);
recvfrom(sClient, buf, BUFSIZE, 0, (SOCKADDR *)&addrServer, &addrServerLen);
cout << buf << endl;
closesocket(sClient);
WSACleanup();
return 0;
}
1. 在服务器端的函数调用时,如果需要客户端的地址长度,则都是通过取地址的形式来使用的。
2. 非阻塞socket编程,需要在socket创建之后,调用
int ioctlsocket(
SOCKET s,
long cmd,//FIONBIO
u_long *argp
);
将socket设置为非阻塞模式。
3.