이 문제에 대한 도움이나 의견을 보내 주시면 감사하겠습니다. C#에서 비동기 소켓 연결을 개발 중이며 로컬 네트워크 서버를 브로드 캐스팅하고 로컬 서버에서 메시지를 받도록 브로드 캐스트 클라이언트 수신기를 설정하려고합니다. 주요 문제는 먼저 한 클라이언트에서 다른 서버로 브로드 캐스팅 한 다음 모든 서버에서 IP 주소를 검색하려고합니다. 여기는 클라이언트 코드의 일부입니다. 또한 서버 쪽이 잘 작동합니다.비동기 클라이언트 브로드 캐스트 수신기
public void ButtonConnectOnClick()
{
// Init socket Client
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
IPAddress ipAddress = IPAddress.Broadcast; //Parse(txtServerIP.Text);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, BROADCASTPORT);
epServer = (EndPoint)ipEndPoint;
string tmp = "hello";
byteData = Encoding.ASCII.GetBytes(tmp);
newsock.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
byteData = new byte[1024];
newsock.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer, new AsyncCallback(OnReceive), null);
}
private void OnSend(IAsyncResult ar)
{
try
{
newsock.EndSend(ar);
}
catch (ObjectDisposedException)
{ }
catch (Exception ex)
{
MessageBox.Show(ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnReceive(IAsyncResult ar)
{
try
{
newsock.EndReceive(ar);
byteData = new byte[1024];
//Start listening to receive more data from the user
newsock.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer, new AsyncCallback(OnReceive), null);
}
catch (ObjectDisposedException)
{ }
catch (Exception ex)
{
MessageBox.Show(ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
당신은 HTTP이 사이트를'봤어? –