gpt4 book ai didi

c - 套接字寻址机制卡住了

转载 作者:太空宇宙 更新时间:2023-11-04 08:47:53 25 4
gpt4 key购买 nike

基本上我正在尝试做一个与套接字通信的服务器-客户端程序。我觉得很奇怪,服务器一旦启动,它甚至不打印第一行。为什么是这样?一定有什么东西从我身上溜走了,我真的需要知道是什么。

服务器.c

int rvsock;

void stop(int sig){
close(rvsock);
}

void* worker(void* p){
struct mymsg m;
int err;
int sock = (int)p;
err = recv(sock,&m,sizeof(m),0);
if(err < 0){
printf("Failed to receive");
exit(1);
}

m.a = ntohl(m.a);
m.b = ntohl(m.b);
m.c = ntohl(m.c);
m.ip = ntohl(m.ip);

printf("Received numbers: %d %d %d from IP:%d", m.a,m.b,m.c,m.ip);

if(m.a < m.b && m.b <= m.c)
m.a = m.c;
else if(m.a < m.b && m.b >= m.c)
m.a = m.b;
else if(m.a > m.b && m.b <= m.c)
m.a = m.a;

m.a = htonl(m.a);
m.b = htonl(m.b);
m.c = htonl(m.c);
err = send(sock, &m,sizeof(m),0);
if(err < 0){
printf("Failed to send!");
close(sock);
return NULL;
}

close(sock);
return NULL;
}

int main(int argc, char** argv) {
printf("DAFUQ"); //It doesn't even print this. Why?
int port;
int sock;
int err;
unsigned int len;
struct sockaddr_in saddr;
struct sockaddr_in caddr;
pthread_t w[100];
int wn = 0;
int i;
sscanf(argv[1], "%d", &port);

signal(SIGINT, stop);

rvsock = socket(AF_INET, SOCK_STREAM, 0);
if(rvsock < 0) {
perror("Failed to create socket");
exit(1);
}

memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(port);

err = bind(rvsock, (struct sockaddr*)&saddr,
sizeof(struct sockaddr_in));
if(err < 0) {
perror("Failed to bind");
exit(1);
}

err = listen(rvsock, 5);
if(err < 0) {
perror("Failed to listen");
close(rvsock);
exit(1);
}

while(1) {
len = sizeof(struct sockaddr_in);
sock = accept(rvsock, (struct sockaddr*)&caddr, &len);
if(sock < 0) {
perror("Failed to accept");
break;
}

pthread_create(&w[wn], 0, worker, (int*)sock);
wn++;
}

for(i=0; i<wn; i++) {
pthread_join(w[i], 0);
}

return 0;
}

客户端.c

int main(int argc, char** argv){
int sock;
int err;
int port;
struct sockaddr_in saddr;
struct mymsg m;

sscanf(argv[1], "%d", &port);

sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0){
printf("failed to create");
exit(1);
}

memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
saddr.sin_port = htons(port);

err = connect(sock, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in));
if(err < 0){
perror("Failed to connect!");
exit(1);
}

printf("give a:");scanf("%d",&m.a);
printf("give b:");scanf("%d",&m.b);
printf("give c:");scanf("%d",&m.c);

m.a = htonl(m.a);
m.b = htonl(m.b);
m.c = htonl(m.c);

send(sock,&m,sizeof(m),0);

return 0;
}

消息.h

struct mymsg{
int a;
int b;
int c;
int ip;
};

最佳答案

在你的服务器中你有这一行。 printf 打印到当您​​启动程序时为您打开的标准输出文件。

printf("DAFUQ");   //It doesn't even print this. Why?

man stdout 提供有关 stdout 的以下信息:

The stream  stdout  is  line-buffered
when it points to a terminal. Partial lines will not appear until
fflush(3) or exit(3) is called, or a newline is printed.

您不打印换行符,不刷新 stdout 并且您的程序不退出,因此不满足将输出打印到终端所需的任何条件。

因此,为了打印该行,您有 3 个选项:

1

printf("DAFUQ");   //It doesn't even print this. Why?
fflush(stdout);

2

printf("DAFUQ\n"); /*notice added '\n'*/   

3打印后调用 exit()(可能不是很有帮助)。

关于c - 套接字寻址机制卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20945030/

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