简体   繁体   中英

Connection refused TCP sockets

I made a simple client and a simple server with TCP sockets.When I was testing them, I got an error message: Connection refused. I have opened the ports for the server, so I don't understand why I get this error... Can you help me? This is the client's source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

#define PORTA 3459


int main()
{
char indirizzo[15];
char buffer[20];
struct sockaddr_in client;
int clients;
puts("Inserire l'indirizzo");
fgets(indirizzo, 15, stdin);
printf("L'indirizzo del destinatario è %s",indirizzo);
puts("Inserire il messaggio");
fgets(buffer, 20, stdin);
client.sin_family = AF_INET;
client.sin_port = htons(PORTA);
client.sin_addr.s_addr = inet_addr(indirizzo);
memset(client.sin_zero, '\0',8);
if((clients = socket(PF_INET, SOCK_STREAM,0)) == -1)
{
printf("%s\n", strerror(errno));
exit(0);    
}
if( (connect(clients, (struct sockaddr *)&client, sizeof(structsockaddr))   ) == -1)
{
printf("%s\n", strerror(errno));
exit(0);    
}
send(clients, buffer, 20,0);
close(clients);
return 0;

}

This is the server's source code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

#define PORTA 3459

 int main()
 {
struct sockaddr_in sock;
int socks;
struct sockaddr_in newsock;
int newsocks;
  if((socks = socket(PF_INET, SOCK_STREAM,0)) == -1)
  {
  puts("Errore: socks non inizializzato\n");
  printf("%s\n", strerror(errno));
  exit(0);
  }
sock.sin_family = AF_INET;
sock.sin_port = htons(PORTA);
sock.sin_addr.s_addr = htonl(INADDR_ANY);
memset(sock.sin_zero, '\0',8);
int si = 1;
if(setsockopt(socks,SOL_SOCKET,SO_REUSEADDR,&si,sizeof(int)) == -1)
{
puts("Errore durante il settaggio del socket\n");
printf("%s\n", strerror(errno));
exit(0);
}
if(bind(socks,(struct sockaddr *)&sock, sizeof(struct sockaddr) ) == -1)
{
puts("Errore durante il binding\n");
printf("%s\n", strerror(errno));
exit(0);
}
char buffer[30];
int lung;
lung = sizeof(newsock);
listen(socks, 5);
   if((newsocks = accept(socks,(struct sockaddr *)&newsock,&lung)) == -1)
   {
   puts("Errore durante l'accettazione del socket remoto\n");
   printf("%s\n", strerror(errno));
   exit(0);
   }
if(recv(newsocks, buffer,sizeof(buffer),0) == -1)
{
puts("Errore durante la ricezione dei dati");
printf("%s\n", strerror(errno));
}
puts(buffer); 
return 0;
}

Code looks good in general, except for not controlling errors in some cases (inet_addr) and the ports are the same both in client and server. If you get connetion refused error, it cannot be a firewall-related problem.

Do you have the same error always or sometimes it works OK?

Most probably the problem is one of these:

1-Client is trying to connect to the wrong IP address. Check the address printed and result of inet_addr.

2-The server doesn't have a loop to keep listening for connections. It finishes after getting one connection. Maybe a first test works then server ends and client is run again getting connection refused.

You could also try not setting SO_REUSEADDR. I don't think it's related but if nothing else works ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM