Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.89/5 (3 votes)
See more:
Okay. I've tried to ask this question before but don't think I've worded it correctly. I currently have a TCP server that can accept an unlimited amount of clients and send bytes of data (string). I now want to send custom objects using a method such as Serialization. How do I write a server that will work with my client side code (below) and accept multiple connections? I also need the server to send the object to all other clients once an object has been received so I will need to keep the connection open on client side.

Here is the code I've got for the Serialization process:

Client side:
C#
Person p = new Person("Tyler", "Durden", 30); // create my serializable object
string serverIp = "127.0.0.1";

TcpClient client = new TcpClient(serverIp, 9050); // have my connection established with a Tcp Server

IFormatter formatter = new BinaryFormatter(); // the formatter that will serialize my object on my stream

NetworkStream strm = client.GetStream(); // the stream
formatter.Serialize(strm, p); // the serialization process

strm.Close();
client.Close();


Server side:
C#
TcpListener server = new TcpListener(9050);
server.Start();
Console.WriteLine("Server ready");
TcpClient client = server.AcceptTcpClient();
NetworkStream strm = client.GetStream();
IFormatter formatter = new BinaryFormatter();
Person p = (Person)formatter.Deserialize(strm); // you have to cast the deserialized object
Console.WriteLine("Hi, I'm " + p.FirstName);

strm.Close();
client.Close();


So what I need to know is how do I make the code above, accept multiple connections, keep them open and then send any received objects to all connected clients. Any help would be appreciated.
Posted

1 solution

Basically, you need a least two separate threads in your service: one accepting new connection, another one writing/reading to/from network stream for communication with all the already connected clients according to your application-layer protocol. Please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^].

—SA
 
Share this answer
 
Comments
Kieran Crown 4-May-14 13:41pm    
Is there a working example of these?
Sergey Alexandrovich Kryukov 4-May-14 14:40pm    
I don't have anything which could be quickly adopted to showing here at this moment, but it's not too hard to write. Why wouldn't you try by yourself, starting with the simplest prototype?
—SA
Kieran Crown 4-May-14 14:41pm    
I will :)
Sergey Alexandrovich Kryukov 4-May-14 14:57pm    
And if you face some problems, feel free to ask follow-up questions (if you want to ask a separate question, please leave me a comment with URL by replying to this comment). After all, don't forget to accept this answer formally (green "Accept" button), because this approach really works.
—SA
Kieran Crown 4-May-14 15:37pm    
http://www.codeproject.com/Questions/768621/How-to-send-object-back-out-to-all-clients

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900