gpt4 book ai didi

c - Unix 服务器客户端连接 : Connection refused

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

我在使用套接字创建我的第一个服务器-客户端应用程序时遇到了一些问题。我已将服务器的套接字地址硬编码为 localhost,端口为 55555。当我尝试在单独的终端中从客户端连接到它时键入命令 ./player localhost 55555,服务器没有任何反应。我认为应该发生的是服务器接收到“Rock”数据,然后打印“Client: ready.\nGo\n Client: Rock”。相反,服务器继续等待,对玩家没有任何反应。我怀疑我实际上并没有连接到服务器,所以我尝试将套接字地址“localhost”硬编码到播放器连接中,并将端口硬编码到 55555(也是硬编码)。我尝试运行命令 ./player 并显示“Connect: Connection Refused”。我想知道如何修复我的硬编码 player.c

referee.c(这是服务器)

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc, const char* argv[])
{
char buf[1024];
struct sockaddr_in localhost;
int listenSock, serviceSock, bytesRead;
size_t len = sizeof(localhost);
int port = 55555;
listenSock = socket(AF_INET, SOCK_STREAM, 0);
if (listenSock<0) {
perror("Socket");
exit(1);
}
bzero((char*)&localhost, len);
localhost.sin_family = AF_INET;
localhost.sin_addr.s_addr = htonl(INADDR_ANY);
localhost.sin_port = htons(port);
bind(listenSock, (struct sockaddr*) &localhost, len);
printf("Paper, Scissors, Rock game start.\nWaiting for player...\n");
listen(listenSock, 5);
do {
serviceSock = accept(listenSock, 0, 0);
if(serviceSock == -1){
perror("Accept");
}
else do {
bzero(buf, sizeof(buf));
if((bytesRead = read(serviceSock, buf, 1024))<0) {
perror("Read");
}
if(bytesRead > 0){
printf("Client: ready.\nGo\n Client: %s\n", buf);
}
} while (bytesRead != 0);
close(serviceSock);
} while(1);
}

player.c(客户端)

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

int main(int argc, char* argv[]) {
int sock;
struct sockaddr_in server;
struct hostent *hp;
char buf[1024], *data = "Rock";
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket");
exit(1);
}
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0) {
fprintf( stderr, "%s: unknown host", argv[1] );
exit(1);
}
bcopy( hp->h_addr, &server.sin_addr, hp->h_length );

server.sin_port = htons(atoi(argv[2]));
if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("Connect");
exit(1);
}
if(write(sock, data, strlen(data)) < 0) {
perror("Write");
}
close(sock);
}

具有硬编码地址/端口的 player.c

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

int main(int argc, char* argv[]) {
int sock;
struct sockaddr_in server;
struct hostent *hp;
char buf[1024], *data = "Rock";
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket");
exit(1);
}
server.sin_family = AF_INET;
hp = gethostbyname("localhost");
if (hp == 0) {
fprintf( stderr, "%s: unknown host", argv[1] );
exit(1);
}
bcopy( hp->h_addr, &server.sin_addr, hp->h_length );

server.sin_port = 55555;
if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("Connect");
exit(1);
}
if(write(sock, data, strlen(data)) < 0) {
perror("Write");
}
close(sock);
}

最佳答案

这是错误的:

server.sin_port = 55555;

应该是:

server.sin_port = htons(55555);

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

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