如何检测聊天服务器IP [英] How to Detect the Chat Server ip

查看:136
本文介绍了如何检测聊天服务器IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个桌面应用程序(聊天),我希望客户端在不指定ipaddress的情况下进行连接

i was creating a desktop application (chat) and i want the client to connect without specifying the ipaddress

private void btnListen_Click(object sender, EventArgs e)
       {
           IPAddress ipAddr = IPAddress.Parse(txtIp.Text);
           ChatServer mainServer = new ChatServer(ipAddr);
           ChatServer.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
           mainServer.StartListening();
           btnListen.Enabled = false;
           txtIp.Enabled = false;
           Name1.Start();

       }



ChatServer是一个类



ChatServer is a class

class ChatServer
{
public void StartListening()
        {
            IPAddress ipaLocal = ipAddress;
            tlsClient = new TcpListener(1986);
            tlsClient.Start();
 ServRunning = true;
            thrListener = new Thread(KeepListening);
            thrListener.Start();
        }
 private void KeepListening()
        {
            while (ServRunning == true)
            {
                tcpClient = tlsClient.AcceptTcpClient();
                Connection newConnection = new Connection(tcpClient);
            }
        }




在客户端程序中
我必须指定IP地址才能与服务器连接.


是否有一种方法可以在不指定ipddress




in client program
i have to specify the ip address in order to connect with the server.


IS there a way to connect the client with server without specifying the ipddress

推荐答案

的情况下将客户端与服务器连接,或者您应该使用IP,或者应该使用服务器的DNS名称. br/>
但是,如果要连接到本地网络中的游戏服务器,则可能要使用IP广播.我不确定TCP/IP是否允许,但UDP是否允许.

然后,您可能想要同时使用两者,例如:
服务器开始收听广播消息.
一旦它接收到客户端消息(一条广播消息,说客户端在那儿),它将获取该客户端的IP并发送另一条UPD消息(以便客户端可以检查它是否是服务器消息).
然后,客户端连接到该服务器的另一个端口.请注意,服务器消息中可能包含此类端口.

请参阅 http://msdn.microsoft.com/上的UdpClient.EnableBroadcast. zh-cn/library/system.net.sockets.udpclient.enablebroadcast.aspx [
Or you should use the IP, or you should use the DNS name of the server.

But if what you want is to connect to a game server in the local network, you may want to use IP broadcasting. I am not sure if TCP/IP allows this, but UDP allows it.

You may then want to use both, something like this:
Server starts to listen to broadcast messages.
As soon as it receives a client message (a broadcast message that says the client is there), it gets the IP of that client and sends another UPD message (so the client can check if it is the server message).
The client then connects to another port of such server. Note that the server message may include such port.

See the UdpClient.EnableBroadcast at http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.enablebroadcast.aspx[^]


这篇关于如何检测聊天服务器IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆