使用 c connect() 连接被拒绝?
Connection refused using the c connect()?
我在使用 c connect() 打开与本地服务器的 TCP 连接时遇到了一些问题(我在使用 VirtualBox 制作的虚拟机上使用 Ubuntu 16.04)。当我启动服务器时一切正常,它开始监听 S_PORT。当客户端尝试使用 connect() 打开与它的 TCP 连接时,程序停止,并且 errno 设置为 111(连接被拒绝)。有关更多信息,我 post 下面的代码:
客户代码:
int main(int argc,char* argv[]){
int sock_cli;
int err;
unsigned short port = 0;
struct in_addr addr;
struct sockaddr_in addr_serv;
struct hostent* ptr = NULL;
messaggio* m = (messaggio*)malloc(sizeof(messaggio));
/* Opening socket */
sock_cli = socket(PF_INET,SOCK_STREAM,0);
if(sock_cli == -1){
printf(" (client) problem with socket() function \n");
return -1;
}
else printf(" (client) socket open successfully, fd: %d \n",sock_cli);
#ifdef _ARG
printf(" (client) argc : %d \n",argc);
for(int i=0;i<argc;i++) printf( " (client) argv[%d] = %s \n",i,argv[i]);
#endif
/* Set address/port */
if(argc == 3){
if(!strcmp(argv[1],"localhost")){
ptr = gethostbyname("localhost");
}
else inet_aton(argv[1],&addr);
port = atoi(argv[2]);
#ifdef _DEBUG
printf(" (client) port %d \n",port);
#endif
}
else{
printf(" (client) few arguments, args = [host] [port] \n");
return -1;
}
/* Set addr_serv */
addr_serv.sin_family = AF_INET;
addr_serv.sin_port = htons(port);
memcpy(&addr_serv.sin_addr.s_addr,ptr->h_addr,ptr->h_length);
memset(addr_serv.sin_zero,'[=10=]',sizeof(addr_serv.sin_zero));
err = connect(sock_cli,(struct sockaddr*)&addr_serv,sizeof(addr_serv));
if(err == -1){
printf(" (client) connect() failed %d \n",errno);
return -1;
}
else printf(" (client) connect() open successfully \n");
m->type = 1;
m->a = rand() % 10 + 1;
m->b = rand() % 10 + 1;
if(send(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0) > 0){
printf(" (client) couple send (%d,%d) \n",m->a,m->b);
}
recv(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0);
#ifdef _DEBUG
printf(" (client) msg received, product: %d \n",m->prod);
#endif
close(sock_cli);
free(m);
return 0;
}
这是服务器代码:
#define S_PORT 3550
#define N_CLI 5
int main(){
int sock_serv; // server socket
int sock_cli; // client socket
unsigned int sin_size;
int err;
struct sockaddr_in addr_serv;
struct sockaddr_in addr_cli;
messaggio* m = (messaggio*)malloc(sizeof(messaggio));
/* Opening socket */
sock_serv = socket(PF_INET,SOCK_STREAM,0);
if(sock_serv == -1) {
printf(" (server) problem with socket() function \n");
return -1;
}
else printf(" (server) socket open successfully, fd: %d \n",sock_serv);
/* Set addr_serv */
addr_serv.sin_family = AF_INET;
addr_serv.sin_port = htons(S_PORT);
addr_serv.sin_addr.s_addr = htonl(INADDR_ANY);
memset(addr_serv.sin_zero,'[=11=]',sizeof(addr_serv.sin_zero));
/* Bind */
err = bind(sock_serv,(struct sockaddr*)&addr_serv,sizeof(addr_serv));
if(err == -1){
printf(" (server) bind() failed \n");
return -1;
}
else printf(" (server) bind() ok, address 'localhost', port %d \n",addr_serv.sin_port);
/* Listen */
err = listen(sock_serv,N_CLI);
if(err == -1) {
printf(" (server) listen() failed \n");
return -1;
}
else printf(" (server) listening \n");
/* Accept */
sin_size = sizeof(addr_cli);
sock_cli = accept(sock_serv,(struct sockaddr*)&addr_cli,&sin_size);
if(sock_cli == -1){
printf(" (server) no connection open \n");
return -1;
}
else printf(" (server) connection open with client, sock_cli: %d \n",sock_cli);
recv(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0); // bloccante
m->prod = (m->a)*(m->b);
send(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0);
close(sock_cli);
printf(" (server) connection close \n");
close(sock_serv);
free(m);
return 0;
}
这是Output
的一个画面
端口错误。您绑定到端口 3550 (0xdde),但随后您 打印 非字节交换版本 56845 (0xde0d)。然后您尝试连接到客户端中的那个。
当您在服务器中打印时,您需要换回本机字节顺序 (ntohs(addr_serv.sin_port)
) 或仅将“3550”传递给客户端,因为这是您在服务器中绑定的内容。
我在使用 c connect() 打开与本地服务器的 TCP 连接时遇到了一些问题(我在使用 VirtualBox 制作的虚拟机上使用 Ubuntu 16.04)。当我启动服务器时一切正常,它开始监听 S_PORT。当客户端尝试使用 connect() 打开与它的 TCP 连接时,程序停止,并且 errno 设置为 111(连接被拒绝)。有关更多信息,我 post 下面的代码:
客户代码:
int main(int argc,char* argv[]){
int sock_cli;
int err;
unsigned short port = 0;
struct in_addr addr;
struct sockaddr_in addr_serv;
struct hostent* ptr = NULL;
messaggio* m = (messaggio*)malloc(sizeof(messaggio));
/* Opening socket */
sock_cli = socket(PF_INET,SOCK_STREAM,0);
if(sock_cli == -1){
printf(" (client) problem with socket() function \n");
return -1;
}
else printf(" (client) socket open successfully, fd: %d \n",sock_cli);
#ifdef _ARG
printf(" (client) argc : %d \n",argc);
for(int i=0;i<argc;i++) printf( " (client) argv[%d] = %s \n",i,argv[i]);
#endif
/* Set address/port */
if(argc == 3){
if(!strcmp(argv[1],"localhost")){
ptr = gethostbyname("localhost");
}
else inet_aton(argv[1],&addr);
port = atoi(argv[2]);
#ifdef _DEBUG
printf(" (client) port %d \n",port);
#endif
}
else{
printf(" (client) few arguments, args = [host] [port] \n");
return -1;
}
/* Set addr_serv */
addr_serv.sin_family = AF_INET;
addr_serv.sin_port = htons(port);
memcpy(&addr_serv.sin_addr.s_addr,ptr->h_addr,ptr->h_length);
memset(addr_serv.sin_zero,'[=10=]',sizeof(addr_serv.sin_zero));
err = connect(sock_cli,(struct sockaddr*)&addr_serv,sizeof(addr_serv));
if(err == -1){
printf(" (client) connect() failed %d \n",errno);
return -1;
}
else printf(" (client) connect() open successfully \n");
m->type = 1;
m->a = rand() % 10 + 1;
m->b = rand() % 10 + 1;
if(send(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0) > 0){
printf(" (client) couple send (%d,%d) \n",m->a,m->b);
}
recv(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0);
#ifdef _DEBUG
printf(" (client) msg received, product: %d \n",m->prod);
#endif
close(sock_cli);
free(m);
return 0;
}
这是服务器代码:
#define S_PORT 3550
#define N_CLI 5
int main(){
int sock_serv; // server socket
int sock_cli; // client socket
unsigned int sin_size;
int err;
struct sockaddr_in addr_serv;
struct sockaddr_in addr_cli;
messaggio* m = (messaggio*)malloc(sizeof(messaggio));
/* Opening socket */
sock_serv = socket(PF_INET,SOCK_STREAM,0);
if(sock_serv == -1) {
printf(" (server) problem with socket() function \n");
return -1;
}
else printf(" (server) socket open successfully, fd: %d \n",sock_serv);
/* Set addr_serv */
addr_serv.sin_family = AF_INET;
addr_serv.sin_port = htons(S_PORT);
addr_serv.sin_addr.s_addr = htonl(INADDR_ANY);
memset(addr_serv.sin_zero,'[=11=]',sizeof(addr_serv.sin_zero));
/* Bind */
err = bind(sock_serv,(struct sockaddr*)&addr_serv,sizeof(addr_serv));
if(err == -1){
printf(" (server) bind() failed \n");
return -1;
}
else printf(" (server) bind() ok, address 'localhost', port %d \n",addr_serv.sin_port);
/* Listen */
err = listen(sock_serv,N_CLI);
if(err == -1) {
printf(" (server) listen() failed \n");
return -1;
}
else printf(" (server) listening \n");
/* Accept */
sin_size = sizeof(addr_cli);
sock_cli = accept(sock_serv,(struct sockaddr*)&addr_cli,&sin_size);
if(sock_cli == -1){
printf(" (server) no connection open \n");
return -1;
}
else printf(" (server) connection open with client, sock_cli: %d \n",sock_cli);
recv(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0); // bloccante
m->prod = (m->a)*(m->b);
send(sock_cli,(void*)m,sizeof(messaggio)-sizeof(long),0);
close(sock_cli);
printf(" (server) connection close \n");
close(sock_serv);
free(m);
return 0;
}
这是Output
的一个画面端口错误。您绑定到端口 3550 (0xdde),但随后您 打印 非字节交换版本 56845 (0xde0d)。然后您尝试连接到客户端中的那个。
当您在服务器中打印时,您需要换回本机字节顺序 (ntohs(addr_serv.sin_port)
) 或仅将“3550”传递给客户端,因为这是您在服务器中绑定的内容。