用windo环境编译用c语言完成一个基于socket网络编程的信息收发程序。要求如下: 1单机同时模拟完成服务器端和客户端的功能即可; 2功能要求客户端发一条消息,服务器端收到对应得消息
时间: 2024-02-06 09:10:38 浏览: 78
c_server:c语言发送接收socket 消息
以下是一个基于socket网络编程的信息收发程序的代码实现,该程序在Windows环境下使用C语言编写,同时模拟了服务器端和客户端的功能。
服务器端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// 创建一个socket来监听连接请求
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
printf("socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 绑定socket到指定的IP地址和端口号
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(27015);
if (bind(listenSocket, (SOCKADDR *) &service, sizeof(service)) == SOCKET_ERROR) {
printf("bind failed: %ld\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 监听连接请求
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("listen failed: %ld\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Waiting for client to connect...\n");
// 接受客户端的连接请求
SOCKET clientSocket = INVALID_SOCKET;
clientSocket = accept(listenSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
printf("accept failed: %ld\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Client connected.\n");
// 接收来自客户端的消息
char recvbuf[1024];
int recvbuflen = 1024;
iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Message received: %s\n", recvbuf);
} else if (iResult == 0) {
printf("Connection closed.\n");
} else {
printf("recv failed: %d\n", WSAGetLastError());
}
// 关闭socket和Winsock
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
// 初始化Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// 创建一个socket来连接服务器
SOCKET connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connectSocket == INVALID_SOCKET) {
printf("socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 设置服务器的IP地址和端口号
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(27015);
// 连接服务器
iResult = connect(connectSocket, (SOCKADDR *) &serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
printf("connect failed: %ld\n", WSAGetLastError());
closesocket(connectSocket);
WSACleanup();
return 1;
}
printf("Connected to server.\n");
// 发送消息给服务器
char sendbuf[1024] = "Hello, server!";
int sendbuflen = strlen(sendbuf);
iResult = send(connectSocket, sendbuf, sendbuflen, 0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(connectSocket);
WSACleanup();
return 1;
}
printf("Message sent.\n");
// 关闭socket和Winsock
closesocket(connectSocket);
WSACleanup();
return 0;
}
```
运行这两个程序,先运行服务器端,然后再运行客户端,客户端会向服务器端发送一条消息,服务器端收到对应的消息并输出到控制台。
阅读全文