help with socket programming in c

i'm doing a simple program in socket programming on c i have server that can handle 2clients in a single machine i'm running ubuntu linux so i got it work but the probelm when clients send a message the server will echo it but i cant distinguish which client send the message client 1 or client 2 here is the code for the server whenever a client sent a message it will be shown in the server side as( client sent : hi)i cant know which client sent it please help

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
 
#define BUFFER_SIZE  1024
 
int serverfd;
int clientfd;
 
int n;
int addrSize;
int serverPort;
char clientIP[25];
char buffer[BUFFER_SIZE];
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
 
int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("Usage: ./server <port>\n");
        exit(0);
    }
    else
    {
        serverPort = atoi(argv[1]);
    }
 
    addrSize = sizeof(struct sockaddr);
 
    /* Create a TCP socket */
    serverfd = socket(AF_INET, SOCK_STREAM, 0);
 
    if(serverfd < 0)
        printf("Error creating server socket");
    serverAddr.sin_family = AF_INET; 
    serverAddr.sin_port = htons(serverPort); /* Specify a port */
    serverAddr.sin_addr.s_addr = INADDR_ANY; /* Accept connections from anywhere */
    memset(&(serverAddr.sin_zero), '\0', 8); /* This must be set to zero */
 
    /* Bind our server socket */
    if(bind(serverfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)
        printf("Could not bind()\n");
 
    /* Start listening on socket for incoming connections */
    if(listen(serverfd, 10) == -1)
        printf("Could not listen()\n");
    fork();//this is the magical line...
    /* accept() accepts an incoming connection and fills a sockaddr_in structure with client information */
    clientfd = accept(serverfd, (struct sockaddr*)&clientAddr, &addrSize);
    
    /* inet_ntoa() converts an IP address to a displayable string */
    snprintf(clientIP, sizeof(clientIP), "%s", inet_ntoa(clientAddr.sin_addr));
    printf("Got connection from %s\n", clientIP);
 
    do
    {
        n = recv(clientfd, buffer, BUFFER_SIZE, 0); /* Receive message from client */
        buffer[n] = '\0'; /* terminate string */
 
        send(clientfd, buffer, BUFFER_SIZE, 0); /* Echo message back to client */
        printf("Client sent: %s\n" , buffer);
    }
    while(strncmp(buffer, "exit", 4) != 0); /* Exit when client sends "exit" */
 
    close(serverfd); /* Close socket */ 
    
    return 0;
}

and this is the client.c code

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include
<arpa/inet.h>
#include
<sys/types.h>
#include
<sys/socket.h>

#define
BUFFER_SIZE  1024

int sockfd;

int n;
int addrSize;
int serverPort;
char serverIP[25];
char
buffer[BUFFER_SIZE];
struct sockaddr_in serverAddr;

int main(int argc, char* argv[])
{
    if(argc < 3)
    {
        printf("Usage: ./client <server ip> <server port>\n");
        exit(0);
    }
    else
    {
        snprintf(serverIP, sizeof(serverIP), "%s", argv[1]);
        serverPort = atoi(argv[2]);
    }

    addrSize = sizeof(struct sockaddr);

      /* Create a TCP socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if(sockfd < 0)
        printf("Error creating socket");

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(serverPort); /* Server port */
    inet_pton(AF_INET, serverIP, &serverAddr.sin_addr); /* Server IP Address */
    memset(&(serverAddr.sin_zero), '\0', 8); /* This must be set to zero */

      /* Connect to server */
    if(connect(sockfd, (struct sockaddr*)&serverAddr, addrSize) != 0)
    {
        printf("Error: Could not connect to %s on port %d\n", serverIP, serverPort);
        exit(0);
    }
    else
        printf("Connected to %s on port %d\n\n", serverIP, serverPort);

    do
    {
        printf("> ");
        fgets(buffer, BUFFER_SIZE, stdin);
        send(sockfd, buffer, strlen(buffer), 0); /* Send message to server */

        n = recv(sockfd, buffer, BUFFER_SIZE, 0); /* Receive message from server */
        buffer[n] = '\0'; /* terminate string */
        printf("< %s\n", buffer);
    }
    while(strncmp(buffer, "exit", 4) != 0); /* Exit when message is "exit" */

    close(sockfd); /* Close socket */

    return 0;
}

With getpeername()?

where should i add this method sir?

Try something like this after the accept call:

socklen_t address_length;
int rval;

address_length = sizeof (serverAddr);
rval = getpeername (clientfd, &serverAddr, &address_length);
assert (rval == 0);

printf (�connection accepted from %s\n�, inet_ntoa (serverAddr.sin_addr));

its not working...

Your server is not able to handle multiple clients. You are doing forking and then sleeping for connection. You should be doing accept() and then fork a new child to handle a new connection. accept() should be there in a loop. e.g pseudo code -

do {
  fd = accept()
  if (fd < 0) {
    // this is error
  } 
  child = fork()
  if (0) {
    // this is child, handle the connection here do read/ write operations.
  }

} while (some condition to exit from sever loop)

Now server will have two child each handling different connection.

i followed that way its wont run i guess that code is okay the matter is that i need to identify each of the two clients bcoz when they chatting with the server it will be like client sent: hi client say hello so which client is saying so client one or two thats the matter..

Then I would say you must change the data structures to use an array of structure which should also include the connection fd and use select() to handling multiple connections. select() will tell you which of connection is having data in their socket buffer.

struct conn {
  int fd;
  int svbuf_len;
  int clbuf_len;
  char srvbuf[1024];
  char cl_buf[1024];
  // put more thing if need.
};