gpt4 book ai didi

c - 从客户端传递多条消息 ->服务器和服务器 -> C中的客户端套接字

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:27 26 4
gpt4 key购买 nike

有人可以帮助确定为什么我的服务器不能接受来自客户端的多条消息吗?

我正在尝试让流程如下所示:
1.客户端向服务器发送消息大小
2. 服务器收到大小并发回响应。在本例中为 0。
3. 客户端检查响应,然后将消息写入服务器。
4. 服务器读取消息并打印出来。

我遇到的问题是第 4 步的 accept() 永远不会解除阻塞。

客户

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(int argc, char *argv[])
{
int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in s_address;
s_address.sin_family = AF_INET;
s_address.sin_port = htons(51717);
s_address.sin_addr.s_addr = INADDR_ANY;
if (connect(sock, (struct sockaddr *) &s_address, sizeof(s_address)) < 0) {
printf("ERROR: Cannot connect()\n");
exit(0);
}

char *org_msg = "Hello";

printf("Writing size of Hello\n");
char msg1[1];
msg1[0] = sizeof(org_msg);
write(sock, msg1, sizeof(msg1));

printf("Waiting for response from server\n");
struct sockaddr_in c_address;
socklen_t c_length = sizeof(c_address);
int new_sock = accept(sock, (struct sockaddr *) &c_address, &c_length);

printf("Reading response from server\n");
char stat[1];
read(new_sock, stat, 1);

if (atoi(stat) == 0) {
printf("Writing Hello to server\n");
write(sock, org_msg, sizeof(org_msg));
}
close(sock);
close(new_sock);
}

服务器

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(int argc, char *argv[])
{
int sock = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in s_address;
s_address.sin_family = AF_INET;
s_address.sin_port = htons(51717);
s_address.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr *) &s_address, sizeof(s_address)) < 0) {
printf("ERROR: Cannot bind()\n");
exit(0);
}

listen(sock, 3);

printf("Waiting for client message\n");
struct sockaddr_in c_address;
socklen_t c_length = sizeof(c_address);
int new_sock = accept(sock, (struct sockaddr *) &c_address, &c_length);

printf("Reading client message\n");
char msg[1];
read(new_sock, msg, 1);

printf("Writing response to client\n");
char stat[1];
stat[0] = '0';
write(new_sock, stat, sizeof(stat));

printf("Waiting for client message\n");
int new_sock2 = accept(sock, (struct sockaddr *) &c_address, &c_length);

printf("Reading client message\n");
char msg2[atoi(msg)];
read(new_sock2, msg2, sizeof(msg2));

printf("MESSAGE: %s\n", msg2);

close(sock);
close(new_sock);
close(new_sock2);
}

最佳答案

您不应在已连接的套接字上调用 accept()。一旦你在服务器中有一个连接的套接字(由 accept() 返回的套接字)你应该继续读取和写入该套接字直到连接关闭。服务器的步骤应该类似于:

listen_socket = socket(...);
listen(listen_socket, ...);
connected_socket = accept(listen_socket, ...);
read(connected_socket, ...)
write(connected_socket, ...)
read(connected_socket, ...)
write(connected_socket, ...)
...

同样,一旦连接成功,客户端应该继续读写套接字 - 客户端的步骤应该是:

connected_socket = socket(...);
connect(connected_socket, ...);
write(connected_socket, ...);
read(connected_socket, ...);
write(connected_socket, ...);
read(connected_socket, ...);
...

关于c - 从客户端传递多条消息 ->服务器和服务器 -> C中的客户端套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13076596/

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