UdpClient
클래스를 사용 중입니다. .net 3.5
동일한 포트에 여러 애플리케이션을 바인드해야합니다.다중 포트를 동일한 포트에 바인드
서버가 모든 요청을 브로드 캐스트하면 포트에서 수신 대기하는 모든 응용 프로그램이 메시지를받을 수 있지만 문제는 동일한 포트에 응용 프로그램을 바인드하려고하면 하나의 응용 프로그램에서만 메시지를 수신하고 다른 사람은 그렇지 않습니다.
UdpClient udpClient = new UdpClient();
Thread thread;
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 11000);
public Form1()
{
//CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
udpClient.ExclusiveAddressUse = false;
udpClient.Client.SetSocketOption(
SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpClient.Client.Bind(endPoint);
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
thread.Abort();
udpClient.Close();
Close();
}
}
private void ReceiveMessage()
{
//while (true)
//{
// IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 11000);
// byte[] content = udpClient.Receive(ref endPoint);
udpClient.BeginReceive(new AsyncCallback(Read_Callback), null);
//if (content.Length > 0)
//{
// string message = Encoding.ASCII.GetString(content);
// this.Invoke(myDelegate, new object[] { message });
//}
// }
}
public void Read_Callback(IAsyncResult ar)
{
try
{
byte[] buffer = udpClient.EndReceive(ar, ref endPoint);
// Process buffer
string s = Encoding.ASCII.GetString(buffer);
// richTextBox1.Text = s;
udpClient.BeginReceive(new AsyncCallback(Read_Callback), null);
}
catch (Exception ex)
{ }
}
PS : 아래
는 두 응용 프로그램에 대한 몇 가지 예제 코드 나는 이유를 알아낼 수 없습니까 아니면 내가 뭔가를 놓친 거지. ?
감사합니다. 링크가 나를 도와주었습니다 ... –