C, Sockel: Connection Refused Fehler

Habe ich eine Datenerfassungs-Modul, von dem ich möchte, um Daten zu sammeln über den Ethernet-Anschluss. Ich bin es immer in Schritten, aktuell würde ich gerne nur connect server von einem client aus. Ich habe Beej ' s guide to Holen Sie sich die basic C-code. Aber ich bekomme immer diese Verbindung Fehler connect: Connection refused.

Das ist was ich tun:

  1. Die Netzwerk-IP, die hier erwähnt ist STATISCHE IP -, die ich konfiguriert haben.

  2. Die port-Nummer eingestellt ist, um 50000 auf Server-Seite und client-Seite habe ich eine Verbindung zu dieser IP auf port 50000.

  3. Ich erstellen und ausführen von der server-Seite Anwendung und dann versuchen, eine Verbindung zu es, indem Sie einen client-Anwendung.

Einen Zweifel über die server-Seite server-Seite Anwendung returns bevor ich anfange, die client-seitige Anwendung, so sollte ich es laufen ( while(1); ), so dass ich eine Verbindung herstellen können, um es von der client-Seite?

Was schief geht, bin ich etwas vergessen hier? Hilfe!!!

Ich bin einfügen, die sehr leicht verändert werden (IP-und port-Nummern sind unterschiedlich) Beej ' s C-code für Server-Seite und Client-Seite hier:

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

}
Schreibe einen Kommentar