• Welcome to Valhalla Legends Archive.
 

guys can u help with socket programming in c

Started by brave2010, March 30, 2010, 05:06 PM

Previous topic - Next topic

hi

hi
0 (0%)
hi
0 (0%)

Total Members Voted: 0

brave2010

'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;
}




#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;
}

Yegg

I didn't bother to look at the client code because you mentioned that what you are trying to solve is server side.

You have to think, your clientfd variable is what stores a descriptor of the client that has just connected to you. There is more than one way to achieve what you are attempting, but one way would be to have an array of type int, and instead of just assigning clientfd whatever the accept() function returns to you, add it to this array. And the array would store each and every client that connects to you.
Now I don't know all the details surrounding the purpose behind what you are doing or trying to do, so this particular method may not be the best. But it will certainly work. But it is also only good for learning from in example code. In a real application that were to be used outside your home for example, you would probably want to use some kind of dynamic array or a linked list to store those the client info instead.