有两个问题如下
第一个问题,我足足等了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();
}
}
}