WindowsTCP消息通信(包括套接字中IP和端口读取方法)服务器和客户端的c代码
时间: 2023-05-15 14:08:06 浏览: 91
TCP套接字的服务器与客户端
以下是一个简单的 Windows TCP 服务器和客户端的 C 代码示例:
服务器端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrLen = sizeof(clientAddr);
char buffer[1024];
// 初始化 Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建服务器套接字
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
printf("Failed to create server socket.\n");
WSACleanup();
return 1;
}
// 绑定服务器套接字到本地 IP 地址和指定端口
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(12345);
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Failed to bind server socket.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 开始监听客户端连接请求
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("Failed to listen on server socket.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server started, listening on port 12345...\n");
// 接受客户端连接请求
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to accept client connection.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Client connected from %s:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 接收客户端发送的数据
int recvLen = recv(clientSocket, buffer, sizeof(buffer), 0);
if (recvLen > 0) {
buffer[recvLen] = '\0';
printf("Received message from client: %s\n", buffer);
} else {
printf("Failed to receive message from client.\n");
}
// 关闭套接字和清理 Winsock
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
char buffer[1024];
// 初始化 Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建客户端套接字
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to create client socket.\n");
WSACleanup();
return 1;
}
// 连接到服务器
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(12345);
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Failed to connect to server.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Connected to server.\n");
// 发送数据到服务器
const char* message = "Hello, server!";
int sendLen = send(clientSocket, message, strlen(message), 0);
if (sendLen == SOCKET_ERROR) {
printf("Failed to send message to server.\n");
} else {
printf("Sent message to server: %s\n", message);
}
// 关闭套接字和清理 Winsock
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
这些代码演示了如何使用 Windows 套接字 API 创建一个简单的 TCP 服务器和客户端,并在它们之间发送消息。在服务器端,我们首先创建一个套接字并将其绑定到本地 IP 地址和指定端口,然后开始监听客户端连接请求。当客户端连接时,我们接受连接并接收客户端发送的消息。在客户端端,我们创建一个套接字并连接到服务器,然后发送一条消息。
阅读全文