views:

2145

answers:

3

As kind of a followup to this question I've gotten a solution working on my local machine, but not on a machine on the network.

I don't know too much about sockets other than that basics, so bear with me. The goal is for a client to look for a server on a local network, and this is the result of some cut/paste/edit code.

This is the client code:

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10294);

    byte[] data = new byte[1024];
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {

        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 10);
        string welcome = "What's your IP?";
        data = Encoding.ASCII.GetBytes(welcome);
        client.SendTo(data, data.Length, SocketFlags.None, ipep);
        IPEndPoint server = new IPEndPoint(IPAddress.Any, 0);
        EndPoint tmpRemote = (EndPoint)server;
        data = new byte[1024];
        int recv = client.ReceiveFrom(data, ref tmpRemote);
        this.IP.Text = ((IPEndPoint)tmpRemote).Address.ToString(); //set textbox
        this.Port.Text = Encoding.ASCII.GetString(data, 0, recv); //set textbox
        client.Close();
    }

This is the server code:

int recv;
        byte[] data = new byte[1024];
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10294);
        Socket newsock = new Socket(AddressFamily.InterNetwork,
                SocketType.Dgram, ProtocolType.Udp);
        newsock.Bind(ipep);
        newsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Any,IPAddress.Parse("127.0.0.1")));

        while (true)
        {
            Console.WriteLine("Waiting for a client...");
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint tmpRemote = (EndPoint)(sender);
            data = new byte[1024];
            recv = newsock.ReceiveFrom(data, ref tmpRemote);
            Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
            string welcome = "7010";
            data = Encoding.ASCII.GetBytes(welcome);
            newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
        }

It works find on my local machine (both server and client) but when I try another machine on the same network I get "An existing connection was forcibly closed by the remote host"

I realize I need to add a lot of try/catch but I'm just trying to get a handle on how this works first.

+2  A: 

I have to start by saying that I know nothing about C#, but...

Looking at the definition of the ipep in the client code, it looks like you're trying to send your data to yourself, rather than broadcast it (as has been suggested in your other question). The thing that caught my attention was that "127.0.0.1" is the address of "localhost".

That would explain why it works nicely when you're running both the client and server on the one machine, as it will be sending to itself.

I would expect that correct endpoint would be for a broadcast address (eg. "255.255.255.255") - although you could also choose the broadcast address of the local network that you're on, depending on how widely you wish to broadcast.

Andrew Edgecombe
Yeah, looking back at it after you mentioned that makes sense, at the time (since this is mostly copy/pasted code) I assumed that was where I was sending from, which of course if I stopped to think about it some more makes no sense to state where I'm sending from.
Davy8
I'll won't be able to try it out till Monday, but I'll +1 and accept if that works!
Davy8
A: 

Could either the OP or Andrew please be so kind as to update the original problem so that we could all benefit from this?

I've been searching for the solution, but cannot seem to understand where the broadcast should be programmed in.

Thanks.

Richard B
A: 
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10294);

Should become:

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 10294);

And

newsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Any, IPAddress.Parse("127.0.0.1")));

Should Become

newsock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Any, IPAddress.Parse("255.255.255.255")));

I think.

OK, this doesn't work, so something's still wrong.

Tuskan360