CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    UDP Chat application

    Hi, I am building small windows chat application. It consists of two part, UDP server and client and it works well while using my local IP (127.0.0.1) but nothing happens when i try to go over my router. I would appriciate any help, either to help with my code or give me some advice how to track what is going on when i start application. Now it seems like on login packets do not come to server but i am not sure. My source is big so i will place here what i think could be right, maybe it's not it .

    This go on client load:

    private void Form1_Load(object sender, EventArgs e)
    {
    CheckForIllegalCrossThreadCalls = false;

    this.Text = "SGSclient: " + strIme;

    //The user has logged into the system so we now request the server to send
    //the names of all users who are in the chat room
    Data msgZaSlanje = new Data ();
    msgZaSlanje.cmdCommand = Command.Lista;
    msgZaSlanje.strIme = strIme;
    msgZaSlanje.strPoruka = null;

    bytePodaci = msgZaSlanje.ToByte();

    klijentSocket.BeginSendTo(bytePodaci, 0, bytePodaci.Length, SocketFlags.None, epServer,
    new AsyncCallback(OnSlanje), null);

    bytePodaci = new byte[1024];
    //Start listening to the data asynchronously
    klijentSocket.BeginReceiveFrom (bytePodaci,
    0, bytePodaci.Length,
    SocketFlags.None,
    ref epServer,
    new AsyncCallback(OnPrimanje),
    null);
    }

    and then my UDP server should respond as it receives message but nothing happens.

    This is my code for UDP server inicialization:

    private void Form1_Load(object sender, EventArgs e)
    {
    try
    {
    CheckForIllegalCrossThreadCalls = false;

    //---UDP socket---
    serverSocket = new Socket(AddressFamily.InterNetwork,
    SocketType.Dgram, ProtocolType.Udp);

    //---Uzima bilo koji IP i port broj 1000---
    IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("78.0.69.84"), 1000);

    //---Spajanje sa mrežnim sučeljem---
    serverSocket.Bind(ipEndPoint);

    IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);

    EndPoint epSender = (EndPoint) ipeSender;

    //---Počinje primati podatke---
    serverSocket.BeginReceiveFrom (bytePrimljeniPodaci, 0, bytePrimljeniPodaci.Length,
    SocketFlags.None, ref epSender, new AsyncCallback(Primanje), epSender);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "UDP Server",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }


    Thanks if you can help.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: UDP Chat application

    Sounds like your router is not also a firewall/gateway. You'll have to refer to your router docs on how to forward ports across the gateway.

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Re: UDP Chat application

    I'm checking that, i disabled my firewall but still nothing.

    I forgot to say that I have dynamic IP address. But before testing the application i check it and it stays the same for long enough period of time. Also, I found many questions like mine on the internet but can't find single answer. I need this issues to be fixed somehow programaticly since i will have to present my work inside other local network with unknown security policies.

    When I use Dns class in .net it gets me my local IP, how can i discover router's WAN interface address?
    //Edit//
    OK, last is done like this:

    string WanIP = new System.Net.WebClient().DownloadString(("http://www.whatismyip.com/automation/n09230945.asp"));
    txtLog.Text = WanIP;

    Still, when i get that ip and send data with client, nothing happens.
    Last edited by perisa10; April 9th, 2010 at 04:49 PM.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: UDP Chat application

    perisa10 (Periša?), I was once pointed out by someone that many home routers don't allow for connections from your own host (inside to inside), and that a connection from the "internet side" of the router should work. So, try to connect from a friend's computer and see what happens.

    As for the dynamic IP, take look at the freedns.afraid.org. They offer "5 free shared hostnames, use anywhere", and
    "… you are welcome to signup for your free account and create up to 5 subdomains off of the domains others have contributed and point these hosts anywhere on the Internet." Basically, you can have a subdomain name that resolves to your home computer, or to your router's external IP, if you use port forwarding.

    Also check out their FAQ page. This entry might interest you: Is there a program/script for updating my IP in FreeDNS?. I tried an app called "FreeDNS Update" and it works fine.

    As for port forwarding, you have to setup your router to forward any traffic that arrives on an external port (or on any of several ports) to an internal port that your app uses. There are various guides on this site, they are designed for specific routers and specific applications that might require port forwarding, but if you can find one that's general enough, the principle is the same.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: UDP Chat application

    You can also read here http://slacksite.com/other/ftp.html about the issues connecting thru a router/firewall. It's about ftp connections but your application would have the same issues.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Apr 2010
    Posts
    3

    Re: UDP Chat application

    @TheGreatCthulhu : I completely ignored the fact about inside-to-inside connection and checked in my router (speedtouch 780) about option called loopback. I enabled it's value but i am not sure on which interface so i telnet in my router but couldn't set it and went crazy about it. Have to figure out something about it since it is good to know for the future.

    I did port forwarding but would like to know one thing. I can set it on server side but do I have to do it for every specific client (behind different routers) ? And can it be done in code since it is not practical to force users to do it by themselves? I read something about Upnp code and found some C# function but didn't test it yet. What is the usual way to deal with these things, how for example commercial messengers solved it?

    @SMA: thanks, i am reading it, it is useful.

    Yes, I am Periša. Have we met?

  7. #7
    Join Date
    Jan 2010
    Posts
    1,133

    Re: UDP Chat application

    Quote Originally Posted by perisa10 View Post
    @TheGreatCthulhu : I completely ignored the fact about inside-to-inside connection and checked in my router (speedtouch 780) about option called loopback. I enabled it's value but i am not sure on which interface so i telnet in my router but couldn't set it and went crazy about it. Have to figure out something about it since it is good to know for the future.

    I did port forwarding but would like to know one thing. I can set it on server side but do I have to do it for every specific client (behind different routers) ? And can it be done in code since it is not practical to force users to do it by themselves? I read something about Upnp code and found some C# function but didn't test it yet. What is the usual way to deal with these things, how for example commercial messengers solved it?
    As for port forwarding, as far as I know, you need to set it up only on the router side; the outside clients then use the routers IP and the (usually same) forwarded port to access the server on the host. If the forwarding works correctly, the server application will receive a socket for each of the connected clients, so that it can communicate with them via those sockets.
    I'm not sure if this will work with the internal (local company's) network - but you can configure internal client apps to use local IP.
    How exactly your apps should interact depends on the protocol that you use (whereby you can use an existing standard, or you can define your own). The server and client should do some sort of virtual "handshake", an exchange of simple descriptive data that the server can use to identify if the client appears to be compatible. Also, some security measures will probably be required. Once the potential client is validated by the server, the communication can continue (but you can only hope that the agreed protocol will be followed, since nothing enforces it).

    I don't have much experience with this, so I can't tell you how it's usually done at the pro level, but I suppose if there's potentially a huge number of simultaneous clients involved, the server application implores some sort of client management system - a specialized class or a group of interacting classes used to realize this logic.

    On the web, I've seen articles suggesting that the server should start a new thread for each client, but this is (IMO) not really a good design, especially if there's a huge amount of clients. So, based on the needs of a specific application, it should be probably estimated what processes/operations need to be done concurrently, and what can be done serially, and create some sort of a balanced solution.

    Quote Originally Posted by perisa10 View Post
    Yes, I am Periša. Have we met?
    No, I don't think so - I just read your name and thought that we both might come from the same general area of former SFRJ, so I wanted to check - though if I read your code in the first post more thoroughly I could have concluded this myself... I didn't see many CodeGuru members working in, or originating from these parts, but I could wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured