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.