Get client property in socket programing

Hi all,
I am comletely new in c programing,
I have a server client program in c , that I use thread in it,
Client send a sentence and server convert it to upper case and send to client,
My question is:
How can in server find which client connect to server, for example :
If a request receive from client 1 ,server send hello client 1 , and for client 2, it send hello client 2, and etc,

Very thanks for your helps,

My code is
Thanks dear,
My code in server is :
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

int main(){
  int welcomeSocket, newSocket, portNum, clientLen, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
  int i;

  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;
  
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  addr_size = sizeof serverStorage;

  /*loop to keep accepting new connections*/
  while(1){
    newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
    /*fork a child process to handle the new connection*/
    if(!fork()){
      nBytes = 1;
      /*loop while connection is live*/
      while(nBytes!=0){
        nBytes = recv(newSocket,buffer,1024,0);
  
        for (i=0;i<nBytes-1;i++){
          buffer[i] = toupper(buffer[i]);
        }

        send(newSocket,buffer,nBytes,0);
        
      }
      close(newSocket);
      exit(0);
    }
    /*if parent, close the socket and go back to listening new requests*/
    else{
      close(newSocket);
    }
  }

  return 0;
}

-----------------------------------client code is
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
  int clientSocket, portNum, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  clientSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;

  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

  while(1){
    printf("Type a sentence to send to server:\n");
    fgets(buffer,1024,stdin);
    printf("You typed: %s",buffer);

    nBytes = strlen(buffer) + 1;

    send(clientSocket,buffer,nBytes,0);

    recv(clientSocket, buffer, 1024, 0);

    printf("Received from server: %s\n\n",buffer);   
  }

  return 0;
}

Open in new window


Thanks for your helps,
Hamid SaeedAsked:
Who is Participating?
 
sarabandeCommented:
/*fork a child process to handle the new connection*/

since you have a new child process for each client, this process exclusively would serve to one client.

fork is not an easy way to handle multiple clients, especially for a beginner. perhaps  you better would use a new thread for each accepted client.

Sara
1
 
Hamid SaeedAuthor Commented:
Very thanks dear,
Can you help me please, for a use case for my question on use pthreads for handle each client ?
0
 
sarabandeCommented:
you would put the code block after if (!fork()) into a function which looks like

void mythreadfunc(void * parg)
{
      int newSocket = (int)parg;
      int nBytes = -1;
      /*loop while connection is alive*/
      while(nBytes!=0){
        nBytes = recv(newSocket,buffer,1024,0);
  
        for (i=0;i<nBytes;i++){
          buffer[i] = toupper(buffer[i]);
        }

        send(newSocket,buffer,nBytes,0);
        
      }
      close(newSocket);
}

Open in new window


note, from your code i assume that the newSocket is blocking. that means it would block with the first recv since the client first connects and then sends data. that - normally - could be enough time for the server to create a thread and start it. hence the first recv blocks until the client successfully has sent data. the thread func would receive the 1024 bytes probably with one recv call. it also is possible that it get less than 1024 bytes what wouldn't bother the server code as it runs in an infinite loop which only ends when the connection is broken. if the connection is alive it either reads successfully or waits for input.

at the client side things are similar. the client sends a 1024 byte buffer and reads up to 1024 bytes. this recv call may block for a while if the server is busy. but finally will read successfully. however, after read it will goon and there is no way to stop. you should improve that by two measures. first, replace the while(1) by something like while(++n < 10) where n is a counter initialized to 0. after while loop you should close the conetcion and terminate, second, put some random sleep time into the while loop such that you can watch what happened at both sides.

in the server code you would call pthread_create instead of fork. pass the thread function as function pointer to pthread_create and cast the newsocket (handle) to void* as argument. in any case do not close the newsocket in the main thread because the connection should be alive as long as the client sends data.

Sara
0
 
sarabandeCommented:
Question was answered and code sample given.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.