Mircea Mihai
Mircea Mihai

Reputation: 167

Problems with C# internet connection

i'm trying to make an application for my laptop which in case i forgot to log off, i can use my smarthphone to log off by using a specific app for that. So i was thinking usually if you have a router... you have a problem cause you don't have the external ip which you can use, and the port. For that i used this function to get the external ip.

        public string adresaIP()
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        WebClient clientWeb = new WebClient();
        String adresaIP = utf8.GetString(clientWeb.DownloadData("http://bot.whatismyipaddress.com"));
        return adresaIP;
    }

But when i try to use te IpEndPoint it dosen't work it give's me an exception Error and i don't know were i did wrong.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace bluetooth_LogOff
{
public partial class Form1 : Form
{
    static byte[] buffer { get; set; }
    static Socket soket;


    public Form1()
    {
        InitializeComponent();
        try
        {
            string ip = adresaIP();

            soket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //soket.Bind(new IPEndPoint(IPAddress.Parse(ip),1234)); <<-- in this way dosen't work 

            soket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"),1234)); // <<- in this way it works....
            soket.Listen(100);


            Socket accept = soket.Accept();
            buffer = new byte[accept.SendBufferSize];
            int bytesRead = accept.Receive(buffer);
            byte[] format = new byte[bytesRead];
            for (int i = 0; i < bytesRead; i++)
            {
                format[i] = buffer[i];
            }
            string primescMesaj = Encoding.ASCII.GetString(format);
            MessageBox.Show(primescMesaj);
            soket.Close();
            accept.Close();



        }
        catch (Exception messaj)
        {
            MessageBox.Show(messaj.ToString());

        }


    }

    private void button1_Click(object sender, EventArgs e)
    {

        label1.Text = adresaIP();





    }
    public string adresaIP()
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        WebClient clientWeb = new WebClient();
        String adresaIP =    `utf8.GetString(clientWeb.DownloadData("http://bot.whatismyipaddress.com"));`
        return adresaIP;
    }
}

}

But the funny thing is if i put the addres like "127.0.0.1" It works, but if i put the string addres it dosen't

Upvotes: 1

Views: 339

Answers (1)

Eli Algranti
Eli Algranti

Reputation: 9007

You cannot bind to the external address that address belongs to the router.

You should bind to address 0.0.0.0 (all addresses) on your laptop and configure your router to forward the laptop port (or use UPnP).

The reason you cannot access your laptop directly is because your router, like most routers is a NAT (network address translation) router. It allows several computers to hide behind a single IP address. So the router will have a public IP address and your laptop and other devices behind the router will have a private IP address (such as those in the range 192.168.x.x)

Most NAT routers can be configured with static port forwarding; i.e. the port in a specific private address is reflected in the same or a different port in the public IP. This allows access to internal devices from the public internet. UPnP is a protocol that does the same thing, but does not require manual configuration on the router. UPnP is usually how P2P applications and some multi-player games gain public accessible ports without human intervention. It is also why UPnP may be considered a security hazard since the computer owner may not be aware of such forwarding.

Upvotes: 3

Related Questions