Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[GCC C] Client / Server Anwendung -> keine Ausgabe

Empfohlene Antworten

Hallo,

habe vor, eine Client/Server Anwendung zu schreiben, bei der kontinuierlich eine Anfrage erstellt und ein Response erzeugt werden soll.

Im konkreten Fall, soll "GET /" am Server ausgelesen werden und dann soll ein Text an den Client zurück geschrieben werden.

Aber das klappt irgendwie nicht ... kann mich da einer bitte helfen

Danke schonmal. ...


/////////////

//  mserver.c


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <netdb.h>


#define BUF_SIZ 4096


int send_request(const int sock, const char *hostname)

{

    char request[BUF_SIZ];


    snprintf(request, sizeof(request),

    "GET /\r\n"

    "Host: %s\r\n"

    "\r\n\r\n", hostname);


    if (send(sock, request, strlen(request), 0) == -1)

    {

        perror("send() failed");

        return 1;

    }


    return 0;

}


int view_response(const int sock)

{

    char response[BUF_SIZ];

    int bytes;


    while((bytes = recv(sock, response, sizeof(response), 0)) > 0)

        fwrite(response, 1, bytes, stdout);


    if (bytes < 0)

    {

        perror("recv() failed");

        return 1;

    }


    return 0;

}


int main(int argc, char *argv[])

{

    struct hostent *host;

    struct sockaddr_in addr;

    int s;


    if (argc < 2)

    {

        fprintf(stderr, "usage: %s <host>\n", argv[0]);

        return 1;

    }


    if (!inet_aton(argv[1], &addr.sin_addr))

    {

        host = gethostbyname(argv[1]);

        if (!host)

        {

            herror("gethostbyname() failed");

            return 2;

        }

        addr.sin_addr = *(struct in_addr*)host->h_addr;

    }


    s = socket(PF_INET, SOCK_STREAM, 0);

    if (s == -1)

    {

        perror("socket() failed");

        return 3;

    }


    printf("connecting to %s:7000...", inet_ntoa(addr.sin_addr));

    fflush(stdout);


    addr.sin_port = htons(7000);

    addr.sin_family = AF_INET;


    if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) == -1)

    {

        perror("connect() failed");

        return 4;

    }


    puts("ok.");


    if (send_request(s, argv[1]))

        return 5;


    if (view_response(s))

        return 6;


    close(s);


    return 0;

}




/////////////////////////////////////////////

// mclient.c


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>


#define BUF_SIZ 4096


int handle_client(const int sock)

{

  char buffer[BUF_SIZ], puffer[200], *p, pget[126];

  int bytes;


  strcpy(puffer,"");

  strcpy(pget,"GET /");

  while((bytes = recv(sock, buffer, sizeof(buffer), 0)) > 0)

  strcat(puffer,buffer);


  p = strstr(puffer,pget);

  fprintf(stderr,"puffer = %s\n",p);


  if (strlen(p) > 0) {

    strcpy(buffer,"Hallo getter :-)\r\n");

    send(sock, buffer, strlen(buffer), 0);

  } else {

    strcpy(buffer,"NOT FOUND\r\n");

    send(sock, buffer, strlen(buffer), 0);

  }


  return 0;

}


int main(void)

{

    int s, c, addr_len;

    struct sockaddr_in addr;


    s = socket(PF_INET, SOCK_STREAM, 0);

    if (s == -1)

    {

        perror("socket() failed");

        return 1;

    }


    addr.sin_addr.s_addr = INADDR_ANY;

    addr.sin_port = htons(7000);

    addr.sin_family = AF_INET;


    if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) == -1)

    {

        perror("bind() failed");

        return 2;

    }


    if (listen(s, 3) == -1)

    {

        perror("listen() failed");

        return 3;

    }


    for(;

    {

        addr_len = sizeof(addr);

        c = accept(s, (struct sockaddr*)&addr, &addr_len);

        if (c == -1)

        {

            perror("accept() failed");

            continue;

        }


        printf("Client from %s\n", inet_ntoa(addr.sin_addr));

        handle_client(c);


        close(c);

    }


    close(s);

    return 0;

}

[/code]

Aber das klappt irgendwie nicht

klappt nicht ist keine ... eh schon wissen ;-)

lauscht denn der server auf port 7000? netstat verrät dir das.

hast du es vom client aus mit telnet auf TCP 7000 versucht?

wenn du schon mit perror() eine fehlerbehandlung ausgibst, solltest du diese auch im post angeben.

s'Amstel

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.