Echo server dla TCP – błąd uruchomienia w drugim terminalu

0

Witam ,próbuję utworzyć prosty echo serwer dla komunikacji TCP, oto kod :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to listen for incoming data
void error(char* s)
{
    perror(s); // define what to do when there is an error
    exit(1); // exit the main() function
}
int main(void)
{
    struct sockaddr_in si_me, si_other; // creates structures
    int s, i, slen = sizeof(si_other), recv_len; // creates ints
    char buf[BUFLEN];
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        error("socket"); // if socket() return -1 -- error
    }
    // zero out the structure
    memset((char*)&si_me, 0, sizeof(si_me)); // put zeros into structure
    si_me.sin_family = AF_INET; // specify address family (IPv4)
    si_me.sin_port = htons(PORT); // specify port
    si_me.sin_addr.s_addr = htonl(INADDR_ANY); //binds to all ip addresses
    //bind socket to port
    if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
        error("bind"); // if bind() return -1 -- error
    }
    //keep listening for data
    while (1) // inf loop
    {
        printf("Waiting for data..."); // show info regarding state
        fflush(stdout); // clears the buffer
        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen)) == -1) {
            error("recvfrom()"); // if receiving failed -- error
        }
        //print details of the client/peer and the data received
        printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Data: %s\n", buf); //now reply the client with the same data if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1) // &si_other saves data from where packet was read
        {
            error("sendto()"); // if sending returned -1 -- error
        }
    }
    close(s);
    return 0;
}

Następnie kompiluje program i odpalam go w 1 terminalu. Włączam drugi terminal wpisuję komendę ncat –vv localhost 8888 -u i pokazuje się komunikat:

Ncat: Could not resolve hostname "–vv": Name or service not known. QUITTING.

Nie wiem co może być przyczyną.

0

A zamiana "-vv" na "-v" nie pomaga? Plus jeśli serwer działa po TCP to czemu próbujesz używać UDP (opcja "-u")?

0

Przy kompilacji mam komunikat:

In function ‘main’:
: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
     close(s);
     ^~~~~

Uruchomienie programu:

Waiting for data...recvfrom(): Transport endpoint is not connected

ncat daje taki sam komunikat.

0

Kod wygląda jakbyś wziął echo serwer UDP i zmienił w nim protokół na TCP. Ale to nie zadziała, bo TCP jest protokołem stanowym. Proponuję zrobić listen+accept albo zmienić protokół z TCP na UDP.

Sprawdź jak wygląda maszyna stanowa protokołu TCP. Twój socket nie jest nawet w stanie listen, co dopiero mówić o nawiązaniu połączenia i wymianie danych.
http://tcpipguide.com/free/diagrams/tcpfsm.png

W skrócie - listen przejdzie w stan LISTEN, accept poczeka na SYN i zwróci deskryptory pliku połączenia po 3way handshake, czyli w stanie ESTABLISHED.

0

Dzięki, za odpowiedzi, jednak problem pozostaje u mnie ten sam :

Ncat: Could not resolve hostname "–v": Name or service not known. QUITTING.

Jakbym nie próbował, ten Ncat nie rusza i co bym zamiast -v nie wpisał zawsze jest could not resolve

0

man ncat i sprawdź składanie. Dawno byś problem rozwiązał samemu

1 użytkowników online, w tym zalogowanych: 0, gości: 1