A. TCP伺服器如何檢測客戶端的IP和埠
你熟悉socket
API不?如果不熟悉的話建議先看看socket編程的文檔。這是鏈接地址:http://msdn.microsoft.com/en-us/library/ms738545(v=VS.85).aspx
一般服務端的sokcet
API調用順序為:
bind()
//
設置服務埠
listen()
//
等待客戶端連接
accept()
//
與客戶端建立連接
請參考:http://msdn.microsoft.com/en-us/library/ms737526(v=VS.85).aspx
這是accept函數的原型:
SOCKET
accept(
__in
SOCKET
s,
__out
struct
sockaddr
*addr,
__inout
int
*addrlen
);
第二個參數,
addr,包含了客戶端的IP地址和埠。你可以認為這就是客戶端的IP和埠。但是,要注意的是這個IP不一定就完全等價於客戶端本機的埠。比如:客戶端在一個區域網里,IP地址是192.168.1.100,然後它通過ADSL路由連接到internet,再通過internet連接到服務端。這個時候,服務端獲得的客戶端IP地址就可能是那個ADSL路由的IP。
B. 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();
}
C. java:伺服器如何檢測客戶端已經斷開
在客戶端退出之後,會出現socket連接中斷,此時是會拋出異常,在拋出異常時列印中斷的客戶端即可。
1. while(Binput.read()!= -1) 這種方法能判斷出客戶端輸入是否為空,客戶端斷開能跳出死循環,但是得到的數據卻從第二個開始了,所以這方法不適宜。
2. 在死循環中加入以下代碼:
try {
socket.sendUrgentData(0);
} catch (IOException e) {
done= false; //如果拋出了異常,那麼就是斷開連接了 跳出無限循環
}
D. 易語言 關於服務端檢查客戶端是否在線的心跳包問題
在客戶端添加一個線程,用來發送在線的心跳包(此包生成的為時間戳,加密),伺服器收到後,自動更新當前在線用戶的在線時間
伺服器添加一個線種,定時循環檢測用戶的時間戳,如果大於或小於設定時間(一般在30秒至1分鍾)即判斷為掉線並做掉線處理;
客戶端防故意斷網,如果發送信息失敗,即斷網
E. socket編程中,伺服器如何檢測到客戶端網路連接的斷開.比如說客戶端的網線斷掉了,從服務端如何能檢測到呢l
通過心跳包來確保客戶端是否正常連接,比如定時發心跳包給客戶端,然後接收回應包,如果沒有收到該回應包則可以認為客戶端已經斷開連接,這個算是常規做法,至少在我所有SOCKET編程中都使用這種方式來維護連接狀態。