Ho un modulo di acquisizione dati dal quale vorrei raccogliere i dati dalla porta Ethernet. Ci sto arrivando nei passaggi, attualmente mi piacerebbe solo il connect
al server da un client. Ho usato la guida di Beej per ottenere il codice C di base. Ma ho appena continuo a ricevere questo errore di connessione connect: Connection refused.
C, Socket: errore di connessione rifiutata
Questo è quello che faccio:
La rete IP qui menzionato è IP STATICO che ho configurato.
Il numero di porta è impostato su 50000 sul lato server e dal lato client si collega a questo IP sulla porta 50000.
ho compilare ed eseguire l'applicazione lato server e quindi provare a connettersi ad esso eseguendo un'applicazione lato client.
Un dubbio su lato server, applicazioni lato server returns
prima di iniziare l'applicazione lato client, quindi devo mantenere in esecuzione (while(1);
) in modo che io possa connettersi ad esso dal lato client?
Cosa non va sto dimenticando qualcosa qui? Aiuto!
Sto incollando il (numeri IP e la porta sono diversi) di codice C molto leggermente modificata del Beej per il lato client e lato server qui:
server.c
/*
** server.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
// code for a server waiting for connections
// namely a stream socket on port 3490, on this host's IP
// either IPv4 or IPv6.
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP address
if ((rv = getaddrinfo(NULL, "50000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("bind");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL)
{
// looped off the end of the list with no successful bind
fprintf(stderr, "failed to bind socket\n");
exit(2);
}
freeaddrinfo(servinfo); // all done with this structure
}
Client.c
/*
** client.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
// code for a client connecting to a server
// namely a stream socket to www.example.com on port 80 (http)
// either IPv4 or IPv6
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo("192.168.2.4", "50000", &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("connect");
continue;
}
break; // if we get here, we must have connected successfully
}
if (p == NULL)
{
// looped off the end of the list with no connection
fprintf(stderr, "failed to connect\n");
exit(2);
}
freeaddrinfo(servinfo); // all done with this structure
}
Grazie per i link sono una buona lettura. – HaggarTheHorrible
@HeatfanJohn Ho controllato un codice che ha fatto questi, ma risulta lo stesso problema. [Server.c] (https://paste.ubuntu.com/25599389/) [client.c] (https: //paste.ubuntu. it/25599391 /) – alhelal
@BandaMuhammadAlHelal Il server sembra in ascolto sulla porta 9999 mentre il client sta tentando di connettersi alla porta FTP_PORT (21). Cambia il tuo client per utilizzare la porta 9999 per connettersi. – HeatfanJohn