Windows客户端
#pragma comment(lib, "ws2_32.lib")
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <iostream>
using namespace std;
const char* DEFAULT_PORT = "5000";
const int BUF_SIZE = 1024;
const char* IP_ADDRESS = "127.0.0.1";
class Client
{
private:
SOCKET SockClient;
public:
int client_start();
void client_close();
void client_func();
};
int Client::client_start()
{
WSADATA WsaData;
SockClient = INVALID_SOCKET;
addrinfo* result = nullptr, hints;
int ConnectResult = 0;
if (WSAStartup(MAKEWORD(2, 2), &WsaData) != 0)
return -1;
SecureZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
ConnectResult = getaddrinfo(IP_ADDRESS, DEFAULT_PORT, &hints, &result);
if (ConnectResult != 0)
{
WSACleanup();
return -1;
}
SockClient = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (SockClient == INVALID_SOCKET)
{
WSACleanup();
return -1;
}
ConnectResult = connect(SockClient, result->ai_addr, result->ai_addrlen);
if (ConnectResult == SOCKET_ERROR)
{
WSACleanup();
return -1;
}
freeaddrinfo(result);
return 0;
}
void Client::client_close()
{
if (shutdown(SockClient, SD_SEND) == SOCKET_ERROR)
{
closesocket(SockClient);
WSACleanup();
}
}
void Client::client_func()
{
char SendBuffer[BUF_SIZE];
memset(SendBuffer, 0, BUF_SIZE);
cin >> SendBuffer;
int SendResult = send(SockClient, SendBuffer, BUF_SIZE, 0);
if (SendResult < 1)
return ;
SendResult = recv(SockClient, SendBuffer, static_cast<int>(strlen(SendBuffer)), 0);
if (SendResult < 1)
return ;
cout << SendBuffer << endl;
}
int main()
{
Client MyClient;
cout << MyClient.client_start() << endl;
MyClient.client_func();
MyClient.client_close();
return 0;
}
Windows服务器端
#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
#include <iostream>
#include <thread>
using namespace std;
const int IP_BUF_SIZE = 129;
const int SERVER_PORT = 5000;
const int BUF_SIZE = 1024;
void server_func(SOCKET SockClient);
class Server
{
public:
Server();
~Server();
void wait_for_client();
private:
WORD WinsockVer;
WSADATA WsaData;
SOCKET SockServer;
SOCKADDR_IN AddrServer;
int AddrLen;
};
int main()
{
Server MyServer;
MyServer.wait_for_client();
return 0;
}
Server::Server()
{
WinsockVer = MAKEWORD(2, 2);
AddrLen = sizeof(SOCKADDR_IN);
AddrServer.sin_family = AF_INET;
AddrServer.sin_port = ::htons(SERVER_PORT);
AddrServer.sin_addr.S_un.S_addr = ADDR_ANY;
if (::WSAStartup(WinsockVer, &WsaData) != 0)
{
cerr << "WSA启动失败,错误代码: " << ::WSAGetLastError() << endl;
system("pause");
exit(1);
}
SockServer = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SockServer == INVALID_SOCKET)
{
cerr << "创建服务器Socket失败!错误代码: " << ::WSAGetLastError() << endl;
::WSACleanup();
system("pause");
exit(1);
}
int RetVal = ::bind(SockServer, (SOCKADDR*)&AddrServer, AddrLen);
if (RetVal != 0)
{
cerr << "服务器Socket绑定失败!错误代码: " << ::WSAGetLastError() << endl;
::WSACleanup();
system("pause");
exit(1);
}
RetVal = ::listen(SockServer, SOMAXCONN);
if (RetVal == SOCKET_ERROR)
{
cerr << "服务器Socket监听失败!错误代码: " << ::WSAGetLastError() << endl;
::WSACleanup();
system("pause");
exit(1);
}
cout << "服务器成功启动..." << endl;
}
Server::~Server()
{
::closesocket(SockServer);
::WSACleanup();
cout << "Socket关闭..." << endl;
}
void Server::wait_for_client()
{
while (1)
{
SOCKET SockClient;
SOCKADDR_IN AddrClient;
SockClient = ::accept(SockServer, (SOCKADDR*)&AddrClient, &AddrLen);
cout << "新的连接" << endl;
if (SockClient != INVALID_SOCKET)
{
thread* Thread = new thread(server_func, SockClient);
Thread->detach();
}
}
}
void server_func(SOCKET SockClient)
{
char RecvBuffer[BUF_SIZE];
memset(RecvBuffer, 0, BUF_SIZE);
int RecvResult = recv(SockClient, RecvBuffer, BUF_SIZE, 0);
if (RecvResult < 1)
return;
RecvResult = send(SockClient, RecvBuffer, BUF_SIZE, 0);
if (RecvResult < 1)
return;
::closesocket(SockClient);
}
};
Linux服务器端
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <thread>
#include <string.h>
const int QUEUE = 20;
const int PORT = 5000;
const int BUFFERSIZE = 1024;
class Server
{
public:
Server();
~Server();
void wait_for_client();
void data_trans(int);
private:
sockaddr_in ServerAddr;
sockaddr_in ClientAddr;
socklen_t AddrLen;
int ServerSocket;
};
int main()
{
Server Server;
Server.wait_for_client();
return 0;
}
Server::Server()
{
AddrLen = sizeof(ClientAddr);
ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(PORT);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(ServerSocket, (struct sockaddr*) & ServerAddr, sizeof(ServerAddr));
listen(ServerSocket, QUEUE);
std::cout << "服务器启动成功" << std::endl;
}
Server::~Server()
{
close(ServerSocket);
}
void Server::wait_for_client()
{
while (1)
{
int Descriptor = accept(ServerSocket, (struct sockaddr*) & ClientAddr, &AddrLen);
if (Descriptor != -1)
{
std::thread * Thread = new std::thread(&Server::data_trans, this, Descriptor);
Thread->detach();
}
}
}
void Server::data_trans(int Descriptor)
{
char Buffer[BUFFERSIZE];
memset(Buffer, 0, sizeof(Buffer));
ssize_t ret_val = recv(Descriptor, Buffer, sizeof(Buffer), 0);
if (ret_val != -1)
{
std::cout << Buffer << std::endl;
close(Descriptor);
return;
}
}