Thread: Socket programming

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Socket programming

    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,

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're completely new to C, I would recommend starting with the basics, and tackling network and thread programming later on.

    What type of sockets are you using?
    Does your program use the "accept()" function?
    If so, what can this tell you?

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Socket Programming

    Thanks dear,
    My code in server is :
    Code:
    #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;
    }
    Thanks for your helps,

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Hamsaeed View Post
    My code in server is :
    Your code? What you've posted is extremely similar to the code here: c - Client connects to server randomly - Stack Overflow

    Furthermore, you've provided no answers to the questions I asked. There were clues in those questions that, if answered, might have proved useful.

    If you're simply trying to hack away at copied code with none of the necessary knowledge, there is not much help I can give you.

    Consider getting a good C programming book and starting at the very beginning. Maybe in a few months, you'll be ready to understand and write networking code that suits your purposes.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    I am sorry,
    in summery, my question is :
    How to get information of client in server ?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Read post #2 again, and do a little bit of research.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    Every time you accept a client, accept returns you a socket object which you can use to communicate with the client. After accepting a client, you should create a new thread to handle the communication between the server and that client. Now in order to be able to see all the clients on each thread, you need to create a list where you will store a reference of each socket object accept returns to you. So your code should look like this:

    Code:
    global list_of_clients;
    
    void handleClient(client)
    {
        //do all the code for the client communication here.
    
        //Now that you have a list with all the clients you can do this:
        msg = recv(client) //Receive something from the client.
         
        //Send the message which you received from current client
        //to all the other clients except the one who send it.
        for c in list_of_clients
        {
            if (c != client) send(msg, c);
        }
    }
    
    
    void handleAccepts()
    {
        new_socket = accept();
        list_of_clients.add( new_socket  );
        start_new_thread(handleClient, new_socket  );
    }
    Last edited by babaliaris; 12-29-2017 at 07:35 AM.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    Ok and thanks for helpful answers,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket programming!
    By ayokilabot in forum C Programming
    Replies: 2
    Last Post: 12-07-2010, 05:52 PM
  2. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  3. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  4. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM
  5. Socket Programming??
    By BigSky in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-10-2004, 12:12 PM

Tags for this Thread