c++ 编写socket
时间: 2023-09-05 12:08:35 浏览: 116
在 C++ 中编写 Socket 程序需要使用到 socket 编程库,如 Winsock、Berkeley sockets 等。以下是一个简单的使用 Winsock 编写的服务器端和客户端的示例:
服务器端代码:
```
#include <winsock2.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
sockaddr_in serverAddr, clientAddr;
int serverPort = 8888;
char buffer[1024];
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "WSAStartup failed!" << std::endl;
return -1;
}
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
std::cout << "socket failed!" << std::endl;
WSACleanup();
return -1;
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(serverPort);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cout << "bind failed!" << std::endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cout << "listen failed!" << std::endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
std::cout << "Server is listening on port " << serverPort << std::endl;
while (true) {
int clientAddrLen = sizeof(clientAddr);
clientSocket = accept(serverSocket, (sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
std::cout << "accept failed!" << std::endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
std::cout << "Client connected: " << inet_ntoa(clientAddr.sin_addr) << std::endl;
while (true) {
memset(buffer, 0, sizeof(buffer));
int recvBytes = recv(clientSocket, buffer, sizeof(buffer), 0);
if (recvBytes == SOCKET_ERROR) {
std::cout << "recv failed!" << std::endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
else if (recvBytes == 0) {
std::cout << "Client disconnected: " << inet_ntoa(clientAddr.sin_addr) << std::endl;
break;
}
std::cout << "Received data from client: " << buffer << std::endl;
int sendBytes = send(clientSocket, buffer, recvBytes, 0);
if (sendBytes == SOCKET_ERROR) {
std::cout << "send failed!" << std::endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
}
closesocket(clientSocket);
}
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
客户端代码:
```
#include <winsock2.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET clientSocket;
sockaddr_in serverAddr;
char buffer[1024];
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "WSAStartup failed!" << std::endl;
return -1;
}
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
std::cout << "socket failed!" << std::endl;
WSACleanup();
return -1;
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cout << "connect failed!" << std::endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
std::cout << "Connected to server!" << std::endl;
while (true) {
std::cout << "Please input data to send: ";
std::cin.getline(buffer, sizeof(buffer));
if (strcmp(buffer, "exit") == 0) {
break;
}
int sendBytes = send(clientSocket, buffer, strlen(buffer), 0);
if (sendBytes == SOCKET_ERROR) {
std::cout << "send failed!" << std::endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
memset(buffer, 0, sizeof(buffer));
int recvBytes = recv(clientSocket, buffer, sizeof(buffer), 0);
if (recvBytes == SOCKET_ERROR) {
std::cout << "recv failed!" << std::endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
std::cout << "Received data from server: " << buffer << std::endl;
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
阅读全文