Client Problem



  • Moin habe einen kleinen Client der will aber nicht verbinden und ich habe einfach keine ahnung woran er liegt habe ihn aus mehreren tuts zusammen gedikselt ....help

    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <stdlib.h>
    /* Windows-System */
    #ifdef _WIN32
    #include <iostream.h>
    #include <winsock.h>
    #include <io.h>
    /* Unix-System */
    #else
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

    int sock;
    struct sockaddr_in host_addr;
    struct hostent *hostinfo;
    char *host;

    char buf[1024];
    unsigned int bytes_sent, bytes_recv;

    /* ggf. Winsock initialisieren */
    #ifdef _WIN32
    WSADATA wsaData;
    if (WSAStartup (MAKEWORD(1, 1), &wsaData) != 0) {
    fprintf (stderr, "WSAStartup(): Kann Winsock nicht initialisieren.\n");
    exit (EXIT_FAILURE);
    }
    #endif

    /* Socket erzeugen */
    sock = socket (AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
    perror ("socket()");
    exit (EXIT_FAILURE);
    }

    /* Adresse des Servers festlegen */
    memset( &host_addr, 0, sizeof (host_addr));
    host_addr.sin_family = AF_INET;
    host_addr.sin_port = htons (7447);

    host_addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
    if (host_addr.sin_addr.s_addr == INADDR_NONE) {
    /* Server wurde nicht mit IP sondern mit dem Namen angegeben /
    hostinfo = gethostbyname ("127.0.0.1");
    if (hostinfo == NULL) {
    perror ("gethostbyname()");
    exit (EXIT_FAILURE);
    }
    memcpy((char
    ) &host_addr.sin_addr.s_addr, hostinfo->h_addr, hostinfo->h_length);
    }

    /* Verbindung aufbauen */
    if (connect(sock, (struct sockaddr 😉 &host_addr, sizeof(struct sockaddr)) == INVALID_SOCKET) {
    {
    puts("Fehler beim Erstellen des Socket!\n");
    }
    exit (EXIT_FAILURE);

    }

    // define variables (welcmsg is the welcome msg for the server)
    char welcmsg[]="Willkommen.\n\n\r\r";
    int bytes;

    // send welcomemsg
    send(sock, welcmsg, strlen(welcmsg), 0);

    // send "TEXT ";
    strcpy(buf, "Na Alles Fit: ");
    bytes = send(sock, buf, strlen(buf), 0);

    // Antwort des Servers empfangen und ausgeben */
    while ((bytes_recv = recv (sock, buf, sizeof(buf), 0)) > 0) {
    write (1, buf, bytes_recv);
    }
    if (bytes_recv == -1) {
    perror ("recv()");
    exit (EXIT_FAILURE);
    }

    printf ("\n");

    #ifdef _WIN32
    closesocket(sock);
    WSACleanup();
    #else
    close(sock);
    #endif

    return 0;

    }



  • Dieser Thread wurde von Moderator/in davie aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • sry jungs habe den Fehler gefunden es lag an meinem router trotzdem danke ...


Anmelden zum Antworten