2016-03-25 26 views
0

Android telefonumu bir Sunucu ile bağlamaya çalışıyorum. Sunucuda C# ile yazılmış bir program çalıştırır. Aşağıdaki kodla yapmaya çalıştım ama işe yaramıyor. Bu benim kod yorumladı ama çalışmıyor kullanmak da uğraş ettikAndroid bir sunucu ile soket bağlantısı

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Socket socket; 
    int port = 52301; 
    String ip = "79.16.115.30"; 
    Button b = (Button) findViewById(R.id.myButton); 
    assert b != null; 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       byte[] b = ip.getBytes(); 
       /*SocketAddress socketAddress = new InetSocketAddress(ip, port); 

       socket.connect(socketAddress);*/ 
       socket = new Socket(ip, port); 
       Toast.makeText(v.getContext(), "CONNESSO", Toast.LENGTH_SHORT).show(); 
      } catch (UnknownHostException e) { 
       Toast.makeText(v.getContext(), "CHI MINCHIA è COSTUI", Toast.LENGTH_SHORT).show(); 
      }catch(IOException e) { 
       Toast.makeText(v.getContext(), "NO I/O", Toast.LENGTH_SHORT).show(); 
      } 
      catch (Exception e) 
      { 
       String cause = "Message: " + e.getMessage(); 
       Toast.makeText(v.getContext(), cause, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 

Android kodudur; Her şeyden

static void Main(string[] args) 
    { 
     string ip = Get_external_ip(); 
     int port = 52301; 
     int max = 10; 
     IPAddress ipAddress = IPAddress.Parse("192.168.1.56"); 
     IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);           //setto i parametri del socket 
     Socket sockServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //creo il socket 
     Socket client; 

     Console.WriteLine(ip); 
     try 
     { 
      sockServer.Bind(localEndPoint); 
      sockServer.Listen(max); 
      while (true) 
      { 
       client = sockServer.Accept(); 
       Console.WriteLine(client.RemoteEndPoint.ToString()); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 

     Console.ReadLine(); 
    } 

    private static string Get_external_ip() 
    { 
     try 
     { 
      WebClient client = new WebClient(); 
      return client.DownloadString("http://icanhazip.com/").TrimEnd('\r', '\n'); 
     } 
     catch (Exception e) 
     { 
      return e.Message; 
     } 
    } 

cevap

0

Birincisi, ağ için arka plan iş parçacığı kullanmak:

Bu

Server üzerinde çalışan bir programdır.

Size çalışma parçacığımı veriyorum. İhtiyaçlarınız için uygulamayı deneyin.

public class YourClass { 
//Server socket variables 
private ServerSocket serverSocket; 
private DataOutputStream dataOutputStream; 
private DataInputStream dataInputStream; 
private Socket client; 
.... 

private class SocketServerThread extends Thread { 

    private int port; 

    private SocketServerThread(int port) { 
     this.port = port; 
    } 

    @Override 
    public void run() { 
     dataInputStream = null; 
     dataOutputStream = null; 

     try { 
      serverSocket = new ServerSocket(port); 
      isSockedOpened = true; 

      client = serverSocket.accept(); //wait for client 
      isClientConnected = true; 

      Log.d(TAG, "Client connected: " + client.getInetAddress().getHostAddress()); 

      dataInputStream = new DataInputStream(
        client.getInputStream()); 

      dataOutputStream = new DataOutputStream(
        client.getOutputStream()); 

      String messageFromClient; 
      while (isClientConnected) { 

       messageFromClient = dataInputStream.readLine(); 

       handleIncomingMessage(messageFromClient); 
      } 
      } 
     } catch (IOException e) { 
      Log.e(TAG, e.getMessage()); 

     } catch (NullPointerException e) { 
      Log.e(TAG, e.getMessage()); 
     } 
    } 
} 

bağlantıyı açmak için:

istemci bağlandığında olmadığını kontrol nasıl
public void connect(int port) { 
    this.port = port; 

    Thread socketServerThread = new Thread(new SocketServerThread(port)); 
    socketServerThread.start(); 
}  

;

public boolean isConnected() { 
    return serverSocket.isBound() || client.isConnected(); 
} 

bağlantısını kesmek için:

public void disconnect(boolean needsGathering) { 
    if (isSockedOpened) { 
     try { 
      if (serverSocket != null) { 
       serverSocket.close(); 
      } 

      if (dataOutputStream != null) { 
       dataOutputStream.close(); 
      } 

      if (dataInputStream != null) { 
       dataInputStream.close(); 
      } 

      if (client != null) { 
       client.close(); 
      } 

     } catch (IOException e) { 
      Log.e(TAG, "Couldn't get I/O for the connection"); 
      Log.e(TAG, e.getMessage()); 
     } 
    } else { 
     Log.d(TAG, "Socket was not opened"); 
    } 
} 
} 
İlgili konular