服务端
- socket() 创建套接字
- bind() 将套接字绑定在本机地址和端口
- listen() 监听,并等待客户端接入
- accept() 接受客户端请求
- recv()/send() 通信
- close() 关闭套接字
客户端
- socket() 创建套接字
- connect() 向服务器端发送连接请求
- recv()/send() 通信
- close() 关闭套接字
服务端:
#include<iostream>
using namespace std;
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include<windows.h>
#include<string>
int main(){
//1.确定协议版本
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (HIBYTE(wsaData.wVersion) != 2 || LOBYTE(wsaData.wVersion) != 2){
cout << "确定版本协议失败: " << GetLastError() << endl;
return -1;
}
cout << "确定版本协议成功!" << endl;
//2.创建socket
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == SOCKET_ERROR){
cout << "创建socket失败: " << GetLastError() << endl;
return -1;
}
cout << "创建socket成功!" << endl;
//3.设置服务器地址簇
SOCKADDR_IN addr = { 0 };
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = inet_addr("10.181.73.111");
addr.sin_port = htons(9527);
//4.绑定
int r = bind(serverSocket, (sockaddr*)&addr, sizeof(addr));
if (r == -1){
cout << "绑定失败: " << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "绑定成功!" << endl;
//5.监听
r = listen(serverSocket, 10);
if (r == -1){
cout << "监听失败: " << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "监听成功!" << endl;
//6. 接受连接
SOCKADDR_IN clientAddr = { 0 };
int len = sizeof(clientAddr);
SOCKET clientSocket = accept(serverSocket, (sockaddr*)&clientAddr, &len);
if (clientSocket == SOCKET_ERROR){
cout << "客户端连接失败:" << GetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
cout << "客户端连接成功" << endl;
// 7. 通信
char buff[1024];
while (1){
r = recv(clientSocket, buff, 1023, NULL);
if (r > 0){
buff[r] = 0;
cout << "来自客户端的消息:" << buff << endl;
}
}
}
客户端:
#include<iostream>
using namespace std;
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include<windows.h>
#include<string>
int main(){
//1.确定协议版本
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (HIBYTE(wsaData.wVersion) != 2 || LOBYTE(wsaData.wVersion) != 2){
cout << "确定版本协议失败: " << GetLastError() << endl;
return -1;
}
cout << "确定版本协议成功!" << endl;
//2.创建socket
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == SOCKET_ERROR){
cout << "创建socket失败: " << GetLastError() << endl;
return -1;
}
cout << "创建socket成功!" << endl;
//3.设置服务器地址簇
SOCKADDR_IN addr = { 0 };
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = inet_addr("10.181.73.111");
addr.sin_port = htons(9527);
//4. 连接
int r = connect(clientSocket, (sockaddr*)&addr, sizeof(addr));
if (r == -1){
cout << "服务器连接失败" << GetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
cout << "服务器连接成功!" << endl;
// 5. 通信
char buff[1024];
while (1){
cout << "输入你发送的消息:";
cin >> buff;
send(clientSocket, buff, strlen(buff), NULL);
}
}
int sendToServer(char *m_pDataBuffer,int sendDataLen)
{
/* 创建一个 socket 用于 tcp 通信 */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return ERROR;
}
/* 连接服务器 */
if (connect(sockfd, (struct sockaddr *) &ServerAddr, sizeof(ServerAddr)) != 0)
{
close(sockfd);
return ERROR;
}
int retLen = writen(sockfd, m_pDataBuffer, sendDataLen);
if(retLen != sendDataLen)
{
SAFE_CLOSE(sockfd);
return ERROR;
}
fd_set readset;
struct timeval tv_read;
int ret = 0;
while(1)
{
FD_ZERO(&readset);
FD_SET(sockfd, &readset);
tv_read.tv_sec = 1;
tv_read.tv_usec = 0;
ret = select(sockfd+1, &readset, NULL, NULL, &tv_read);
if(ret < 0)
{
backret = ERROR;
goto outExit;
}
else if (ret == 0)
{
backret = ERROR;
goto outExit;
}
else
{
usleep(200*1000);
memset(recvBuf, 0, 1024);
len = HPR_Recv(sockfd, recvBuf, 1024);
if (len == 0)
{
backret = ERROR;
goto outExit;
}
else if (len < 0)
{
backret = ERROR;
goto outExit;
}
}
}
outExit:
close(sockfd);
return backret;
}