日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

10054 java,为什么Socket.Receive在远程主机断开连接时抛出SocketException(10054)?

發(fā)布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 10054 java,为什么Socket.Receive在远程主机断开连接时抛出SocketException(10054)? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我以前用C編寫套接字程序,無法理解為什么會發(fā)生這種情況 .

我的服務器在接收調用時阻塞,當它返回0時,我打破了while循環(huán)并關閉了線程 .

public class MyServer {

public MyServer() {

}

public void Init() {

ThreadPool.QueueUserWorkItem(StartListening);

while (true) {

Console.Read();

}

}

public void StartListening(Object state) {

// Establish the local endpoint for the socket.

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try {

// Bind the socket to the local endpoint and listen for incoming connections.

listener.Bind(localEndPoint);

listener.Listen(100);

while (true) {

Console.WriteLine("Waiting for a connection...");

// get connection

Socket client = listener.Accept();

// push client to another thread

ThreadPool.QueueUserWorkItem(HandleClient, client);

}

} catch (Exception e) {

Console.WriteLine(e.ToString());

}

}

private void HandleClient(Object obj) {

// Get the socket that handles the client request.

Socket client = (Socket)obj;

// Create the state object.

StateObject state = new StateObject();

state.workSocket = client;

try {

while (true) {

int bytesRead = client.Receive(state.buffer);

Console.WriteLine("bytesRead=" + bytesRead);

// remote dc.

if (bytesRead == 0)

break;

String content = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);

Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);

client.Send(state.buffer, 0, state.buffer.Length, 0);

}

} catch (SocketException e) {

Console.WriteLine("SocketException : {0}", e.ToString());

}

client.Shutdown(SocketShutdown.Both);

client.Close();

}

private void Send(Socket handler, String data) {

byte[] byteData = Encoding.ASCII.GetBytes(data);

// Begin sending the data to the remote device.

//handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);

}

}

但是,當我單擊客戶端的關閉按鈕("x")時,服務器的Receive調用將拋出SocketException . 根據(jù)MSDN's Remarks section,這不應該滿足面向連接的部分(見上文)和客戶端關閉部分(見下文)的條件 .

Client.cs (pseudo):

public class MyClient {

public void Init() {

byte[] bytes = new byte[1024];

Socket sender = null;

try {

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

sender = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.Tcp);

sender.Connect(remoteEP);

Console.WriteLine("Socket connected to {0}",

sender.RemoteEndPoint.ToString());

while (true) {

// Encode the data string into a byte array.

String input = Console.ReadLine();

byte[] msg = Encoding.ASCII.GetBytes(input);

// Send the data through the socket.

int bytesSent = sender.Send(msg);

// Receive the response from the remote device.

int bytesRec = sender.Receive(bytes);

Console.WriteLine("Echoed test = {0}",

Encoding.ASCII.GetString(bytes, 0, bytesRec));

}

} catch (ArgumentNullException ane) {

Console.WriteLine("ArgumentNullException : {0}", ane.ToString());

} catch (SocketException se) {

Console.WriteLine("SocketException : {0}", se.ToString());

} catch (Exception e) {

Console.WriteLine("Unexpected exception : {0}", e.ToString());

} finally {

sender.Shutdown(SocketShutdown.Both);

sender.Close();

}

}

}

我對finally塊的淺層理解是它總會執(zhí)行 . 但這似乎并非如此 .

那么,我在這里做錯了什么?我是否應該捕獲異常,關閉服務器端的客戶端套接字然后繼續(xù),忽略它?但我更愿意,如果沒有拋出異常 .

總結

以上是生活随笔為你收集整理的10054 java,为什么Socket.Receive在远程主机断开连接时抛出SocketException(10054)?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。