Tengo un módulo de adquisición de datos del cual me gustaría recopilar datos del puerto Ethernet. Estoy llegando en los pasos, actualmente me gustaría simplemente connect
al servidor de un cliente. He usado la guía de Beej para obtener el código C básico. Pero cada vez me siento este error de conexión connect: Connection refused.
C, zócalos: conexión Error rehusado
Esto es lo que hago:
La red IP mencionado aquí es IP ESTÁTICA que he configurado.
El número de puerto se establece en 50 000 en el lado del servidor y del lado del cliente conecto a esta IP en el puerto 50000.
que generar y ejecutar la aplicación del lado del servidor y luego tratar de conectarse a él ejecutando una aplicación del lado del cliente.
Una duda acerca del lado del servidor, la aplicación del lado del servidor returns
antes de que comience la aplicación del lado del cliente, por lo que debe mantenerlo funcionando (while(1);
) para que pueda conectarse a él desde el lado del cliente?
¿Qué está pasando, estoy olvidando algo aquí? ¡Ayuda!
estoy pegando el código en C modificado muy ligeramente (números IP y el puerto son diferentes) de Beej para el lado del servidor lado y cliente aquí:
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
}
Gracias por los enlaces son una buena lectura. – HaggarTheHorrible
@HeatfanJohn Comprobé un código que ha hecho esto, pero da como resultado el mismo problema. [Server.c] (https://paste.ubuntu.com/25599389/) [client.c] (https: //paste.ubuntu. com/25599391 /) – alhelal
@BandaMuhammadAlHelal Su servidor parece estar escuchando en el puerto 9999 mientras su cliente intenta conectarse al puerto FTP_PORT (21). Cambia tu cliente para usar el puerto 9999 para conectarte. – HeatfanJohn