Good day.

Client (Android) - Server (Java)

With the help of sockets I try to connect to the server.

If the variable value is successfully sent from the emulator (server accepts), then when I try to repeat on a real phone, the server does not receive anything. Just silence. Permission to the Internet is, and not only on the Internet, and on Wi-Fi and everything else.

Server:

public class Server { static ServerSocket ss; static Socket s; static DataInputStream dis; public static void main(String[] args){ Thread thread = new Thread(new Runnable(){ @Override public void run() { System.out.println("Server get started:\n"); try{ ss = new ServerSocket(80); while(true){ s = ss.accept(); dis = new DataInputStream(s.getInputStream()); System.out.println("Received: " + dis.readUTF()); dis.close(); s.close(); } } catch(Exception ex){ex.printStackTrace();} } }); thread.start(); } 

Customer:

 public class MainMenu extends Activity { Button btn_start; TextView welcome_text_view; public static String name = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText et = new EditText(MainMenu.this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); et.setPadding(16, 0, 0, 16); et.setLayoutParams(lp); AlertDialog.Builder inputNameDialog = new AlertDialog.Builder(MainMenu.this); inputNameDialog .setTitle("You are welcome!") .setMessage("Input your name:") .setPositiveButton("Go!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { name = et.getText().toString(); if(!name.equals("")) welcome_text_view.setText("Hello, " + name + "!"); new Connecter1().execute(); } }) .setView(et) .create(); inputNameDialog.show(); welcome_text_view = (TextView) findViewById(R.id.welcome_text_view); btn_start = (Button) findViewById(R.id.btn_start); btn_start.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { Intent intent = new Intent(MainMenu.this, Game.class); startActivity(intent); GamePanel.speed = 10; GamePanel.score = 0; } }); } private class Connecter1 extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... voids) { try { Socket s = new Socket("Мой ip", 80); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(name); dos.flush(); dos.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } return null; } } 

When ip 192.168 was installed on the emulator ... local, in short - everything worked. Now, when I run on a real device, I changed the ip, recognizing it on 2ip. On a real device, data is transmitted via wi-fi, if that matters.

So what's the problem after all?

  • Do you have wi-fi on your phone? - Lucky_girl
  • Yes, wi-fi is on - Crok

1 answer 1

Use HttpUrlConnection , which sockets?

  • An example can throw? And this connection will also need to be implemented in a separate thread? - Crok