Socket服务器端如何检测客户端的连接状态?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket服务器端如何检测客户端的连接状态?相关的知识,希望对你有一定的参考价值。
Socket TCP/IP通信中,服务器端如何检测到客户端的断开连接动作,由于客户端不是我编写的,所以不能要求客户端在断开连接时发送断开消息,我的服务器端也不能通过定时向客户端发送指令检测客户端的连接状态,会造成客户端接收数据的混乱
请问各位大虾,服务器端有没有其他办法能够检测到客户端断开连接的动作?
环境是C#.NET
客户端与服务端都不发送心跳包
可以的话给个实例啊,谢谢了
首先谢谢wefgod3,但是你给我的不是我想要的
我的服务端开启了多线程,每个客户端连接一个线程,当客户端执行Socket.Shutdown、Socket.Close()主动断开连接时,希望服务端能够检测到客户端的这个动作,然后取消与此客户端相连的线程和Socket
现在我不知道是服务端上层在不主动发送心跳包的情况下,如何得知客户端的Socket已经关闭了
http://msdn.microsoft.com/zh-cn/library/system.net.sockets.aspx
这里例子很多
但是具体的可能你看比我看好···毕竟你比我了解的多
里面的类可能你能用到
既然这样我还是直接给你两个看看算了···
TcpListener 类
从 TCP 网络客户端侦听连接。
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MyTcpListener
public static void Main()
TcpListener server=null;
try
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while(true)
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: 0", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: 0", data);
// Shutdown and end connection
client.Close();
catch(SocketException e)
Console.WriteLine("SocketException: 0", e);
finally
// Stop listening for new clients.
server.Stop();
Console.WriteLine("\nHit enter to continue...");
Console.Read();
TcpClient 类
为 TCP 网络服务提供客户端连接。
static void Connect(String server, String message)
try
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: 0", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: 0", responseData);
// Close everything.
stream.Close();
client.Close();
catch (ArgumentNullException e)
Console.WriteLine("ArgumentNullException: 0", e);
catch (SocketException e)
Console.WriteLine("SocketException: 0", e);
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
本回答被提问者采纳
Socket(套接字)在服务器端和客户端之间的基本工作原理
Socket之间的连接过程主要可以概括为以下三步:
-
服务器建立监听:客户端初始化Socket动态库后创建套接字,然后指定客户端Socket的地址,循环绑定Socket直至成功,然后开始建立监听,此时客户端处于等待状态,实时监控网络状态;
-
客户端提出请求:客户端的Socket向服务器端提出连接请求,此时客户端描述出它所要连接的Socket,指出要连接的Socket的相关属性,然后向服务器端Socket提出请求;
- 连接确认并建立:当服务器端套接字监听到来自客户端的连接请求之后,立即响应请求并建立一个新进程,然后将服务器端的套接字的描述反馈给客户端,由客户端确认之后连接就建立成功,然后客户端和服务器两端之间可以相互通信,传输数据,此时服务器端的套接字继续等待监听来自其他客户端的请求;
以上是关于Socket服务器端如何检测客户端的连接状态?的主要内容,如果未能解决你的问题,请参考以下文章