C#中异步Stocket中EndReceive报远程主机强迫关闭了一个现有的连接问题

孟林-David.Meng 2016-04-12 10:37:09
有两个问题如下
第一个问题,我足足等了2分钟,ReceiveCallback里的断点才进去,怎么会等这么长时间
第二个问题,ServerSocket 中的EndReceive报“远程主机强迫关闭了一个现有的连接”这个错误
调试截图如下






ClientSocket 中的Program.cs代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SendTest
{
public class StateObject
{
public Socket workSocket = null;
public const int BUFFER_SIZE = 1024;
public byte[] buffer = new byte[BUFFER_SIZE];
public StringBuilder sb = new StringBuilder();
}

class Program
{
static Socket clientSocket;
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, 8081)); //配置服务器IP与端口
Console.WriteLine("连接服务器成功");
}
catch
{
Console.WriteLine("连接服务器失败,请按回车键退出!");
return;
}
Send();
}

/// <summary>
/// 发送消息
/// </summary>
private static void Send()
{
if (clientSocket != null)
{
try
{
byte[] byteData = Encoding.UTF8.GetBytes("我是客户端,寻求帮助");
clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), clientSocket);
Console.WriteLine("BeginSend 执行完毕");
Console.ReadLine();
}
catch (Exception ex)
{

}
}
}

/// <summary>
/// 发送消息回掉函数
/// </summary>
/// <param name="ar"></param>
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
if (client != null)
{
int bytesSent = client.EndSend(ar);
}
}
catch (Exception ex)
{

}
}
}
}

ServerSocket 中的Program.cs代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ReceiveTest
{
class Program
{
static Socket clientSocket;
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, 8081)); //配置服务器IP与端口
Console.WriteLine("连接服务器成功");
}
catch
{
Console.WriteLine("连接服务器失败,请按回车键退出!");
return;
}

Thread myThread = new Thread(Receive);
myThread.Start();
Console.ReadLine();
}

/// <summary>
/// 接收消息
/// </summary>
private static void Receive()
{
if (clientSocket != null)
{
try
{
StateObject state = new StateObject();
state.workSocket = clientSocket;
clientSocket.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallback), state);
Console.WriteLine("BeginReceive 执行完毕");
}
catch (Exception ex)
{

}
}
}

/// <summary>
/// 接收消息回掉函数
/// </summary>
/// <param name="ar"></param>
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject so = (StateObject)ar.AsyncState;
Socket s = so.workSocket;
int read = s.EndReceive(ar);
if (read > 0)
{
string msg = Encoding.ASCII.GetString(so.buffer, 0, read);
}
}
catch (Exception ex)
{

}
}

public class StateObject
{
public Socket workSocket = null;
public const int BUFFER_SIZE = 1024;
public byte[] buffer = new byte[BUFFER_SIZE];
public StringBuilder sb = new StringBuilder();
}
}
}
...全文
538 1 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
此问题已经解决了,解决思路
1.同步发,同步接的Demo我也做了一份,发送没有问题,接收也没有问题
2.异步发,异步接的这个Demo,在异步接里面报错EndReceive报“远程主机强迫关闭了一个现有的连接问题”就有问题,所以我就想,那么我就用异步发,同步接总行了吧,果然同步接就好着呢,可以接到异步发来的消息
3.我就想同样的异步发代码,但是同步接可以接到消息,异步接就报错不行,我就对比同步接和异步接的代码,发现问题如下,果然问题出现在这里了,修复后,异步发异步接就好了
如下图所示
同步接代码




而异步接里面直接用的是clientSocket,没有用clientSocket.Accept()去得到一个新的Scoket


改造异步接代码如下图所示就好了


执行结果如下
  • 打赏
  • 举报
回复 1
发帖
C#
加入

10.7w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
申请成为版主
帖子事件
创建了帖子
2016-04-12 10:37
社区公告

让您成为最强悍的C#开发者