Is there some trick to networking between LAN machines?
I want a client/server system on the network but I haven't a slightest about the IP.

I'd like to avoid dropped packets, so from my limited knowledge I'd go with TCP.
Etc.

How do I locate another machine via IP on a LAN? There are multiple machines on my LAN with the same external internet IP address.

Sorry for stupid questions but I have answered a lot of those in my day, so if you would kindly entertain my ignorance it would be much appreciated.

Recommended Answers

All 3 Replies

Each machine will have its own unique IP address *internal* to the LAN. You can use the internal IP to connect machines *on the same LAN* together.

If you are trying to get outside machines to connect to internal machines, you'll have to configure your router to use Port Forwarding, central service router (one machine gets all connections passes requests to machines that service the request and passes response back to client), or do a reverse connection (Machine outside LAN requests service, routing machine hands off request to actual machine that will service this client, service machine requests connection to client).

Trying to connect to my own machine:
Client (exception thrown, target machine actively refused it):

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace CS_LAN_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();
            IPEndPoint ep = new IPEndPoint( IPAddress.Loopback, 0);
            client.Connect(ep); // <-- Exception!

            byte[] buffer = new byte[2048];
            NetworkStream ss = client.GetStream();
            ss.Read(buffer, 0, buffer.Length);
            Console.WriteLine(buffer.ToString());//Doesn't work, I know.
            ss.Close();
            client.Close();
        }
    }
}

And server: (no problems afaik)

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace CS_LAN_Server
{
    class Program
    {

        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 0);
            server.Start();
            TcpClient client = server.AcceptTcpClient();
            
            NetworkStream ns = client.GetStream();
            byte[] message = Encoding.ASCII.GetBytes("Now is the winter of your discount tent!");
            ns.Write(message, 0, message.Length);

            client.Close();
            server.Stop();
        }
    }
}

Trying to connect to my own machine:
Client (exception thrown, target machine actively refused it):

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace CS_LAN_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();
            IPEndPoint ep = new IPEndPoint( IPAddress.Loopback, 0);
            client.Connect(ep); // <-- Exception!

            byte[] buffer = new byte[2048];
            NetworkStream ss = client.GetStream();
            ss.Read(buffer, 0, buffer.Length);
            Console.WriteLine(buffer.ToString());//Doesn't work, I know.
            ss.Close();
            client.Close();
        }
    }
}

And server: (no problems afaik)

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace CS_LAN_Server
{
    class Program
    {

        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Any, 0);
            server.Start();
            TcpClient client = server.AcceptTcpClient();
            
            NetworkStream ns = client.GetStream();
            byte[] message = Encoding.ASCII.GetBytes("Now is the winter of your discount tent!");
            ns.Write(message, 0, message.Length);

            client.Close();
            server.Stop();
        }
    }
}

You are using port number ZERO which is classified as a "well known" port number. "Well known" port numbers range from 0 (ZERO) to 1023 and cannot be used. You can use "registered ports" which range from 1024 thru 49151 provided that the port number you choose is not already in use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.