Thursday, January 17, 2013

Chat Server simulation : Socket programming in C for multiple clients using Posix threads

Compile the code for SERVER and CLIENT as two separate programs with the argument -lpthread example : gcc -o server server.c -lpthread gcc -o client client.c -lpthread and then execute in the order ./server ./client

SERVER

CLIENT

27 comments:

  1. I execute this code but when i give it username and password it stop... so please give a brief discussion how to use it.. and what is this "user.txt" file for? thanks

    ReplyDelete
    Replies
    1. first run the server code with a specified port and then execute the client code while the server is running. The client is to be connected to the same port to which server is listening.
      username is to be given in the format user-...

      user.txt file stores the usernames of the users that are logged in...

      Delete
  2. can you please tell me how to send same message to all the clients currently connected to the server......it would be great pleasure....and how to block a particular user in multiclient-server chat system....using c language..
    Gyaneshwar Prasad... i am student of nitc and i have networks lab this sem ..plzz help....and reply asap.. thanxxxx.... :)

    ReplyDelete
    Replies
    1. The struct 'u' holds the socket info all the users that are logged in. Just use a loop and send the message to all.

      for(j=1;j<nu;j++)
      send(u[j].socket,buffer,sizeof buffer,0);
      (some slight changes may be needed in the code)

      To block a particular client add a field in the userinfo struct for 'blocked users'. When msg comes check whether the sender is in the blocked users list of the receiver. If not the message can be delivered else the message is to be blocked.

      Hope it was helpful

      Delete
  3. in server program I am typing : ./server 6000
    in client program I am typing : ./client 127.0.0.1 6000
    is it ok???
    but then after putting username and passwor and msg it is showing "Error reading from socket" in client terminal and segmentation fault in server terminal. why???

    ReplyDelete
    Replies
    1. username should be in the format user-
      eg: user-1, user-2....

      Message should be in the format

      To user-2: hello

      So you've to start atleast two client programs for sending message.

      Delete
    2. message format is not correct error message whats the corect format i try with this user-1: hello

      Delete
  4. To Godly T.Alias, Thank you very much for your ideas man! But, I couldn't find download link :P pls fix it quickly! If you can, just mail that code to sperumalcse@gmail.com
    :: Thanx in advance ::

    ReplyDelete
    Replies
    1. You can just copy-paste the code from my blog. No need of download link for that.

      Delete
    2. what is your blog

      Delete
  5. gcc -o client client1.c -lpthread
    Undefined first referenced
    symbol in file
    recv /var/tmp//ccYZP7w7.o
    send /var/tmp//ccYZP7w7.o
    gethostbyname /var/tmp//ccYZP7w7.o
    socket /var/tmp//ccYZP7w7.o
    connect /var/tmp//ccYZP7w7.o
    ld: fatal: symbol referencing errors. No output written to client
    collect2: ld returned 1 exit status

    ReplyDelete
    Replies
    1. This message is generated because gcc has encountered what looks like a function call, but there is no function with that name. As per the message the functions recv, send, gethostbyname, socket & connect are missing. As these functions are not user defined functions, it is not because of an error in the code. Check your header files, compiler and linker.
      Anyway the same code is working perfectly in my gcc 4.8 compiler.

      Delete
    2. compiling process

      Delete
  6. I tried to compile these program but it is not working properly so plz give me the command for compilation and execution of the given program with its output,and plz reply fast its very urgent..

    ReplyDelete
    Replies
    1. You can find the commands in the above comments and in the post description.

      Delete
  7. I Followed the steps to compile the client program i.e. gcc -o client client.c -lpthread, I'm getting some warning: incompatible implicit declaration of built-in-function 'memset' and same warming with server program i.e. @ 'strcpy & strlen'..
    Though i executed these two with './a.out' now i'm getting a message on my terminal window "Socket retrieve success"..plz help me to solve this error and execute the program successfully..
    Thank U in advance..

    ReplyDelete
    Replies
    1. After compiling with command gcc -o client client.c -lpthread, you've to execute it using command ./client, not ./a.out.

      Delete
  8. Hi Godly, when I wrote the client connection ./client 127.0.0.1 8000 the server print this message: ERROR READING FROM SOCKET: Success
    How can I fix it??? and how is the users.txt structure???

    Thanks

    ReplyDelete
    Replies
    1. Please do try with the updated code and report if the error is still there. The user.txt file just stores the user names and passwords in each lines.

      Delete
  9. hye Godly.Can this code modified to chat group?which no need to assigned user to send message,message will be sent to every user that logged in.

    ReplyDelete
  10. Is the ip address fixed to 127.0.0.1? How can I use other ip address? Thanks.

    ReplyDelete
    Replies
    1. You have to give the IP address of the server. If you are running the server program in your PC itself then you have to give this IP address (localhost).

      Delete
  11. how to write username in user.txt file and what would be the location of user.txt file

    ReplyDelete
    Replies
    1. Username would be asked when you create a user.
      user.txt will be in the folder where the client and server programs exist.

      Delete
  12. Nice one.TY - it really helps me

    ReplyDelete
  13. could you please explain how we are suppose to put the username in. I dont understand the format. Thanks

    ReplyDelete
  14. i am getting error called BINDING ERROR permission denied .please help me to sort it out

    ReplyDelete