2011-08-31 2 views
0

기본 UDP 파이프 또는 리디렉터를 찾고 있습니다. 물론 모두 볼 수 있어야합니다기본 UDP C# 또는 VB.NET 파이프?

클라이언트 2 서버 및 서버 2 클라이언트 데이터. 여기

내가 뭘하려하지만 수신되는 전화를 할 때 모르기 때문에 실패합니다 ..

은 내가이 다시 라우팅해야 다음
data = serverUdpClient.Receive(sender) 

를 호출 말, 그래서 나는이 전화

clientUdpClient.Send(data, data.Length) 

이제 TCP 파이프에서 적절한 줄이옵니다.

data = clientUdpClient.Receive(sender).. 

하지만 .. 다시 전화를해야 ..

data = serverUdpClient.Receive(sender) 
    clientUdpClient.Send(data, data.Length) 

내가

data = clientUdpClient.Receive(sender).. 

꽤 많은 코드의 흐름이 모든 엿 사용하기 전에 ..이 소켓이되어 있기 때문에 블로킹. 내가 UDP로 작업을 시작했을 때 .. 모든 예제는 초보자가 네트워킹 소켓으로 작업하려고하기 때문에 너무 비싸지 않으므로 비 블로킹을 피한다고 말한다. 나는 그 문장이 틀렸다는 것을 안다.

Public serverUdpClient As System.Net.Sockets.UdpClient 
Public clientUdpClient As System.Net.Sockets.UdpClient 

Sub runProxy() 
    If serverUdpClient IsNot Nothing Then 
     serverUdpClient.Close() 
     serverUdpClient = Nothing 
    End If 
    If clientUdpClient IsNot Nothing Then 
     clientUdpClient.Close() 
     clientUdpClient = Nothing 
    End If 

    Try 
     'Listen for incoming udp connection request. 
     serverUdpClient = New UdpClient(New IPEndPoint(IPAddress.Any, Int32.Parse(Int(txtListeningPort.Text)))) 

     WriteLog("Server started at: " + txtListeningPort.Text) 

     Dim data As Byte() = New Byte(1023) {} 
     Dim sender As IPEndPoint = New IPEndPoint(IPAddress.Any, 0) 

     While True 
      data = serverUdpClient.Receive(sender) 

      'Connect to server. 
      If clientUdpClient Is Nothing Then 
       clientUdpClient = New UdpClient(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text))) 
       clientUdpClient.Connect(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text))) 
      End If 

      clientUdpClient.Send(data, data.Length) 
      data = clientUdpClient.Receive(sender) 

      serverUdpClient.Send(data, data.Length) 
     End While 
    Catch ex As Exception 
     WriteLog("Errors at runProxy @ " + ex.Message) 
    End Try 
End Sub 

시도해도 제대로 작동하지 않습니다.

 While True 
      If serverUdpClient.Available > 0 Then 
       data = serverUdpClient.Receive(sender) 

       'Connect to server. 
       If clientUdpClient Is Nothing Then 
        clientUdpClient = New UdpClient(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text))) 
        clientUdpClient.Connect(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text))) 
       End If 

       clientUdpClient.Send(data, data.Length) 
      End If 

      If clientUdpClient.Available > 0 Then 
       data = clientUdpClient.Receive(sender) 

       serverUdpClient.Send(data, data.Length) 

      End If 

     End While 

답변

0

여기

은 내가 만든 것을 비동기없이 하나 ..입니다 .. 내가해야 할 일을했을 모두가 연결된 인스턴스에 내 보낸 IPEndPoint로 변환했다가 수정과 구글 모두에 .. 단지 UDP 프록시를 작동/C에서 코딩 된 SO #.

누군가가 내가 그것을 고쳤다는 것을 배우고 싶다면 해결책이있다. ..이게 우연히 발견되면 이것은 아마도 모든 구글의 유일한 UDP 프록시 일 뿐이라는 것을 기억해 두라. 그것은 C#으로 코딩되어있다 .. 쉽게 이식된다. 온라인 .NET 컨버터 VB.NET에

이 코드가 작동 행복하세요)

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Net.Sockets; 
using System.Net; 

namespace UdpProxy 
{ 
    class Program 
    { 
     public static IPEndPoint m_listenEp = null; 
     public static EndPoint m_connectedClientEp = null; 
     public static IPEndPoint m_sendEp = null; 
     public static Socket m_UdpListenSocket = null; 
     public static Socket m_UdpSendSocket = null; 


     static void Main(string[] args) 
     { 

      // Creates Listener UDP Server 
      m_listenEp = new IPEndPoint(IPAddress.Any, 7900); 
      m_UdpListenSocket = new Socket(m_listenEp.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); 
      m_UdpListenSocket.Bind(m_listenEp); 

      //Connect to zone IP EndPoint 
      m_sendEp = new System.Net.IPEndPoint(IPAddress.Parse("REMOTE_IP_GOES_HERE"), 7900); 
      m_connectedClientEp = new System.Net.IPEndPoint(IPAddress.Any, 7900); 

      byte[] data = new byte[1024]; 

      while (true) 
      { 
       if (m_UdpListenSocket.Available > 0) 
       { 

        int size = m_UdpListenSocket.ReceiveFrom(data, ref m_connectedClientEp); //client to listener 

        if (m_UdpSendSocket == null) 
        { 
         // Connect to UDP Game Server. 
         m_UdpSendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
        } 

        m_UdpSendSocket.SendTo(data, size, SocketFlags.None, m_sendEp); //listener to server. 

       } 

       if (m_UdpSendSocket != null && m_UdpSendSocket.Available > 0) 
       { 
        int size = m_UdpSendSocket.Receive(data); //server to client. 

        m_UdpListenSocket.SendTo(data, size, SocketFlags.None, m_connectedClientEp); //listner 

       } 
      } 


      // Wait for any key to terminate application 
      Console.ReadKey(); 
     } 
    } 
}