Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have managed to create a chat program where two clients can send and receive messages from each other. I am trying to improve it so that instead of the clients sending it to each other, the message is first sent to a server and then the server sends it to the client.

Any Ideas on how to create the server?

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-14 17:33pm    
Any special reason to use UDP, which does not guaranteed delivery in correct order? Why not TCP?
—SA
WA_coder 14-Mar-14 5:35am    
I'm quite new to networking and UDP seemed like the simpler version.
Sergey Alexandrovich Kryukov 14-Mar-14 9:25am    
It is not simpler. You have no sessions, connections, disconnections, but, at the same time, no guarantee of delivery and its order. To enable organized sequential chat, you well have to add your own code to guarantee all that, it it will become more complex then TCP. That's why I strongly recommend to use TCP.
—SA

I wrote one of these in VB6 long ago. It was fun. :)

The server needs to bind to the udp port that your clients send on. And then your server needs to send on the port that your clients are listening to.

You'll want to have some sort of protocol that you implement so that your server can see what the message is and who it is for. I don't know of any built in ways to do that so you can just structure your messages so that the first part is who it is from, then who it is to, etc, etc.

Then your server will parse the message and be able to determine what to do with it. It will see who it is going to and send it on. This can get very complicated very quickly because you might want to have a message that the client sends back acknowledging they received the message. There are all kinds of things you can do.

It may be likely that there is a library for .Net you can download to facilitate some of it so you don't have to do it all by hand but I am not familiar with any. I would recommend searching google to see if there are any pre built solutions or at least some pieces to make it easier.
 
Share this answer
 
Comments
WA_coder 13-Mar-14 15:03pm    
I understand the what you are saying :) But could you point me in the right direction about the code needed.
Does the server code need to be exactly the same as the client or is it completely different?

Thanks
ZurdoDev 13-Mar-14 15:05pm    
It depends on you. The code will be very much the same, meaning it will also have to bind to ports and be listening. But where it is different is it will parse the message to see what type of message it is. Your clients will have to be changed to, to construct the messages for the server.
Please see my comment to the question and rethink you idea to use UDP. Overall, it may appear much more difficult than TCP. You can use networking on several different levels, starting from TCP sockets. Please see my short overviews of them:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

For some other ideas (not directly related to the chat, but useful to think at), please take a look at my past answer: an amateur question in socket programming[^].

—SA
 
Share this answer
 

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