Archived

This topic is now archived and is closed to further replies.

Mirate

winsock issue with client/server app

Recommended Posts

hello. i may seem stupid for asking this but im willing to take the humiliation for an answer anyway heres the deal i have a working client/server app that allows for unlimited clients to connect to a nonblocking server and the clients can each send messages to server and they are recieved and displayed properly only thing is that i need the server to send replies back to clients depending on what the message is. for those of you who know this is a simple thing with winsock (for the most part) but the problem is that ive tried having the server send the message back using the same socket that the client needing the message is connected on and i get an error, the client is setup to run a thread for recieving that data by recv() function in winsock using the socket its connected to the server on. this doesnt work. so then i tried using the same way the server goes about determining what sockets have data ready to recieve with the select() function. even though there is only 1 socket in the FD_SET that the client is setting up i get an error with the select function. i dont know what to do except when a client connects on a socket to have the server connect to the client on another socket. is there anyway other than that (dont know if that will work either) to have say a socket with both send and recieve capabillities? Life is just one big game.

Share this post


Link to post
Share on other sites
hello, could you please break your paragraph up into smaller sentences and not use so many runon sentences cause im having a hardtime reading it cause its just one big long line of text with no real breaks and it hurts my head to read all that much less figure out what your saying when id really like to help you but dont wanna strain my eyes trying to pick apart your post so i can understand it to help you in the best way possible but like i said i cant read it very well cause its just all jumbled together hey im outta coke i should run to the store and gets some more cause i need cigarettes too and maybe get some gatoraid and candy bars oh right i was helping with your network problem i really wish i could help.

-=[ Megahertz ]=-

Share this post


Link to post
Share on other sites
Basically what Im saying is that I have a client/server app that works fine when it comes to the clients sending data to the server; however, when were talking about the server sending data back to the clients, this is where Im having issues.

My server is a non blocking server written in winsock, vc++6.
the client is a simple chat like program that sends text messages to the server depending on what the user types in.

The server then recieves the messages, interprets them, determines what needs sent back, then sends it.

my server is running 3 threads, 1 for accepting new clients, 1 for listening and recieving messages, and 1 for sending messages back to clients. then there is obviously the main loop for server side console commands but thats irrelevant.

i tried having the server send data on the same socket that the client is connected on, and having a thread on the client recv() on the other end using that socket. this generates a select error on the client side.

what i need help with is how to go about making this 2 way client/server relationship without having the server connect to the client on another socket. help?

Life is just one big game.

Share this post


Link to post
Share on other sites
Is this TCP or UDP. It can make some small differences in how you do things.

Also how you manage the clients can make a difference too.

In the case of TCP whenever you accept a new connection youre going to get a file descriptor back and that is the file descriptor you should use when sending back data to the client. Also any new incomming data will also come in on this file descriptor as the server only uses the listening socket for the initial connection.

In the case of UDP, the server listens for new connections but doesnt create a new file descriptor since theres no call to accept made. If you get data on the listening socket you have to see what the ip address and port the data came from and use that to deterimine if its a new client connecting or an existing client sending data.

Usually when you get a new client you create a new client object for that connection. I usually copy the file descriptor of the listening socket (in the case of UDP) to a member variable thats in the client object so that the client object can refrence that when sending new data out. I do the same for TCP except I copy the file descriptor I get back from accept.

Now where this differs with regard to TCP and/or UDP is that in the case of TCP, new incomming data comes in on the new socket where as with UDP incomming data comes in on the listening socket. Outgoing data will go out on the same socket in either case.

In any event, you shouldn't end up with 2 sockets for a client where one is for sending and one of for recieving.

Not sure if any of this helps, but I'd need a little more information on what protocol your using and what you're doing when new clients connect or new data comes in.

-=[ Megahertz ]=-

[edited by - Megahertz on April 6, 2004 2:40:42 PM]

Share this post


Link to post
Share on other sites
Some implementations of select() will un-block when you write on a selected socket from another thread.

In general, using the SAME socket from different threads has been error-prone and caused a lot of grief in a variety of different implementations. I don''t remember the details for WinSock, but in general, I would recommend against it.

Share this post


Link to post
Share on other sites
What im using is tcp/ip for the connections and i have a mutex handle that i use for the threads.

in the recieving thread i call WaitForSingleObject(mutexHandle); then copy all data i need from the global array of clients/data to a local variable then ReleaseMutex(mutexHandle);
that way there is no way the both threads can be either reading or writing to the data at the same time.

thanks megahertz that will help on server side but on the clientside im still confused. I have the main app running for getting user input and sending it to server, then i have another thread running for receiving data from server (using same mutex checking that i use in the server).

what i need to know for the clientside of things is that when i call connect() on the client and it returns a socket, is that the socket i use for recv() as well?
thanks for all the help.

Life is just one big game.

Share this post


Link to post
Share on other sites
"what i need to know for the clientside of things is that when i call connect() on the client and it returns a socket, is that the socket i use for recv() as well?"

Yes.


-=[ Megahertz ]=-

Share this post


Link to post
Share on other sites