C# 测试Socket是否已连接
您好,我正在编写一个简单的服务器程序来侦听连接。我的问题是,如何测试套接字是否已连接。这是我的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你的豆子在这里搞砸了。在底层,在操作系统级别,有两个不同的概念:一个侦听套接字 - 即
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 successfulaccept()
.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.
server.Start()
应该在循环之外。它仅被调用一次,并且侦听套接字将保持打开状态,直到调用Stop
为止。AcceptSocket
将阻塞,直到客户端连接为止。如果您希望能够接受多个套接字,请继续循环它。server.Start()
should be outside the loop. It's only called once and the listening socket will remain open untilStop
is called.AcceptSocket
will block until a client have connected. If you want to be able to accept multiple sockets, then keep looping it.