Snake
Snake

Reputation: 3

how to use my C# messenger over internet?

i've written a simple local messenger with c# for use of myself. now it works over a local network(lan,wifi) and works fine. now i want to give it to my friend and use it over internet but have no idea how to use different ip except local host.

i'd be grateful for any help. thanks in advance.


    private void InitializeConnection()
    {

        ipAddr = IPAddress.Parse(txtIp.Text);
        tcpServer = new TcpClient();
        tcpServer.Connect(ipAddr, 1986);

        Connected = true;
        UserName = txtUser.Text;

        txtIp.Enabled = false;
        txtUser.Enabled = false;
        txtMessage.Enabled = true;
        btnSend.Enabled = true;
        btnConnect.Text = "Disconnect";

        swSender = new StreamWriter(tcpServer.GetStream());
        swSender.WriteLine(txtUser.Text);
        swSender.Flush();

        thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
        thrMessaging.Start();
    }

this the client part

    public ChatServer(IPAddress address)
    {
        ipAddress = address;
    }

    private Thread thrListener;

    private TcpListener tlsClient;

    bool ServRunning = false;

    public static void AddUser(TcpClient tcpUser, string strUsername)
    {
        ChatServer.htUsers.Add(strUsername, tcpUser);
        ChatServer.htConnections.Add(tcpUser, strUsername);

        SendAdminMessage(htConnections[tcpUser] + " has joined us");
    }

and this server part.

Upvotes: 0

Views: 3259

Answers (1)

Josh Bowden
Josh Bowden

Reputation: 5990

Intro

To be able to get another user to connect to a computer of yours, there are a quite a few things you are going to have to do. Hopefully, this should work but networking can get complex in general and not all networks run alike. I assume you are running this behind some sort of router that you have access to. Networking is complex and that really is why this answer is so awfully long (if you are questioning yourself on reading the rest of it). For the most part it is pretty straight and forward, but might take some time.

And before you get started, just a:

Forewarning

Thinking security wise, like almost everything on computers and with networking in general, there is not really something called "entirely safe" (at least as far as I know). As with networking, opening ports is not entirely safe. I'm not a top notch security expert, but by doing this, you are allowing other computers to send and receive data with your computer. This should be safe in your case of just having a simple text chat, but in other cases this might not be the same. With more complex and important cases such as dealing with SSH and FTP, there is more security involved. Just as a warning, make sure to take care when messing with your network, or with computers in general.

Getting Started

Anyways, (if I have not scared you off yet) these are the steps that I had to take to get something like your chat server working:


Forwarding the port on your router

The first thing you are going to have to do is forward or open the port on your router. Well, before you can even do this, there are several things you need to understand about networking:

Basically, your router is what connects your network of computers to the internet and allows connections to be be made through ports. If you don't understand what a port is, it is what is used for internet connections to be made and basically acts like the house number to a street address. It tells where on that street the house it and is more specific than just the street address. In terms of networking, this is a number which tells the specific place the communication is going. The port number ("the house number") is telling where on the computer's IP address ("the street address") to connect.

Specifically, the port number is an 16-bit unsigned integer ranging from 0 to 65535 (but port number 0 actually cannot be used according to Wikipedia) Even though this is a 16-bit unsigned integer which would be a ushort or a UInt16 in C#, when using a TcpClient or a TcpListener, they instead use a signed 32-bit integer instead which is the standard int or a Int32. Also, The port is generally denoted after an IP address with a colon (":"), for example 123.45.67.89:80 but might be different in other cases. This is using IPv4 but there is also IPv6 which I have not yet worked with.

Now, what about forwarding the port and why do you need to do that? What forwarding the port does is forward connections to a certain port on the router to a computer which is behind the router instead. There is not always a simple, straight and forward way to do this since router companies have different ways of accomplishing this. Generally, to figure this out, you can Google for instructions on how to forward the port on your particular router, so for example you can search for "forward port on router company's name router" to find it. To do this, you are probably also going to need a few things before getting started with that:

  • You are going to need your routers admin user name and password which is generally not (and probably should not be) what is used to connect to it with. If you don't know what it is, whoever setup your router should know. Once you have got this working you are ready to move on.
  • You are also probably going to need the your computer's local IP address which is used to address the computers in the router's network. To find this, your router will probably tell you what it is, but here are the steps to do it on Windows if you can't seem to find it.
    1. Open up command prompt
      • Open the run dialog by going to Start >> Run... or by pressing Win+R
      • Type cmd and press enter
    2. Run ipconfig by typing it in and pressing enter to find your computers IP address
      • You should see a list of network interface connections
      • To find the one we are looking for, you want to find the network connection you are using to connect to the internet through your router. It is probably different on computer but in my case its named Wireless LAN adapter Wireless Network Connection since I'm using a laptop. The name, I would assume, probably has LAN in it.
      • This should not have: Media State . . . . . . . . . . . : Media disconnected listed under it since you need to be connected to your router. If you are not then simply do so.
      • Instead, there should be IPv4 Address. . . . . . . . . . . : with the IP address of your actual computer listed next to it. This is the IP address of the computer whose port you want to forward. Of course, this might be different if you are using a router using IPv6.

Also, in the process of doing this, your port number that were using may be taken by another process or service. This is okay and easy to fix; you just have to change the port number. You are going to want to trying a high number such as 8500 for example until you find one that works. Once you have that number, just update the port number in your code. To make this even easier it, is much easier to have a constant that stores this such as:

const int Port = 8500;

So then you can have the client connect with:

tcpClient.Connect(ipAddress, Port)

And then have the server listen with

TcpListener tcpListener =  new TcpListener(ipAddress, Port);   

As with other global variables such as the port number that you might have, it may be better to create a global variable class, but at the same time, that may also introduce other issues by using it such as with global properties with threading. Just a suggestion and a heads up.

Though, once you have managed to complete forwarding the port, you can move on to the much simpler part of getting your friend to actually connect to your server.


Geting your router's IP address

Once you have managed to do all of that, you probably should give your a pat on the back because that is the real actual part of getting your router to cooperate with what you were trying to do. Now you just have you give your IP address, port number, and of course your client program to your friend, so he or she can connect to the server. To get your router's public IP address you can go over to the ironically called website at http://www.whatismyip.com/. Here you can get your public IP address but also make sure that it says "No Proxy Detected" below it. If you are using a proxy, hopefully you know what that is and can connect to the internet without to get your IP address. (Trust me, you don't want me to explain want a proxy is for now)

Now once you have got that, you are going to want to send that IP and also your port number to your friend. To be smart about it, you don't want this publicly displayed as you probably don't want a bunch of random people trying to break into your network. Sending it over IM or email should (hopefully) be fine as long as your friend does not publicly display it. So when sending your IP address, just be smart about it.

Once you have done that, just don't forget to actually start your server! Without your server running, there is no way to communicate to the clients and accept incoming connections. This will save a lot of frustration from accidentally forgetting to.


Conclusion

Hopefully, this will help you be able to create a cool chat program and also understand a few other things in the process. I really wish this was not so long, but networking is really just complicated in for the most part.

Hope this helps you quite a bit!

Upvotes: 3

Related Questions