Godly T.Alias - B.Tech CSE,
National Institute of Technology Calicut Follow me on GitHub
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
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
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...
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.... :)
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.
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???
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 ::
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
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.
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..
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..
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???
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).
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
ReplyDeletefirst 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.
Deleteusername is to be given in the format user-...
user.txt file stores the usernames of the users that are logged in...
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..
ReplyDeleteGyaneshwar Prasad... i am student of nitc and i have networks lab this sem ..plzz help....and reply asap.. thanxxxx.... :)
The struct 'u' holds the socket info all the users that are logged in. Just use a loop and send the message to all.
Deletefor(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
in server program I am typing : ./server 6000
ReplyDeletein 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???
username should be in the format user-
Deleteeg: 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.
message format is not correct error message whats the corect format i try with this user-1: hello
DeleteTo 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
ReplyDelete:: Thanx in advance ::
You can just copy-paste the code from my blog. No need of download link for that.
Deletewhat is your blog
Deletegcc -o client client1.c -lpthread
ReplyDeleteUndefined 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
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.
DeleteAnyway the same code is working perfectly in my gcc 4.8 compiler.
compiling process
DeleteI 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..
ReplyDeleteYou can find the commands in the above comments and in the post description.
DeleteI 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'..
ReplyDeleteThough 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..
After compiling with command gcc -o client client.c -lpthread, you've to execute it using command ./client, not ./a.out.
DeleteHi Godly, when I wrote the client connection ./client 127.0.0.1 8000 the server print this message: ERROR READING FROM SOCKET: Success
ReplyDeleteHow can I fix it??? and how is the users.txt structure???
Thanks
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.
Deletehye 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.
ReplyDeleteIs the ip address fixed to 127.0.0.1? How can I use other ip address? Thanks.
ReplyDeleteYou 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).
Deletehow to write username in user.txt file and what would be the location of user.txt file
ReplyDeleteUsername would be asked when you create a user.
Deleteuser.txt will be in the folder where the client and server programs exist.
Nice one.TY - it really helps me
ReplyDeletecould you please explain how we are suppose to put the username in. I dont understand the format. Thanks
ReplyDeletei am getting error called BINDING ERROR permission denied .please help me to sort it out
ReplyDelete