Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous multi-direction server-client communication over the same open socket?

Tags:

c++

c#

sockets

I have a client-server app where the client is on a Windows Mobile 6 device, written in C++ and the server is on full Windows and written in C#.

Originally, I only needed it to send messages from the client to the server, with the server only ever sending back an acknowledgement that it received the message. Now, I would like to update it so that the server can actually send a message to the client to request data. As I currently have it set up so the client is only in receive mode after it sends data to the server, this doesn't allow for the server to send a request at any time. I would have to wait for client data. My first thought would be to create another thread on the client with a separate open socket, listening for server requests...just like the server already has in respect the client. Is there a way, within the same thread and using the same socket, to all the server to send requests at any time?

Can you use something to the effect of WaitForMultipleObjects() and pass it a receive buffer and an event that tells it there is data to be sent?

like image 320
Adam Haile Avatar asked Aug 04 '08 13:08

Adam Haile


People also ask

Is socket programming asynchronous?

Asynchronous sockets increase the performance and scalability of your client-server applications. A socket is defined as the end point of a two way communication between two processes running over a network. Inter-process communication can be achieved using sockets.

Is socket two way communication?

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.

Can a socket be both server and client?

You can use the same socket for whatever you want, as long as your protocol handles it.


1 Answers

When I needed to write an application with a client-server model where the clients could leave and enter whenever they want, (I assume that's also the case for your application as you use mobile devices) I made sure that the clients send an online message to the server, indicating they were connected and ready to do whatever they needed doing.

at that time the server could send messages back to the client trough the same open connection.

Also, but I don't know if that is applicable for you, I had some sort of heartbeat the clients sent to the server, letting it know it was still online. That way the server knows when a client was forcibly disconnected from the network and it could mark that client back as offline.

like image 68
sven Avatar answered Sep 24 '22 10:09

sven