我正在尝试创建一个非常基本的服务器,它使用套接字连接两个不同的进程。当尝试从客户端连接到服务器时,在我结束该过程之前没有任何事情发生,那么我连接到服务器的客户端数量就是它循环的次数。为什么要等到我终止显示信息的过程之后呢?
服务器端:
while(1){
if (listen(s1, 1) == -1) {
printf("/nListen error");
exit(1);
}
s0 = accept(s1, NULL, NULL);
if (s0 == -1) {
printf("/nAccept Error");
exit(1);
}
else
printf("/n Successfully Connected to client");
printf("attempting to write");
write(s0, "this", 3);
close(s0);
}
客户方:
int main(int args, char* argv[]) {
unlink("Client");
int s0, s1;
struct sockaddr name0 = { AF_UNIX, "Client" };
s0 = socket(AF_UNIX, SOCK_STREAM, 0);
if(s0 == -1)
return -1;
socklen_t clientlen = sizeof(struct sockaddr) + 7;
bind(s0, &name0, clientlen);
struct sockaddr name1 = { AF_UNIX, "Server" };
s1 = socket(AF_UNIX, SOCK_STREAM, 0);
if(s1==-1)
return -1;
socklen_t serverlen = sizeof(struct sockaddr) + 7;
bind(s1, &name1, clientlen);
if (connect(s0, &name1, serverlen) == -1) {
printf("connect() fail\n");
return -1;
}
card** deck = malloc(sizeof(card) * 52);
char buf[20];
read(s1, buf, sizeof(buf));
printf(buf);
printf("\nBye");
return 0;
}