服务器源码:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <winsock2.h>
#include <iostream>
using namespace std;#pragma comment(lib, "ws2_32.lib")#define PORT 8888
#define BUFFER_SIZE 4196const char* kExitFlag = "exit";int main() {WORD winsock_version = MAKEWORD(2, 2);WSADATA wsa_data;if (WSAStartup(winsock_version, &wsa_data) != 0) {printf("Failed to init socket dll!\n");return 1;}SOCKET server_socket= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (server_socket == INVALID_SOCKET) {printf("Failed to create server socket!\n");return 2;}struct sockaddr_in server_addr;server_addr.sin_family = AF_INET;server_addr.sin_port = htons(PORT);server_addr.sin_addr.S_un.S_addr = INADDR_ANY;if (bind(server_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {printf("Failed to bind port!\n");return 3;}if (listen(server_socket, 10)) {printf("Failed to listen!\n");return 4;}struct sockaddr_in client_addr;int client_addr_len = sizeof(client_addr);printf("Wait for connecting...\n");SOCKET client_socket = accept(server_socket, (SOCKADDR*)&client_addr, &client_addr_len);if (client_socket == INVALID_SOCKET) {printf("Failed to accept!\n");return 5;}printf("Succeed to receive a connection: %s\n", inet_ntoa(client_addr.sin_addr));char recv_buf[BUFFER_SIZE + 1];while (true) {int ret = recv(client_socket, recv_buf, BUFFER_SIZE, 0);if (ret < 0) {printf("Failed to receive data!\n");break;}recv_buf[ret] = 0;printf("Receive from Client: \"%s\" \n", recv_buf);if (strcmp(kExitFlag, recv_buf) == 0) {printf("Exit!\n");break;}char send_data[BUFFER_SIZE + 1];printf("Enter message to send to client: ");cin.getline(send_data, BUFFER_SIZE);printf("Send to Client: \"%s\" \n", send_data);send(client_socket, send_data, strlen(send_data), 0);}closesocket(client_socket);closesocket(server_socket);WSACleanup();return 0;
}
客户端源码:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <winsock2.h>
#include <iostream>
using namespace std;#pragma comment(lib, "ws2_32.lib")
#define PORT 8888
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 4196const char* kExitFlag = "exit";int main() {WORD winsock_version = MAKEWORD(2, 2);WSADATA wsa_data;if (WSAStartup(winsock_version, &wsa_data) != 0) {printf("Failed to init socket!\n");return 1;}SOCKET client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (client_socket == INVALID_SOCKET) {printf("Failed to create server socket!\n");return 2;}struct sockaddr_in server_addr;server_addr.sin_family = AF_INET;server_addr.sin_port = htons(PORT);server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP);if (connect(client_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {printf("Failed to connect server: %ld !\n", GetLastError());return 3;}char recv_data[BUFFER_SIZE + 1];while (true) {char data[BUFFER_SIZE + 1];printf("Input data: ");cin.getline(data, BUFFER_SIZE);if (send(client_socket, data, strlen(data), 0) < 0) {printf("Failed to send data!\n");break;}int ret = recv(client_socket, recv_data, BUFFER_SIZE, 0);if (ret < 0) {printf("Failed to receive data!\n");break;}recv_data[ret] = 0;printf("Receive data from server: \"%s\"\n", recv_data);if (strcmp(data,kExitFlag)==0) {printf("Exit!\n");break;}}closesocket(client_socket);WSACleanup();return 0;
}
编译指令(假设服务器源码为server.cpp,客户端源码为client.cpp):
g++ server.cpp -o server.exe -lwsock32 -lws2_32
g++ client.cpp -o client.exe -lwsock32 -lws2_32