用C语言与Winsock完成一对一Socket客户端和用户端程序
时间: 2024-02-05 13:12:01 浏览: 113
socket编程
以下是一个简单的基于Winsock的一对一Socket客户端和服务器端程序的示例,使用C语言编写:
客户端程序:
```
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
int iResult;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET)
{
printf("socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(27015);
iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR)
{
printf("connect failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
char sendbuf[1024] = {0};
char recvbuf[1024] = {0};
while (1)
{
printf("Enter message to send:");
fgets(sendbuf, 1024, stdin);
iResult = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
iResult = recv(ConnectSocket, recvbuf, 1024, 0);
if (iResult > 0)
{
printf("Message received: %s\n", recvbuf);
}
else if (iResult == 0)
{
printf("Connection closed\n");
break;
}
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
}
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
```
服务器端程序:
```
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
int iResult;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
SOCKET ListenSocket;
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET)
{
printf("socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(27015);
iResult = bind(ListenSocket, (SOCKADDR*)&service, sizeof(service));
if (iResult == SOCKET_ERROR)
{
printf("bind failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
printf("listen failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
SOCKET ClientSocket;
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
char recvbuf[1024] = {0};
char sendbuf[1024] = {0};
while (1)
{
iResult = recv(ClientSocket, recvbuf, 1024, 0);
if (iResult > 0)
{
printf("Message received: %s\n", recvbuf);
sprintf(sendbuf, "Received message: %s", recvbuf);
iResult = send(ClientSocket, sendbuf, strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
}
else if (iResult == 0)
{
printf("Connection closed\n");
break;
}
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
}
closesocket(ClientSocket);
closesocket(ListenSocket);
WSACleanup();
return 0;
}
```
注意:以上示例仅为演示目的,未进行错误处理等完整的实现。在实际开发中,需要进行更完整的错误处理。
阅读全文