상태에 따라 UDP를 보내는 여러 대의 컴퓨터를 청취하는 프로그램이 필요합니다. 채팅 프로그램에서 비트를 잘라내어 붙여 넣었지만 시작시에 "시스템에 충분한 버퍼 공간이 없거나 큐가 가득 차서 소켓 작업을 수행 할 수 없습니다"라는 오류 메시지가 나타납니다. 만약 내가 좋아 그러면 오류가 모두 잘 작동합니다. 아래 코드에서 While (true)을 제거하면 오류가 발생하지 않지만 항목을 하나만 가져옵니다. 콜백이 계속 실행되고 데이터가 전달 될 때마다이를 처리하기 위해 보낼 것이라고 생각했지만 잘못된 것일 수 있습니다. 이것은 내 문제가 어디에 있는지 믿을 수 있지만 그것을 해결하는 방법을 해결할 수 없습니다 그래서 소켓을 폴링하지 않고 데이터를 계속 받습니까?C# BeginReceiveFrom이 (가) 오류를 내고 있습니다.
private void Form1_Load(object sender, EventArgs e)
{
try
{
CheckForIllegalCrossThreadCalls = false;
//We are using UDP sockets
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//Assign the any IP of the machine and listen on port number 701
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 701);
//Bind this address to the server
serverSocket.Bind(ipEndPoint);
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender;
while (true)
{
//Start receiving data
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
가능한 경우 [OnReceive]에 대한 코드를 포함시켜주십시오 (http://stackoverflow.com/posts/25769099/edit). –
두려운 CheckForIllegalCrossThreadCalls 할당을 제거하십시오. while (true) 루프를 근본적으로 제거하십시오. 이제 올바른 코드를 작성할 확률이 훨씬 높아집니다. –