windows c语言 socket编程实例
时间: 2023-06-30 15:01:47 浏览: 161
### 回答1:
以下是一个简单的 Windows 平台下使用 C 语言编写的 socket 编程实例:
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#define MAX_BUFFER_SIZE 1024
int main()
{
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrLen = sizeof(clientAddr);
char buffer[MAX_BUFFER_SIZE];
char response[MAX_BUFFER_SIZE] = "Hello from server!";
// 初始化 Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to initialize winsock.\n");
return 1;
}
// 创建服务器 socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
printf("Failed to create server socket.\n");
WSACleanup();
return 1;
}
// 设置服务器地址和端口
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(12345);
// 绑定服务器地址和端口到 socket
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("Failed to bind server address.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 开始监听客户端连接
if (listen(serverSocket, 5) == SOCKET_ERROR)
{
printf("Failed to listen for client connections.\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server is listening for incoming connections...\n");
// 接受客户端连接并进行通信
while (1)
{
// 等待客户端连接
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: %s:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 接收客户端发送的数据
memset(buffer, 0, sizeof(buffer));
if (recv(clientSocket, buffer, sizeof(buffer), 0) == SOCKET_ERROR)
{
printf("Failed to receive data from client.\n");
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Received data from client: %s\n", buffer);
// 发送响应给客户端
if (send(clientSocket, response, strlen(response), 0) == SOCKET_ERROR)
{
printf("Failed to send response to client.\n");
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Response sent to client: %s\n", response);
// 关闭客户端 socket
closesocket(clientSocket);
}
// 关闭服务器 socket
closesocket(serverSocket);
// 清理 Winsock
WSACleanup();
return 0;
}
在该例子中,服务器创建一个 socket 监听客户端的连接请求。一旦有客户端连接进来,服务器会接收客户端发送的数据,然后返回一个固定的响应给客户端。
### 回答2:
以下是一个使用Windows的C语言编写的简单socket编程实例:
```c
#include <stdio.h>
#include <winsock2.h>
#define MAX_BUFFER_SIZE 1024
#define DEFAULT_PORT 8080
int main()
{
WSADATA wsaData;
SOCKET listenSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrSize;
char buffer[MAX_BUFFER_SIZE];
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Winsock库初始化失败.\n");
return 1;
}
// 创建监听socket
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket == INVALID_SOCKET)
{
printf("创建socket失败.\n");
WSACleanup();
return 1;
}
// 设置服务器地址和端口
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(DEFAULT_PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
// 绑定socket到服务器地址和端口
if (bind(listenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("绑定socket失败.\n");
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 开始监听连接请求
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR)
{
printf("监听连接请求失败.\n");
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("服务器正在监听端口 %d ...\n", DEFAULT_PORT);
while (1)
{
// 接受客户端连接请求
clientSocket = accept(listenSocket, (struct sockaddr *)&clientAddr, &clientAddrSize);
if (clientSocket == INVALID_SOCKET)
{
printf("接收连接失败.\n");
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 从客户端读取数据
memset(buffer, 0, sizeof(buffer));
if (recv(clientSocket, buffer, MAX_BUFFER_SIZE, 0) == SOCKET_ERROR)
{
printf("接收数据失败.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
// 打印接收到的数据
printf("接收到来自客户端的数据: %s\n", buffer);
// 发送响应给客户端
const char *response = "服务器已收到你的请求.";
if (send(clientSocket, response, strlen(response), 0) == SOCKET_ERROR)
{
printf("发送响应失败.\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
// 关闭客户端socket
closesocket(clientSocket);
}
// 关闭监听socket
closesocket(listenSocket);
// 清理Winsock库
WSACleanup();
return 0;
}
```
这个例子创建了一个服务器程序,它通过在本地8080端口监听连接请求。一旦有客户端连接,服务器将接收该客户端的请求,并向客户端发送一个简单的响应。此例仅用于演示目的,并省略了错误处理和多线程处理。在实际开发中,您还需要根据需求添加适当的错误处理和多线程支持。
### 回答3:
以下是一个简单的Windows C语言socket编程实例:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#define MAX_BUFFER_SIZE 1024
#define PORT 8080
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddress, clientAddress;
char buffer[MAX_BUFFER_SIZE];
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Failed to initialize Winsock\n");
return 1;
}
// 创建服务器socket
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
fprintf(stderr, "Failed to create server socket\n");
WSACleanup();
return 1;
}
// 绑定服务器地址和端口
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
fprintf(stderr, "Failed to bind server socket\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 监听客户端连接请求
if (listen(serverSocket, 1) == SOCKET_ERROR) {
fprintf(stderr, "Failed to listen for client connections\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server is listening on port %d\n", PORT);
// 接受客户端连接
int clientAddressSize = sizeof(clientAddress);
if ((clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressSize)) == INVALID_SOCKET) {
fprintf(stderr, "Failed to accept client connection\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Client connected\n");
// 接收和发送数据
while (1) {
memset(buffer, 0, sizeof(buffer));
if (recv(clientSocket, buffer, sizeof(buffer), 0) == SOCKET_ERROR) {
fprintf(stderr, "Failed to receive data from client\n");
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Received from client: %s", buffer);
if (send(clientSocket, buffer, strlen(buffer), 0) == SOCKET_ERROR) {
fprintf(stderr, "Failed to send data to client\n");
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 1;
}
}
// 关闭socket和Winsock库
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
以上是一个简单的基于Windows平台的C语言socket编程示例。它创建了一个服务器端socket,并通过指定的端口监听客户端连接请求。一旦有客户端连接成功,服务器将接收来自客户端的数据,并将其发送回客户端。这个过程将持续进行,直到服务器或客户端关闭连接。
阅读全文