CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2004
    Posts
    2

    Basic client server help needed

    I have written a basic client server program (2 programs), and all it displays is the words hello world. the files are bellow.

    what i am trying to do is this:

    once the connection is astablished the user is asked for their user name, if they are reg then they get access to some information, a little text file, if they are not reg then they do not.

    can anyone help me with this.
    i am programming in c on linux.

    thanks in advance
    g

    /* An example client for "Hello, World!" server*/


    #include <stdio.h> /* perror() */
    #include <stdlib.h> /* atoi() */
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h> /* read() */
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>

    int main(int argc, char *argv[])
    {
    int clientSocket,
    remotePort,
    status = 0;
    struct hostent *hostPtr = NULL;
    struct sockaddr_in serverName = { 0 };
    char buffer[256] = "";
    char *remoteHost = NULL;

    if (3 != argc)
    {
    fprintf(stderr, "Usage: %s <serverHost> <serverPort>\n",
    argv[0]);
    exit(1);
    }

    remoteHost = argv[1];
    remotePort = atoi(argv[2]);

    clientSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (-1 == clientSocket)
    {
    perror("socket()");
    exit(1);
    }

    /*
    * need to resolve the remote server name or IP address
    */
    hostPtr = gethostbyname(remoteHost);
    if (NULL == hostPtr)
    {
    hostPtr = gethostbyaddr(remoteHost, strlen(remoteHost),
    AF_INET);
    if (NULL == hostPtr)
    {
    perror("Error resolving server address ");
    exit(1);
    }
    }

    serverName.sin_family = AF_INET;
    serverName.sin_port = htons(remotePort);
    (void) memcpy(&serverName.sin_addr, hostPtr->h_addr,
    hostPtr->h_length);

    status = connect(clientSocket, (struct sockaddr*) &serverName,
    sizeof(serverName));
    if (-1 == status)
    {
    perror("connect()");
    exit(1);
    }

    /*
    * Client application specific code goes here
    *
    * e.g. receive messages from server, respond, etc.
    */
    while (0 < (status = read(clientSocket, buffer,
    sizeof(buffer) - 1)))
    {
    printf("%d: %s", status, buffer);
    }

    if (-1 == status)
    {
    perror("read()");
    }

    close(clientSocket);

    return 0;
    }
    =================================


    /* An example client for "Hello, World!" server*/


    #include <stdio.h> /* perror() */
    #include <stdlib.h> /* atoi() */
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h> /* read() */
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>

    int main(int argc, char *argv[])
    {
    int clientSocket,
    remotePort,
    status = 0;
    struct hostent *hostPtr = NULL;
    struct sockaddr_in serverName = { 0 };
    char buffer[256] = "";
    char *remoteHost = NULL;

    if (3 != argc)
    {
    fprintf(stderr, "Usage: %s <serverHost> <serverPort>\n",
    argv[0]);
    exit(1);
    }

    remoteHost = argv[1];
    remotePort = atoi(argv[2]);

    clientSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (-1 == clientSocket)
    {
    perror("socket()");
    exit(1);
    }

    /*
    * need to resolve the remote server name or IP address
    */
    hostPtr = gethostbyname(remoteHost);
    if (NULL == hostPtr)
    {
    hostPtr = gethostbyaddr(remoteHost, strlen(remoteHost),
    AF_INET);
    if (NULL == hostPtr)
    {
    perror("Error resolving server address ");
    exit(1);
    }
    }

    serverName.sin_family = AF_INET;
    serverName.sin_port = htons(remotePort);
    (void) memcpy(&serverName.sin_addr, hostPtr->h_addr,
    hostPtr->h_length);

    status = connect(clientSocket, (struct sockaddr*) &serverName,
    sizeof(serverName));
    if (-1 == status)
    {
    perror("connect()");
    exit(1);
    }

    /*
    * Client application specific code goes here
    *
    * e.g. receive messages from server, respond, etc.
    */
    while (0 < (status = read(clientSocket, buffer,
    sizeof(buffer) - 1)))
    {
    printf("%d: %s", status, buffer);
    }

    if (-1 == status)
    {
    perror("read()");
    }

    close(clientSocket);

    return 0;
    }

  2. #2
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    628

    Re: Basic client server help needed

    well.. all you have to do is send and receive... to start with, instead of saying hello world, send a message asking for the username.. then have the client send it and verify it.. then send the text file.. what exactly is the problem? look into send and recv.

  3. #3
    Join Date
    Dec 2004
    Posts
    2

    Re: Basic client server help needed

    iv tried that, i dont know how to keep the client open,.

    i know how to send the message but not how to ask them to enter some data and then store it.

    any help would be great thanks
    (another novice)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured