C# 测试Socket是否已连接

发布于 2024-10-15 11:16:30 字数 1096 浏览 1 评论 0原文

您好,我正在编写一个简单的服务器程序来侦听连接。我的问题是,如何测试套接字是否已连接。这是我的代码

using System;
using System.Net;
using System.Net.Sockets;

class server
{
    static int port = 0;
    static String hostName = Dns.GetHostName();
    static IPAddress ipAddress;
    static bool listening = true;

    public static void Main(String[] args)
    {
        IPHostEntry ipEntry = Dns.GetHostByName(hostName);

        //Get a list of possible ip addresses
        IPAddress[] addr = ipEntry.AddressList;

        //The first one in the array is the ip address of the hostname
        ipAddress = addr[0];

        TcpListener server = new TcpListener(ipAddress,port);

        Console.Write("Listening for Connections on " + hostName + "...");

        do
        {

            //start listening for connections
            server.Start();



        } while (listening);


        //Accept the connection from the client, you are now connected
        Socket connection = server.AcceptSocket();

        Console.Write("You are now connected to the server");

        connection.Close();


    }


}

Hi i am writing a simple server program that is listening for connections. My Question is, How can I test if the socket is connected or not. Here is my code

using System;
using System.Net;
using System.Net.Sockets;

class server
{
    static int port = 0;
    static String hostName = Dns.GetHostName();
    static IPAddress ipAddress;
    static bool listening = true;

    public static void Main(String[] args)
    {
        IPHostEntry ipEntry = Dns.GetHostByName(hostName);

        //Get a list of possible ip addresses
        IPAddress[] addr = ipEntry.AddressList;

        //The first one in the array is the ip address of the hostname
        ipAddress = addr[0];

        TcpListener server = new TcpListener(ipAddress,port);

        Console.Write("Listening for Connections on " + hostName + "...");

        do
        {

            //start listening for connections
            server.Start();



        } while (listening);


        //Accept the connection from the client, you are now connected
        Socket connection = server.AcceptSocket();

        Console.Write("You are now connected to the server");

        connection.Close();


    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉风有信 2024-10-22 11:16:30

我想你的豆子在这里搞砸了。在底层,在操作系统级别,有两个不同的概念:一个侦听套接字 - 即TcpListener,以及一个连接套接字 - 这就是您所需要的成功accept()后获取。

现在,侦听 TCP 套接字尚未连接,但绑定到本地计算机上的端口(可能还有地址)。这是服务器等待来自客户端的连接请求的地方。一旦此类请求到达,操作系统就会创建一个新的套接字,该套接字在某种意义上是连接的,它具有通信所需的所有四个部分 - 本地 IP 地址和端口以及远程地址和端口 - 已填充。

从一些介绍性文本开始,像这个之类的东西。更好的是 - 从真实的开始。

I think you got your beans messed up here. Underneath, at the OS level, there are two distinct notions: a listening socket - that's the TcpListener, and a connected socket - that's what you get after successful accept().

Now, the listening TCP socket is not connected, but bound to the port (and possibly address) on the local machine. That's where a server waits for connection requests from clients. Once such request arrives the OS creates a new socket, which is connected in a sense that it has all four parts that are required for communication - local IP address and port, and remote address and port - filled in.

Start with some introductory text, something like this one. Even better - start with a real one.

强者自强 2024-10-22 11:16:30

server.Start()应该在循环之外。它仅被调用一次,并且侦听套接字将保持打开状态,直到调用 Stop 为止。

AcceptSocket 将阻塞,直到客户端连接为止。如果您希望能够接受多个套接字,请继续循环它。

server.Start()should be outside the loop. It's only called once and the listening socket will remain open until Stop is called.

AcceptSocket will block until a client have connected. If you want to be able to accept multiple sockets, then keep looping it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文