我想知道,如果我在其他线程上同步运行,我的应用程序是否会异步获得字符串?
我实际上不知道如何使用BeginSend/BeginReceive,所以我使用了socket.Send(...),socket.Receive(...)在不同的线程上,这是否使我的应用程序模拟异步连接。
顺便说一下,如果字符串大于缓冲区的大小,会发生什么?对于客户端,我使用了telnet,但是telnet实际上是在从键盘上获取字符串后立即发送它们,所以我实际上不能超过缓冲区的大小,但是如果我使用另一个发送完整字符串的客户端呢?有没有办法告诉程序我发送的数量超过了缓冲区允许的数量(通过我的变量recv,它是socketReceive的结果)?
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace CSharp_Console_Application
{
class Program
{
delegate Socket GetClients();
static GetClients newClients;
static List<Socket> clients;
static ManualResetEvent allDone = new ManualResetEvent(false);
static void Main(string[] args)
{
IPEndPoint serverIPEP = new IPEndPoint(IPAddress.Any, 9080);
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(serverIPEP);
serverSocket.Listen(10);
Console.WriteLine("Waiting for connections...");
clients = new List<Socket>();
newClients = () =>
{
Socket clientSocket = serverSocket.Accept();
IPEndPoint clientIPEP = (IPEndPoint)clientSocket.RemoteEndPoint;
Console.WriteLine("Connected to {0}.", clientIPEP);
clients.Add(clientSocket);
SendString("Welcome to my server!", clientSocket);
return clientSocket;
};
while (true)
{
allDone.Reset();
newClients.BeginInvoke((itfAR) =>
{
allDone.Set();
AsyncResult ar = (AsyncResult)itfAR;
GetClients invokedDelegate = (GetClients) ar.AsyncDelegate;
Socket clientSocket = invokedDelegate.EndInvoke(itfAR);
IPEndPoint clientIPEP = (IPEndPoint)clientSocket.RemoteEndPoint;
Console.WriteLine("Sent 'Welcome!' to {0}.", clientIPEP);
string currentString = "";
while (true)
{
currentString += ReceiveString(clientSocket);
if (currentString.Contains('\n'))
{
Console.Write(clientIPEP + " said: " + currentString);
List<Socket> clientsWithoutThis = new List<Socket>();
clientsWithoutThis.AddRange(clients);
clientsWithoutThis.Remove(clientSocket);
SendToAll(clientsWithoutThis, currentString);
currentString = "";
}
}
},
null);
allDone.WaitOne();
}
}
static void SendToAll(List<Socket> clients, string message)
{
byte[] data = new byte[1024];
clients.ForEach(clientSocket =>
{
IPEndPoint clientIPEP = (IPEndPoint)clientSocket.RemoteEndPoint;
data = Encoding.ASCII.GetBytes(clientIPEP + " said: " + message + "\r");
clientSocket.Send(data, data.Length, SocketFlags.None);
});
}
static void SendString(string message, Socket clientSocket)
{
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(message + "\n\r");
clientSocket.Send(data, data.Length, SocketFlags.None);
}
static string ReceiveString(Socket clientSocket)
{
byte[] data = new byte[1024];
int recv = clientSocket.Receive(data, data.Length, SocketFlags.None);
if (recv < 1)
return null;
string receivedString = Encoding.ASCII.GetString(data, 0, recv);
return receivedString;
}
}
}发布于 2013-06-26 18:57:20
这样一来,你的ManualResetEvent就变得无用了。一旦阻塞了主线程,就会通过调用set()在BeginInvoke的第一行中释放。然后将其重新设置为等待状态,但在另一个BeginInvoke可能仍在运行时,另一个BeginInvoke调用将解除代码阻塞。因此,最终您将拥有多个BeginInvoke线程,并最终将出现多个异常或内存不足错误。
https://stackoverflow.com/questions/10195117
复制相似问题