C# - Android - 套接字问题

时间:2014-05-02 08:51:13

标签: c# android .net sockets

我正在构建一个客户端 - 服务器应用程序,我遇到了端口问题 - 防火墙已关闭。

我打开了端口路由器,当客户端尝试与应用服务器通信时,一切运行顺利,但在此之后,当服务器尝试与客户端进行通信时,则存在问题。

例如,客户端从:80.246.32.76到服务器:80.236.42.57端口6000,直到现在一切正常。

之后,客户端打开端口5000,当服务器尝试与端口5000中的客户端进行通信时,不再有效。

此服务器代码(C#):

public static void Receiver_Socket (int Port = -1, string Message = "")
 {
  IPEndPoint localEndPoint = new IPEndPoint (IPAddress.Parse ("10.100.102.7"), 6000);
  listener = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  listener.Bind (localEndPoint);
  listener.Listen ( 100 ) ;
  Receiver = listener.Accept ();

  IP_Connected_Address = Receiver.RemoteEndPoint.ToString ();
 }

public static void Sender_Socket (string IP_Address = "", int Port = -1)
 {
  string IP_Connected_Address_Neto = IP_Connected_Address.Substring (0, IP_Connected_Address.IndexOf (":", 0));

  IPEndPoint remoteEP = new IPEndPoint (IPAddress.Parse (IP_Connected_Address_Neto), 5000));
  Sender = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

  Sender.Connect (remoteEP);//there is the problem in C# - Server side
 }

这是客户端的代码(Android):

class Connect_To_Server implements Runnable
 {
  @ Override
  public void run ( )
   {
    try
     {
      InetAddress serverAddr = InetAddress.getByName ("80.236.42.57");
      Sender_Socket = new Socket (serverAddr, 6000);
     }
    catch (UnknownHostException e1)
     {
      e1.printStackTrace ();
     }
    catch ( IOException e1 )
     {
      e1.printStackTrace ();
     }
    Connected_To_Server = true ;
   }
 }

class Connect_From_Server implements Runnable
 {
  @ Override
  public void run ( )
   {
    try
     {
      Receiver_Socket = new ServerSocket (5000);
     }
    catch ( IOException e )
     {
      / / TODO Auto- generated catch block
      e.printStackTrace ();
     }
    try
     {

      Receiver_Socket_Accepter = Receiver_Socket.accept ();//there is the problem in android - Client side

     }
    catch (UnknownHostException e1)
     {
      e1.printStackTrace ();
     }
    catch ( IOException e1 )
     {
      e1.printStackTrace ();
     }
   }
 }

0 个答案:

没有答案