Click here to Skip to main content
15,662,823 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:
Person p = new Person("Tyler", "Durden", 30); // create my serializable object
string serverIp = "";

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


Server side:
TcpListener server = new TcpListener(9050);
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);


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.

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[^].

Share this answer
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?
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.
Kieran Crown 4-May-14 15:37pm

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