2011-10-20 3 views
1

이 문제에 대한 도움이나 의견을 보내 주시면 감사하겠습니다. 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); 
     } 
    } 
+0

당신은 HTTP이 사이트를'봤어? –

답변

0

한 가지 옵션은 기존 서비스 검색 프로젝트를 사용하는 것입니다. ZeroConf은 Apple의 Bounjour를 기반으로하는 전체 .NET 구현입니다. 이 프레임 워크를 활용하면 앱을 시작하고 사용 가능한 모든 서비스와 IP 주소를 쿼리 할 수 ​​있습니다.

코드는 project documentation의 직접 복사물이지만 사용 용이성을 보여주기 위해 게시되었습니다. // meta.codereview.stackexchange.com /`:

검색 (클라이언트 측)

static void Main(string[] args) 
    { 
     BonjourServiceResolver bsr = new BonjourServiceResolver(); 
     bsr.ServiceFound += new Network.ZeroConf.ObjectEvent<Network.ZeroConf.IService>(bsr_ServiceFound); 
     bsr.Resolve("_daap._tcp"); 
     Console.ReadLine(); 
    } 

    static void bsr_ServiceFound(Network.ZeroConf.IService item) 
    { 
     // Here goes the code for what you want to do when a service is discovered 
     Console.WriteLine(item); 
    } 

출판 (서버 사이드)

Service s = new Service(); 
s.AddAddress(ResolverHelper.GetEndPoint()); 
s.Protocol = "_touch-able._tcp.local."; 
s.Name = "MyName"; 
s.HostName = s.Addresses[0].DomainName; 

//The indexer on the service enables to set metadatas 
s["DvNm"] = "PC Remote"; 
s["RemV"] = "10000"; 
s["DvTy"] = "iPod"; 
s["RemN"] = "Remote"; 
s["txtvers"] = "1"; 
s["Pair"] = "0000000000000001"; 

//After setting all this, the only thing left to do is to publish your service 
s.Publish(); 
Thread.Sleep(3600000); 
s.Stop(); 
관련 문제