gpt4 book ai didi

C服务器连接到客户端

转载 作者:行者123 更新时间:2023-11-30 19:39:16 27 4
gpt4 key购买 nike

我正在尝试创建一个带有推送通知的服务器/客户端。

首先客户端与服务器连接,然后服务器保存客户端的IP地址和端口。有时服务器需要与客户端连接以发送通知。

这是服务器在接受第一个连接后保存客户端 IP 地址和端口的方式:

char client_ip[INET_ADDRSTRLEN];
int port_c;
// Socket, ..., bind, listen, accept
// Gets Client IP address
struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&cli_addr;
struct in_addr ipAddr = pV4Addr->sin_addr;
inet_ntop( AF_INET, &ipAddr, client_ip, INET_ADDRSTRLEN );
printf("Client ip: %s\n", client_ip);

// Gets Client Port Number
port_c = (int) ntohs(cli_addr.sin_port);
printf("Client port is: %d\n", (int) ntohs(cli_addr.sin_port));

// [ ... ]

// Time to send notification to client
bzero((char *) &cli_addr, sizeof(cli_addr));
// Setting IP address and port
cli_addr.sin_family = AF_INET;
bcopy(client_ip, (char *)&cli_addr.sin_addr.s_addr, strlen(client_ip));
cli_addr.sin_port = htons(port_c);

// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);

// Bind socket to the local address
if(bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0)
error("Error bind");

if (connect(sockfd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) < 0)
error("ERROR connecting");

我收到绑定(bind)错误:无法分配请求的地址。而且我不确定如何在第一次连接后对客户端进行编码。它将监听并接受连接,但如果我在客户端代码中这样做

cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = INADDR_ANY;
cli_addr.sin_port = htons(port_client);

IP 地址是否与第一次连接相同?

如果有人有其他解决方案或更好的解决方案,请分享。

最佳答案

您正在尝试将服务器套接字绑定(bind)到客户端 IP。那是行不通的。您只能将套接字绑定(bind)到本地地址。您必须将绑定(bind)用于传入连接,而不是传出连接。

如果您希望服务器响应客户端的请求,只需使用 accept() 返回的套接字,然后使用此套接字send()即可。

您的服务器代码应如下所示:

SOCKET sock_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in sock_local_addr;
memset(&sock_local_addr, 0, sizeof(sock_local_addr));
sock_local_addr.sin_family = AF_INET;
sock_local_addr.sin_port = htons(SERVER_PORT);
sock_local_addr.sin_addr.s_addr = INADDR_ANY;
bind(sock_server, (const struct sockaddr *)&sock_local_addr, sizeof(sock_local_addr)); // bind server socket to any network interface of the server
listen(sock_server, 10);
struct sockaddr_in client_addr;
SOCKLEN_T client_addr_size = sizeof(client_addr);
SOCKET sock_client = accept(sock_server, (struct sockaddr *)&client_addr, &client_addr_size); // accept incomming connection; this will block until a client connects
recv_size = recv(sock_client, buf_in, sizeof(buf_in), 0); // receive data from client
send(sock_client, buf_out, sizeof(buf_out), 0); // send data back to client (buf_out is buffer containing the data to be sent)
shutdown(sock_client, SHUT_RDWR); // shutdown connection with client

关于C服务器连接到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com