1

I seem to be having a hard time figuring out how to get my client app to automatically connect to a server app running on a separate machine on my LAN.

Right now the only way I'm able to get the client to connect to the server is by manually specifying the server's IP address in code:

 private TcpClient client = new TcpClient();
 private IPEndPoint serverEndPoint = neIPEndPoint(IPAddress.Parse("Server IP address goes here"), 8888);

My server app uses a TCP Listener, so I figured my client could do something similar, to be able to find the server, but I can't figure out how to implement it in code.

Code from my server app for finding the client to connect to:

private TcpListener tcpListener;
    private Thread listenThread;
    private int connectedClients = 0;
    private delegate void WriteMessageDelegate(string msg);

    public Form1()
    {
        InitializeComponent();
        Server();
    }

    private void Server()
    {
        this.tcpListener = new TcpListener(IPAddress.Any, 8888); 
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

I've tried using a TextBox that the user can manually enter their server's IP address into (since they wouldn't have access to the code), but I think an automatic connection would be much more user friendly, especially since I don't know how to permanently save the user's server IP address if they use the above method of setting the IP for the client to connect to.

So, my question is: What would be the best method for me to enable my client to automatically connect to a server running on my LAN?

Thanks,

Patrick



UPDATE

I tried implementing the code for a UDP broadcast, but I can't seem to get it working.

Here is what I've added to my client (Along with the client code I had in there before):

public partial class Console : Form
{

    //FIND SERVER

        private void FindServer()
    {
        var Client = new UdpClient();
        var RequestData = Encoding.ASCII.GetBytes("SomeRequestData");
        var ServerEp = new IPEndPoint(IPAddress.Any, 0);

        Client.EnableBroadcast = true;
        Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));

        var ServerResponseData = Client.Receive(ref ServerEp);
        var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
        Console.WriteLine("Recived {0} from {1}", ServerResponse, ServerEp.Address.ToString());

        Client.Close();
    }


    // SEND MESSAGES TO SERVER (VIA USER INTERACTION)

    private TcpClient client = new TcpClient();
    private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("My IP Address was here (I tried changing it to "broadcast" as well"), 8888);

    public Console()
    {
        InitializeComponent();
        client.Connect(serverEndPoint);

    }

    private void SendMessage(string msg)
    {
        NetworkStream clientStream = client.GetStream();

        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = encoder.GetBytes(msg);

        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();

    }

Here is what I've added to my server code:

private void BroadcastToClients()
    {
        var Server = new UdpClient(8888);
        var ResponseData = Encoding.ASCII.GetBytes("SomeResponseData");

        while (true)
        {
            var ClientEp = new IPEndPoint(IPAddress.Any, 0);
            var ClientRequestData = Server.Receive(ref ClientEp);
            var ClientRequest = Encoding.ASCII.GetString(ClientRequestData);

            Console.WriteLine("Recived {0} from {1}, sending response", ClientRequest, ClientEp.Address.ToString());
            Server.Send(ResponseData, ResponseData.Length, ClientEp);
        }
    }

I'd imagine there must be some conflicting code in there, but since I'm so new to this, I can't seem to figure it out...



UPDATE I've still not managed to make any progress on this. Anyone out there that might be able to chime in and help me figure out why this isn't working for me?

Patrick
  • 430
  • 6
  • 21

1 Answers1

2

Your client app can send a broadcast on the local subnet, on startup, 'asking for server'. Your server will be listening for that message, and replies to the client. Now the client knows the server's IP address and can start the TCP connection.

You have it here: C# How to do Network discovery using UDP Broadcast

Community
  • 1
  • 1
Bruno Garcia
  • 6,029
  • 3
  • 25
  • 38
  • That sounds like exactly what I'm looking for, but as this is my first project in C#, I have no idea how to implement that in code... Would you be able to give me an example of how to do that, or point me in the right direction to learn how to do it myself? Thanks – Patrick Oct 24 '15 at 18:43
  • Can I basically copy the code from that answer directly into my existing code? Like I said, I'm very new to this, and while that answer looks like the type of thing that I need for my project, I'm not sure how to implement it. Sorry for asking such a "newbie" question, but I'm learning :) – Patrick Oct 24 '15 at 19:08
  • That snippet is definitely not production quality code. The objective is to give you some insight so you can further research and implement your solution. – Bruno Garcia Oct 24 '15 at 19:36
  • That's what I assumed it was, however, with my limited knowledge, I'm completely tapped out on how to implement it without conflicting with my existing code (which I need, so my client can send messages to the server). If you (or anyone else) could try to explain to me how to implement it (with the existing client and server code I've provided in my original question), that would be very much appreciated. I'm completely lost as to where to look, and it's starting to hurt my brain a bit lol. Thanks for all your help so far though, I know this is exactly the type of thing I need to implement! – Patrick Oct 24 '15 at 19:54
  • I still haven't been able to make any further progress on this. Any one else feel like chiming in with a tip? – Patrick Oct 27 '15 at 03:35
  • I didn't want to have this question flagged for deletion as a duplicate because it has some good information for other people with similar issues. [HERE](http://stackoverflow.com/questions/34441470/allow-client-app-to-automatically-connect-to-server-app-on-same-network) is a link to a question with an excellent answer for this question. – Patrick Dec 29 '15 at 00:59