![]() |
|
#1
|
|||
|
|||
server/client.... multiple clientsok, I'm supposed to make a server/client program that accepts
connections from clients, but allows them to chat to each other... now in java, all you have to do is just send a message to the server, and the server just echos that messages to all the clients... but in C, once you call the accept message, the thread pauses, and waits for a connection... what i need to know, is how to create another thread in C to be able to handle message I/O from client to client.. Thanks for the help.. Justin here is the code.. CPP / C++ / C Code:
CPP / C++ / C Code:
I dont know if I should use the prof's defined "readln" and "await_contact" or are there "predefined" methods in C like socket, or connect, listen,bind.. ???? Last edited by LuciWiz : 20-Oct-2006 at 12:18.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: server/client.... multiple clientsThe predefined functions given by your prof maps to functions in C socket library.
await_contact -> collection of socket()+bind()+listen()+accept() calls accpets returns a socket descriptor for the new client connection received. await_contact() is a blocking function because of accept() call. It blocks until a client connection arrives after which it returns with the descriptor for the new connection (different from the one on which it is listening). In you case it is stored in variable "conn" which is further used for sending (send ->send()) and receiving (recvln() -> recv()) . Now to handle multiple client connections simultaneously, you have 3 possible ways to do it. 1. Start a new thread everytime await_contact returns and pass the new socket descriptor value ('conn') to that thread so that it can work with it independently without stopping the main thread which continues back up to await_contact() and listen for new connections from client. 2. Same as option 1 except fork() a new process instead of a thread. Just remember, creating a new process for each new client can be more resource expensive for the OS than creating a thread. 3. Use select() function call which provides a non-blocking way of handling multiple client connections without using thread per client or process per client approach. It stores all socket descriptors (server+ multiple 'conn's) in a descriptor set and samples (or polls) them regularly to see if any of them is ready for reading or writing i.e. recv() or accept() (for server descriptor) or send(). It then uses that socket descriptor and do the necessory I/O on the socket and when finished goes back to select() for polling the socket again. If you google for select() or Beej's Guide to Network Programming, you will find good reference material. Hope this helps. Thanks. |
|
#3
|
|||
|
|||
Re: server/client.... multiple clientsok, so i get the idea,
and i know to use.. pthread.h ... but the parameters are very very confusing to me... is there anyway someone could break down the params for me... so each time the await_contact returns, i would call... "ret = pthread_create();" somethin along those lines.. but how would i go about passing the connection from the await_contact to the newly created thread? just have a method that adds to the current thread, like void sendToThread(connection conn){ //create socket with param conn? //then listen? } Justin... |
|
#4
|
|||
|
|||
Re: server/client.... multiple clientsok, here is my revised server side application...
CPP / C++ / C Code:
ok now, Im getting errors at argument 3 and 4 of pthread_create... i dont think im fully understanding the parameters... i thought 3 was the param of the method you wanted to execute in the thread... and 4 was the parameter for the method passed in param 3... am I right? thanks, Justin Last edited by LuciWiz : 21-Oct-2006 at 11:57.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#5
|
|||
|
|||
Re: server/client.... multiple clientspls post the errors.
The method you want to excuting in the thread is prototyped as void * func_name (void *arg) Have you done it.? |
|
#6
|
|||
|
|||
Re: server/client.... multiple clientsok I got the thing to compile...
but after the client connects, im getting a segmentation fault...i have no idea what that is.. here is the code for the server/client.. oh, I am also getting a warning: parameter 4 of 'pthread_create' makes pointer from integer without cast? what does that mean? CPP / C++ / C Code:
CPP / C++ / C Code:
so in all there are two problems.. the 4th param in 'pthread_create': making a pointer from integer without cast. and segmentation fault in the chatserver application.. i think it has something to do with the: Code:
Last edited by LuciWiz : 24-Oct-2006 at 01:10.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#7
|
|||
|
|||
Re: server/client.... multiple clientsIs "connection" typedefed has "Integer"? Atleast thats what it looks like from your "sendStuff" function.
You are not initializing "conn" anywhere before calling CPP / C++ / C Code:
How do you know for sure that "conn" is actually greater than 0. If you want to simply run continuously, just do CPP / C++ / C Code:
Now to your thread part of the code. This is what pthread_create looks like CPP / C++ / C Code:
and this is what you have CPP / C++ / C Code:
The last argument should be a pointer. Now since your last variable "conn" is an integer, you need to cast it as void pointer when passing it to pthread_create as below. CPP / C++ / C Code:
The best way to troubleshoot is to use "printf" statements inside your code to see whats going on with the values you are passing down the functions. For. e.g. Before calling pthread_create() you could printout the connection value as CPP / C++ / C Code:
Also, you can capture and print the socket id inside the thread function "sendStuff". CPP / C++ / C Code:
This will help you make sure, that what you are passing to pthread_create is what actually being used by "sendStuff" to send and receive. Try this and see what happens. [/c] |
|
#8
|
|||
|
|||
Re: server/client.... multiple clientsk, will do...
yes, conn is an integer... i looking into await contact, and at the end of the function, it returns accept, which returns int. thx for the (void*)&conn I would've never figured that out lol... ill post when I modify... thanks again, Justin |
|
#9
|
|||
|
|||
Re: server/client.... multiple clientsok, I changed it, and it compiled and works!
the server side that is... now the client wont connect, i put a printf("EXIT STATUS"); in the CPP / C++ / C Code:
umm, I dont see why it wouldn't connect, the await_contact on the server side is still in the main function... oh, I also added a CPP / C++ / C Code:
in the chatserver.c so see if it even go into the while(1){} loop Justin CPP / C++ / C Code:
CPP / C++ / C Code:
Last edited by LuciWiz : 24-Oct-2006 at 01:12.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#10
|
|||
|
|||
Re: server/client.... multiple clientsWhat port is server listening on.? By appnum i think you mean the "port number"
Is this a Windows machien or a unix machine that you are working on? Each type of OS gives different APIs to print the reason for disconnection. For.e.g Unix has perror() while Windows has WSAGetLastError (I think!! I work on Unix mostly). I suggest you read this before going forward. It gives good info on Network Programming. Beej's Guide to Network Programming Using Internet Sockets Thanks, |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multiple Domain Hosting – the best way to save money!!! | luvila | Web Hosting Advertisements & Offers | 0 | 07-Aug-2006 04:03 |
| Multiple Clients Trouble | Golmal | C Programming Language | 1 | 16-May-2006 02:43 |
| Multiple Domain Hosting – save your money!!! | luvila | Web Hosting Advertisements & Offers | 0 | 30-Jan-2006 07:32 |
| Linker errors with multiple file progam | nkhambal | C Programming Language | 2 | 24-Apr-2005 02:37 |
| strange sizeof(structure) - multiple of 8 | pinkpanther | C Programming Language | 11 | 30-May-2004 07:20 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The