Jason
Jason

Reputation: 233

C# TCP connecting to a computer

After some research I've almost managed to get a program to connect to another PC using TCP.

I've made 2 programs :

  1. one to connect and

  2. one to receive which goes on the other computer if that makes sense.

I'm not sure if I use my public IP address to connect but it doesn't work.

I'm not sure if its the program or the wrong IP.

So here is the code for the program that connects.

    public static bool IsConnected;
    public static NetworkStream Writer;

    static void Main(string[] args)
    {
        Console.Title = "Offline";
        TcpClient Connector = new TcpClient();

    GetConnection:

        Console.WriteLine("Enter server IP :");
        string IP = Console.ReadLine();

        try
        {
            Connector.Connect(IP, 2001);
            IsConnected = true;
            Console.Title = "Online";
            Writer = Connector.GetStream();
        }
        catch
        {
            Console.WriteLine("Error connecting to target server! Press any key to try again.");
            Console.ReadKey();
            Console.Clear();
            goto GetConnection;
        }

Its a console application where I just type in the ip address and it tells me if its connected or not,

It uses port 2001 just like the receiver which the code for that is below.

    public static NetworkStream Receiver;
    [DllImport("kernel32.dll")]

    static void Main(string[] args)
    {
        FreeConsole();

        TcpListener l = new TcpListener (2001);

        l.Start();
        TcpClient Connection = l.AcceptTcpClient();
        Receiver = Connection.GetStream();
    }

If anyone has any ideas as to why it doesn't connect its appriciated

Upvotes: 0

Views: 92

Answers (1)

Jason
Jason

Reputation: 233

I was using the public ip address instead of the ipv4 address

Upvotes: 1

Related Questions