Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:27:57+00:00 2026-05-28T03:27:57+00:00

If I run the client program from two different shells, how should the server

  • 0

If I run the client program from two different shells, how should the server be able to determine to whom he is talking to currently?

Platform: Linux, GCC

Client code: This client takes the host name as an argument.

#define SERVERPORT 3490
#define MAXDATASIZE 1000 

int main (int argc, char *argv[])
{
    int             clientSocketFD;
    int                 numOfBytesReceived;
    char                    receivedData[MAXDATASIZE];
    struct hostent                  *objHostent;
    struct sockaddr_in              serverAddress;

    if (argc != 2) 
    {
        fprintf (stderr,"usage: key in client hostname.\n");
        exit (1);
    }

    if ((objHostent = gethostbyname (argv[1])) == NULL)
    {
        herror ("error: incorrect host name specified.");
        exit (1);
    }

    if ((clientSocketFD = socket (PF_INET, SOCK_STREAM, 0)) == -1) 
    {
        perror ("error: socket system call failed.");
        exit (1);
    }

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port    = htons (SERVERPORT);
    serverAddress.sin_addr   = *((struct in_addr *)objHostent->h_addr);
    memset (serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);

    if (connect (clientSocketFD, (struct sockaddr *)&serverAddress, sizeof serverAddress) == -1)
    {
        perror ("connect() error");
        exit (1);
    }

    while (1)
    {
        if ((numOfBytesReceived = recv (clientSocketFD, receivedData, MAXDATASIZE-1, 0)) == -1)
        {
            perror ("\nrecv() error");
        }

        receivedData [numOfBytesReceived] = '\0';

        if (send (clientSocketFD, "Get Lost", 15, 0) == -1)
        {
            perror ("send() error");
        }
    }
    close (clientSocketFD);
    return 0;
}

Server code:

#define SERVERPORT 3490
#define BACKLOG 10

struct threadArguments
{
    int    threadId;
    int    listeningSocketDescriptor;
    char *message;
};

struct threadArguments threadDataArray [5];

void* genericThreadFunction (void* threadData)
{
    struct threadArguments *temp;
    temp         = (struct threadArguments *) threadData;

    printf ("Hello World! It's me, thread #%ld!\n", (long) temp->threadId);

    while (1)
    {
        if (send (temp->listeningSocketDescriptor, temp->message, 14, 0) == -1)
            perror ("\nsend() error\n");

        char receivedData [14];
        int    numOfBytesReceived;
        if ((numOfBytesReceived = recv (temp->listeningSocketDescriptor, receivedData, 14-1, 0)) == -1)
        {
            perror ("\nrecv() error\n");
        }
        else
        {
            receivedData [numOfBytesReceived] = '\0';
            printf ("\nReceived: %s\n", receivedData);
        }
    }
}

int main (void)
{
    int                 serverSocketFD;
    int                 newServersocketFD;
    struct sockaddr_in          serverAddress;

    struct sockaddr_in          clientAddress;
    socklen_t           sin_size;

    if ((serverSocketFD = socket (AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror ("socket");
        exit (1);
    }
    int yes=1;
    if (setsockopt (serverSocketFD, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    {
        perror ("setsockopt");
        exit (1);
    }

    serverAddress.sin_family     = AF_INET;                 // host byte order
    serverAddress.sin_port       = htons (SERVERPORT);      // short, network byte order
    serverAddress.sin_addr.s_addr = INADDR_ANY;             // automatically fill with my IP
    memset (serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);

    if (bind (serverSocketFD, (struct sockaddr *)&serverAddress, sizeof serverAddress) == -1)
    {
        perror ("bind");
        exit (1);
    }

    if (listen (serverSocketFD, BACKLOG) == -1)
    {
        perror ("listen"); 
        exit (1);
    }

    pthread_t threads [5];
    int     returnValueOfPthreadCreate;
    long    threadId = -1;

    while (1) 
    {
        sin_size = sizeof clientAddress;
        if ((newServersocketFD = accept (serverSocketFD, (struct sockaddr *)&clientAddress, &sin_size)) == -1) 
        {
                perror ("accept");
                continue;
        }
        printf ("accept(): got connection from %s\n", inet_ntoa (clientAddress.sin_addr));

        threadId++;

        threadDataArray [threadId].threadId                  = threadId;
        threadDataArray [threadId].listeningSocketDescriptor = newServersocketFD;
        threadDataArray [threadId].message                   = "Excuse Me, please!";

        returnValueOfPthreadCreate = pthread_create (&threads [threadId], NULL, genericThreadFunction, (void*) &threadDataArray[threadId]);

        if (returnValueOfPthreadCreate)
        {
            printf ("ERROR; return code from pthread_create() is %d\n", returnValueOfPthreadCreate);
        }
    }

    pthread_exit (NULL);
    return 0;
}

EDIT 1.

Peername returns 0:

if ((newServersocketFD                                                 = accept (serverSocketFD, (struct sockaddr *)&clientAddress, &sin_size)) == -1) 
        {
                perror ("accept");
                continue;
        }
        printf ("accept(): got connection from %s\n", inet_ntoa (clientAddress.sin_addr));
        printf ("\npeername: %d", getpeername (newServersocketFD, (struct sockaddr *)&clientAddress, &sin_size));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:27:58+00:00Added an answer on May 28, 2026 at 3:27 am

    The client’s ip-address and (source-)port, fetched as described below, do identify a client(-connection) uniquely.

    If you are using address family IF_INET to create the listen()ing socket you can pull the client’s ip-address and (the client’s connection specific) (source-)port from the variable of type struct sockaddr_in which’s reference was passed to accept() by using it’s members sin_addr and sin_porteach time accept() returns successfully.

    Depending on the platform you are on, you need to change the byte order of such members (see man-page for ntoh() on how to do this). For directly converting the member sin_addr to a character array you are already using a method (inet_ntoa()) taking care of the byte order.

    Using getpeername() on the file descriptor which was return by the successful accept() (and, in the context of the server process, is also uniquely identifying the client’s connection) shall return the same values for ip-address and port of the peer as the call to accept() did.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run client server UDP program . My both machines are
I have a server program and a client program. I built it based from
I'm writing a simple program that will run entirely client-side. (Desktop programming? do people
I have an application which get copied and run on client machines. The program
Currently we run our web applications on a thin client browser IE 6 and
I have a client/server program that attempts to send and receive an object. There
I'm trying to create a client-server program that send and receive a string (its
I have two simple Python files: client.py and server.py . The client simply sends
I have a server and a client program (both running on the same machine).
We are designing a client/server program and I need some help with a design

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.