Building a simple chat application using asynchronous communication and UDP in C#

I’ve moved this post to my new blog.
http://fpgentil.github.io/2012/simple-chat-application-using-asynchronous-communication-and-udp-in-csharp/

84 comments

  1. Hi, what modifications do i need to add in order to make it work with multiple clients. I have created different application for server/client but how do i make the server to accept more connections?

    1. Hello Adi,

      Well, you can either use one different port to each client you have.
      Since UDP is conectionless, you do not have to worry about handshaking or other stuff like that, just “copy/paste” your clients and connect them to your sever.
      Let me know if this was helpful.

      Regards,
      Felipe

  2. Hey i m new in c# can you explain brefly how to check this code i want to build server client windows application what should i do can you bullet a procedure in proper flow

  3. This works great on a local network but I am wondering how it could be changed so that I can use across the internet?

    1. Hello Mike,

      Well I believe If you setup your external IP adress it should work as well, shouldn’t it?
      I haven´t tried yet, but the principles are the same!

      Regards,
      Felipe

      1. I have been trying for the last little while with no luck. If I put my external ip in the friend field and forward the ports on the router, i still get nothing. 😦

        You can send an email to me directly if you prefered to discuss what I am trying to do. (bergsma@gmail.com)

  4. Hey fpgentil You Have Done Awesome I have Checso ked Your Application I Already Built an c# application BY using Your tutorial

    Building a simple server client application using C#

    Now I want to Convert this application so that multiples clients can connect to single server

    and then onwards i want make application just like a yahoo server means i m making an environment over Lan same as yahoo chat

    means

    A single server
    and
    Multiples can connect to that server
    and Do chat with server
    means all persons who are connected to that server will receive messages which a single client send to server

    i hope u got my meaning these are my objectives

    simply now

    i want to convert this (which i have developed) application (Based on TCP protocol)
    so thats multiples clients can to single server

    1. Hello Raza,

      Do you mean the receiver get the sender port number?
      If so, all you have to do is, in your private void OperatorCallBack(IAsyncResult ar) you add those lines below

      IPEndPoint auxIP = (IPEndPoint) epRemote;
      string senderIP = auxIP.Address.ToString();

      Hope it helps!
      Regards,
      Felipe

  5. hi
    Im just working on three components client, cache and server

    Server will allow the user to browse and download file
    cache will cache the previous downloaded files by client
    Client will interact with server through cache

    Will the above method is similar as I am working?
    How to develop it in simple?

    1. Hello,

      Basically server and cliente will communicate by packages, so its similar.
      You can base your code in mine, implement cache, it should’n be that hard.

      Anything I can help, let me know 🙂
      Regards,
      Felipe

  6. Hello.
    i want to connect multiple clients to 1 server. The clients only send messages to server. How can i do this. How can i connect multiple clients to the server whitout knowing the ip adress of each client?

    1. Hello Sebastian,

      Well, i’ve never done anything like that, although I believe one possible solution is to implement a server with a list of sockets, each one will be connected to client, still you have only one server but multiple sockets. I don’t know if this is the best solution but I think it’s worth a try 🙂

      Regards,
      Felipe

  7. @fpgentil Nice work man ….You made my day ……I have just tried multiple clients …..working fine….. …..i have also completed file transfer with the help of code project and google…….Now i want to implement voice chat…..Do you have any idea about that…sry about my poor english:)

    1. Hello,

      Thank you so much for the feedback 🙂
      About the voice chat, I have never worked with anything like that, but I would love to help and understand more about it.
      If you’d like to discuss, you can add me on skype or facebook.. i could at least help you finding out the solution

      Regards,
      Felipe

      1. OK fine now i am on my way to build a voice chat app ….i think it will complete with in 2 days ….if i have any doubts i will contact u

  8. Hello i would like to know if it was possible that with the same principle as sending a text message like this i could send commands like adding stuff to a database or returning stuff from it.. and if it cant what would be the best way to do it.
    Anyways Thanks! Simple and easy to understand code!

    1. Hello Nuno,

      Well, I believe the principle is the same, so you could as well send commands within UDP packages.. The only thing that would matter in this case it would be how you handle this command when it arrives from network.

      For example, to add stuff to database, you could build your sql query and send it thru network, receive on the server side and execute it.

      I don’t know if it was clear enough, I think it depends on what kind of commands you want to use.

      Feel free to talk to me about it, i’d love to help 🙂

      Regards,
      Felipe

  9. Hi fpgentil,
    You do a great job. I want to develop a student management system for my former high school. I want to use visual c#.net and sql/mysql database server to store all the information. Client-server application is the way to go so that teachers and administrators can login to the system from their PCs and do their job. I will be contacting you on a few tips on the same.
    Thanks in advance.

  10. hi
    sir when we run the project only only one window show ,,,,and what is the port number …sir plz help me bz this is my semester project

  11. hello fpgentil
    can we make an application where messages from all the clients go through the server
    but they should be able to chat privately as well as an option to chat in group??
    if u have any code like this can u please mail it to me??

    1. Hello mayur,

      Actually I dont have any code like this.
      To chat in group the application needs to be redesigned, I haven’t thought about it yet but if you want to talk about it you can add me on Skype at gen7il.

      I’m sorry that took so long, I was on vacation.
      Regards,
      Felipe

  12. Hello!
    Why this is not working in my computer.
    It I click on Start button it is done. But when I click on Send button it shows me an error message which is given below:-

    System.Net.Sockets.SocketException(0x80004005): An existion connection was
    forcibly closed by the remote host
    at System.Net .Sockets.Socket.EndReceiveFrom(IAsyncResult asyncResult,
    EndPoint& endPoint)
    at AsyncComm.Form1.OperatorCallBack(IAsyncResult ar) in
    c:\Users\Hitesh\Desktop\AsyncComm\AsyncComm\AsyncComm\Form1.cs:line
    67

    Please help it out…

  13. hello dear thank u so much…..
    now can u tell me how we can send image and text both on client computer…
    any help will be appreciate

    1. Hello prabhat,

      To send an image all you have to do is convert that into a byte array and then the same to “reconstruct” it on the other side.
      Tell me if you have any problems with it.

      Regards,
      Felipe

  14. hey can u tell me …suppose that i have 5 computer and i want that every user can send data to each other…then i have to make 5 socket connection or not??? i am sending my code on ur mail id….plzzz give a look on dis and give me solution ..thanks

    1. Hello there,

      Well, if you have 5 computers then each one should be one socket. I’ll take a look on your code and answer you there.

      Regards,
      Felipe

      1. Hello sir , I really need help in this code , if you don’t mind could i have your email …

  15. hello fpgentil!
    i want to develop a “auto emoticon chat application” . its a simple chat app with a feature of automatically attachment of emoticon with the chat message by detection of user face expression.
    i had developed the face dedection and recognition part in “open cv ” and integrate it with c#.
    can you help me to make a chat app with above feature i.e automatically attachment of emoticon?

    1. Hello Asan,

      That sounds like an amazing app, I sure would like to help you. My email is cdigentil at gmail or if you prefer add me on Skype, my nickname is gen7il.

      Regards,
      Felipe

  16. hello!
    I have just copied and paste all of this in my visual studio, but i dont know how to work on it, an d when i click on start button, an exception for ip address comes out, and that is incorrect format for ip addresses written in text boxes, help needed, i need that urgently plz…..

    plz mail me the working template on mh.anjum90@gmail.com

    that would be so nice of you
    BEST REGARDS
    Anjum

  17. Hey Felipe, i’m trying to make a software to communicate with another computer, kinda the same way as the chat, but we need internet communication.
    so here’s the scenario:
    two computers, A and B, in different networks.
    they have IP A and B.
    And there’s a special problem here: both computers are in a network with more than one computer, hooked up with a router.
    For example computer A has a computer C in the same network, by a router, which means their external IP Address are the same.

    so the problem is: How do I make A and B communicate via UDP or TCP via internet (not local network)?

    – I have tried before:
    – UdpClient (class) says it’s listening to the IP and Port but never gets an answer
    – TcpClient says it’s not a valid IP in its context ( something like that)

    I ran out of ideas how to make it work, and even researching about it, they all show how to do it locally only.

    Can you help me out here ? 🙂

    Thank you

    1. Hello Lucas,

      Man that is clearly a nice problem 🙂
      I have never implemented anything like that before, would you mind adding me on Skype (gen7il) or Facebook (felipe.gentil) so we could talk about it?

      Cheers,
      Felipe

  18. Hello Sir ,you run app on same machine but if we have two different machines then how to get ip of the machine connected through lan cable i,e the other computer ip? reply early please

    1. Hello sunny,

      Well, i have tested this application using 2 apps running on the same computer, over the network and the internet, and they all worked well for me.

      You have only to pay attention on the IP that you are using and windows firewall.

      Regards,
      Felipe

  19. hello fpgentil
    i have tested this code on same computer its works fine but if i work on A LAN network its not working properly as it work on independent machine.

    bye the way its really a nice article . thanks a lot sir

    1. Hello sir,

      Are you getting any error while testing this code on LAN? I’m pretty sure I’ve tested once and it worked for me.

      Thanks for the commend and feedback 🙂

      Regards,
      Felipe

  20. Hi I get this error:

    A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call)
    no address was supplied

    the error comes from here:

    System.Text.ASCIIEncoding enc =
    new System.Text.ASCIIEncoding();
    byte[] msg = new byte[1464];
    msg = enc.GetBytes(txtChat.Text);
    sckCommunication.Send(msg);
    lbChat.Items.Add(UserInfo.UserName + txtChat.Text);
    txtChat.Clear();

  21. hi fpgentil
    i want to make this for multi clients user, how to do this?
    is there is tutorial you recommend to me to do this!

  22. Hello fpgentil,

    What if i want multiple clients on one server ? They´re all gonna use different ports ? Sorry if it´s already been ansewred but i do not had the time to read all answers.

      1. Hi again fpgentil,

        are you brazilian ? Can we communicate through emails ? Because I have an error using your code but the error is in portuguese.

        Thanks in advance.

        Narciso.

Leave a reply to Rush Cancel reply