Good morning.

There is a client on the android, it should go from the line to the server. I launch the client on a real device.

Here is the client code:

public class Client extends Thread { private boolean running = false; private static Socket s = null; private String ip = "тут мой ip"; private int port = 80; private String data; static DataInputStream din = null; static DataOutputStream dout = null; public Client(String name, int score){ data = name + " " + score; } public void setRunning(boolean b){running = b;} @Override public void run() { while (running) { try { s = new Socket(ip, port); dout = new DataOutputStream(s.getOutputStream()); din = new DataInputStream(s.getInputStream()); dout.writeUTF(data); } catch (Exception ex) {} } } 

}

All this action should work in a different class, in this method:

 private void sendData(String name, int score){ client = new Client(name, score); client.setRunning(true); client.start(); } 

Server code:

 public class Server { static ServerSocket ss; static Socket s; static DataInputStream din; static DataOutputStream dout; static String data = ""; public static void main(String[] args){ try{ System.out.println("Сервер запущен\n"); ss = new ServerSocket(80); s = ss.accept(); din = new DataInputStream(s.getInputStream()); dout = new DataOutputStream(s.getOutputStream()); while(true){ data = din.readUTF(); System.out.println("Score: " + data); } } catch(Exception ex){ex.printStackTrace();} System.out.println("Сервер остановлен"); } 

}

Wi-fi on the device turned on, ip is exactly mine, everything in theory should work. The client in a separate thread, I do not understand then what is the matter. Tell me please.

  • Port 80 is better not to use. Try with the port number above 1024 - Maxim Drobyshev
  • And why do you constantly connect to the server in a loop? And then do not close the connection. That is, it turns out as soon as you start a thread, it loops. At the same time it connects to the server. And does not transmit anything. Since data == null - Maxim Drobyshev
  • Well, I'll clean the loop, only as you understand that data == null, I initialized the data in the constructor before launching the stream - Crok
  • Now port 1234, only nothing has changed, still nothing comes to the server - Crok
  • First of all, in spite of your code, I recommend checking access to the port from the local network, and then already from the device, after running your server of course, if everything is okay, then watch the code. - avengerweb

1 answer 1

On the client side:

Client.java:

 import java.util.*; import java.net.*; import java.io.*; public class Client extends Thread { //Все null,false,0 по умолчанию. Можно не писать. private boolean running; private Socket s; private String ip = "192.168.1.4"; private int port = 1234; static DataInputStream din; static DataOutputStream dout; public void setRunning(boolean b){running = b;} @Override public void run() { try { s = new Socket(ip, port); dout = new DataOutputStream(s.getOutputStream()); din = new DataInputStream(s.getInputStream()); while (running); } catch (Exception ex) { ex.printStackTrace(); } finally { close(); } } public boolean isConnected() { return s!=null&&s.isConnected(); } public void sendMessage(String message) { if(dout!=null&&running) { try { dout.writeUTF(message); dout.flush(); } catch(Exception ex) { running = false; } } } public void close() { if(din!=null) try{din.close();}catch(IOException io){} if(dout!=null) try{dout.close();}catch(IOException io){} if(s!=null) try{s.close();}catch(IOException io){} } } 

Class call:

 Client clt = new Client(); clt.setRunning(true); clt.start(); while(!clt.isConnected()); clt.sendMessage("Hello"); clt.sendMessage(", World!"); clt.close(); 

Manifesto:

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 

On the server side:

Server.java:

 static ServerSocket ss; static Socket s; static DataInputStream din; static DataOutputStream dout; static String data = ""; public static void main(String[] args){ try{ System.out.println("Сервер запущен\n"); ss = new ServerSocket(1234); s = ss.accept(); din = new DataInputStream(s.getInputStream()); dout = new DataOutputStream(s.getOutputStream()); while((data = din.readUTF())!=null){ System.out.println("Score: " + data); } } catch(Exception ex){ ex.printStackTrace(); } finally { if(dout!=null) try{dout.close();}catch(IOException io){} if(din!=null) try{din.close();}catch(IOException io){} if(s!=null) try{s.close();}catch(IOException io){} if(ss!=null) try{ss.close();} catch(IOException io){} } System.out.println("Сервер остановлен"); } 
  • Instead of your ip, I wrote 127.0.0.1, launched the server, launched the application on the emulator - nothing came. - Crok
  • Strange to me through the modem works. Emulators have a network issue. When I tested my chat in the game, I made sure that it does not always work. Did they arrive on a real device? - Maxim Drobyshev
  • and tried it on a real one, drove in my ip - it didn't work out - Crok
  • I think the problem is that the port on which I try to connect the server and the client is closed. The question is now different, is it possible to open the port in the code? - Crok
  • Can. The question may be stupid, but are you exactly connected to that IP? Do you learn ip via ipconfig or via 2ip.ru? You can forward. I even have code somewhere lying around. What is the OS on the PC? My server didn’t work on Linux somehow. - Maxim Drobyshev