如何理解socket,用c++语言vs2022编译器给出一个示例代码
时间: 2024-05-13 22:14:47 浏览: 11
Socket是一种用于网络通信的编程接口,它允许不同的计算机之间进行通信。Socket可以在不同的计算机之间传输数据,使得计算机之间的数据交换变得更加容易。
以下是一个使用C语言和VS2022编译器的socket示例代码,该代码创建一个TCP服务器并监听客户端的连接请求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define MAX_BUFFER_SIZE 1024
#define DEFAULT_PORT 9999
int main(int argc, char* argv[])
{
WSADATA wsaData;
SOCKET listenSocket, clientSocket;
struct sockaddr_in serverAddress, clientAddress;
int clientAddressLength, bytesReceived;
char buffer[MAX_BUFFER_SIZE];
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
// Create the socket
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET)
{
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Bind the socket to a specific address and port
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(DEFAULT_PORT);
if (bind(listenSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
{
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
// Listen for incoming connections
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR)
{
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Server is listening on port %d...\n", DEFAULT_PORT);
// Wait for a client to connect
clientAddressLength = sizeof(clientAddress);
clientSocket = accept(listenSocket, (struct sockaddr*)&clientAddress, &clientAddressLength);
if (clientSocket == INVALID_SOCKET)
{
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
printf("Client connected: %s:%d\n", inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port));
// Receive data from the client
while ((bytesReceived = recv(clientSocket, buffer, MAX_BUFFER_SIZE, 0)) > 0)
{
buffer[bytesReceived] = '\0';
printf("Received %d bytes from client: %s\n", bytesReceived, buffer);
// Echo the data back to the client
if (send(clientSocket, buffer, strlen(buffer), 0) == SOCKET_ERROR)
{
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
if (bytesReceived == SOCKET_ERROR)
{
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Client disconnected.\n");
// Cleanup
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
该代码创建了一个TCP服务器,它在本地监听端口9999上的连接请求。当客户端连接到服务器时,服务器接受连接并接收来自客户端的数据。收到数据后,服务器将数据回显给客户端。当客户端断开连接时,服务器关闭连接并退出。
需要注意的是,该示例代码仅适用于Windows平台,如果在其他平台上编译该代码,可能需要进行一些修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)