BrandonPotter / SimpleTCP

Straightforward .NET library to handle the repetitive tasks of spinning up and working with TCP sockets (client and server).
Apache License 2.0
363 stars 108 forks source link

server.GetIPAddresses() not working correctly #52

Closed emnylmz closed 5 years ago

emnylmz commented 5 years ago

I want to send messages to clients connected to the server. But the GetIpAdresses method gives me my own ip address or my local address (127.0.0.1).

SimpleTcpClient client; private void button1_Click(object sender, EventArgs e) { foreach (var serverIp in server.GetIPAddresses()) { client = new SimpleTcpClient(); client.StringEncoder = Encoding.UTF8; client.DataReceived += Client_DataReceived; client.Connect(serverIp.ToString(),23); client.WriteLineAndGetReply(rtb_server_message.Text, TimeSpan.FromSeconds(10)); }

    }
BrandonPotter commented 5 years ago

Seems like there might be a misunderstanding here... Server.GetIPAdsresses() returns the IP’a that the server is listening on, not clients connected.

Server should fire off a client connected event, which will give you the tcpclient that you can use to send a message to that client.

emnylmz commented 5 years ago

Thanks for your answer. So how do I send messages from the server to the client? How can I access the ropes of clients connected to the server? Does SimpleTcpServer have a method for this?

BrandonPotter commented 5 years ago

Look at the “broadcast” message if you want to send something to all connnected clients.

Or add a handler to the ClientConnected event to be notified when a client is connected and get the individual client’s object.

emnylmz commented 5 years ago

Problem is solved. Thanks. :)