一、用Winsock的API套接字函数实现如下功能:服务器端接收客户端的连接,并向客户端发送“hello ,I am server !”.客户端显示接收到的内容。当一个客户端连接到服务器端后,在服务器端显示该客户端的IP地址。(25分)
时间: 2024-02-27 18:56:32 浏览: 133
以下是用 Winsock 的 API 套接字函数实现的服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") // 链接 ws2_32.lib 库
int main()
{
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrLen = sizeof(clientAddr);
char sendBuf[] = "hello, I am server!";
char recvBuf[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");
return 1;
}
// 绑定服务器端套接字
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Failed to bind server socket!\n");
closesocket(serverSocket);
return 1;
}
// 监听客户端连接
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("Failed to listen server socket!\n");
closesocket(serverSocket);
return 1;
}
// 接收客户端连接并发送欢迎消息
printf("Waiting for client...\n");
clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to accept client socket!\n");
closesocket(serverSocket);
return 1;
}
printf("Client %s connected!\n", inet_ntoa(clientAddr.sin_addr));
send(clientSocket, sendBuf, sizeof(sendBuf), 0);
// 接收客户端的消息
printf("Waiting for message...\n");
int recvLen = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (recvLen > 0) {
recvBuf[recvLen] = '\0';
printf("Received message from client: %s\n", recvBuf);
}
// 关闭套接字并清理 Winsock
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
以下是用 Winsock 的 API 套接字函数实现的客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") // 链接 ws2_32.lib 库
int main()
{
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
char sendBuf[] = "Hello, server!";
char recvBuf[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");
return 1;
}
// 连接服务器端
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Failed to connect to server!\n");
closesocket(clientSocket);
return 1;
}
// 接收服务器端的消息
printf("Waiting for message...\n");
int recvLen = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (recvLen > 0) {
recvBuf[recvLen] = '\0';
printf("Received message from server: %s\n", recvBuf);
}
// 发送消息给服务器端
send(clientSocket, sendBuf, sizeof(sendBuf), 0);
// 关闭套接字并清理 Winsock
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
阅读全文