我正在学习套接字,我有这个程序:
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 client
{
public class MyClient
{
static ManualResetEvent _clientDonte = new ManualResetEvent(false);
private static Socket _clientSocket;
private SocketAsyncEventArgs _connectSocketEventArg;
private CancellationTokenSource _cancelationTokenSource;
public MyClient()
{
}
public void Start(string address, uint port)
{
_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPEndPoint serverEP = new IPEndPoint(IPAddress.Parse(address), (int)port);
_connectSocketEventArg = new SocketAsyncEventArgs();
_connectSocketEventArg.Completed += ConnectSocketEventArgCompleted;
_connectSocketEventArg.RemoteEndPoint = serverEP;
_connectSocketEventArg.UserToken = _clientSocket;
_clientSocket.ConnectAsync(_connectSocketEventArg);
_clientDonte.WaitOne();
Console.WriteLine("DAJESZ KURWA, NAPISZ COS!");
while (true)
{
SendFromConsole();
}
}
private void SendFromConsole()
{
var message = Console.ReadLine();
var buff = Encoding.ASCII.GetBytes(message);
_connectSocketEventArg.SetBuffer(buff, 0, buff.Length);
_clientSocket.SendAsync(_connectSocketEventArg);
}
private void ConnectSocketEventArgCompleted(object sender, SocketAsyncEventArgs e)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ReceiveAsync(e);
break;
case SocketAsyncOperation.Send:
SendAsync(e);
break;
default:
throw new Exception("Invalid operation completed");
}
}
private void SendAsync(SocketAsyncEventArgs e)
{
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
Socket sock = e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ReceiveAsync(e);
}
}
}
private void ReceiveAsync(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
Console.WriteLine("SERVER>>: {0}", Encoding.ASCII.GetString(e.Buffer));
Socket sock = e.UserToken as Socket;
}
}
private void ProcessConnect(SocketAsyncEventArgs e)
{
_cancelationTokenSource = new CancellationTokenSource();
Task.Factory.StartNew(ProgressConnect, _cancelationTokenSource.Token);
if (e.SocketError == SocketError.Success)
{
Thread.Sleep(701);
_cancelationTokenSource.Cancel();
Console.WriteLine("Nawiazano polaczenie!");
_clientDonte.Set();
}
else if (e.SocketError == SocketError.SocketError)
{
Console.WriteLine("Nie udalo sie! nacisnij cos by wyjsc");
Console.ReadKey();
Environment.Exit(0);
}
}
private void ProgressConnect()
{
while (!_cancelationTokenSource.Token.IsCancellationRequested)
{
Console.Write(".");
_cancelationTokenSource.Token.WaitHandle.WaitOne(700);
}
}
}
}
应该允许写入消息,发送消息并异步等待响应。不幸的是,当我发送第一条消息时,它会陷入无限循环的发送。 我看过这个Reusing SocketAsyncEventArgs with ReceiveAsync results in infinite loop主题,但它没有帮助。
答案 0 :(得分:1)
我从未使用过这种结构,但是;
无限循环的发送可能是,您永远不会检查您尝试发送的字节是否已发送。您可能想要检查BytesTransferred
属性是否等于您尝试发送的字节。
此外,您正在使用手动重置事件阻止当前线程..为什么您没有使用非异步Connect()
?