0

I want to develop app that connect to a server and send and receive message. i'm really beginner in that. So,i wrote this code by This tutorial, and it seem that i get some mistake with the port or ip address beacuse i didn't get the message to the console. My inspiration is the problem is in my router setting maybe

Here is my android code (Project android)

public class MainActivity extends Activity {

    Socket client;   
    PrintWriter printWriter; 
    EditText edIp,edPort,edMess;
    String message;
    int port = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            edIp = (EditText) findViewById(R.id.edIp);
            edPort = (EditText) findViewById(R.id.edPort);
            edMess= (EditText) findViewById(R.id.edMessage);

            edIp.setText("10.0.2.2");
            edPort.setText("4444");
    }


    public void onClick(View v){
        message = edMess.getText().toString();
        edMess.setText("");
        port = Integer.parseInt(edPort.getText().toString());

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                        client = new Socket(edIp.getText().toString(),port);

                        printWriter = new PrintWriter(client.getOutputStream());
                        printWriter.write(message);
                        printWriter.flush();
                        printWriter.close();
                } catch (Exception e) {
                    // TODO: handle exception
                }

            }
        }).start(); 

    }


}

(java aplication)

public class Main {


    public static void main(String[] args) throws IOException {
        Socket clientSocket = null;
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        System.out.println("Server started...");
            clientSocket = serverSocket.accept();

        } catch (IOException e) {
            System.err.println("error" + e);
        }

        Scanner in1 = new Scanner(clientSocket.getInputStream());
        String mess;

        while (true) {
            if(in1.hasNext()){
                mess = in1.nextLine();
                System.out.println("Client message : "+mess);
            }

        }

    }

}
Matan
  • 296
  • 5
  • 24
  • Are you running the client code in an emulator? Is your java server running on the same pc as your emulator? Did you request internet permission in the manifest? There will be messages/warnings/exception/errors in your logcat. Please show a relevant part. – greenapps Jun 22 '14 at 10:09
  • 1. no it's on my real device. 2. yes my java and my android code run in same coputer 3.I have all the permission needs 4.No error or exaption - but when i running the java console ,sometimes it give me "errorjava.net.BindException: Address already in use: JVM_Bind Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:23) – Matan Jun 22 '14 at 10:28
  • You have an impossible setup. First you state thst you use a real device. Then you state that server and android code run on the same pc. This is contradictory. Please explain your setup better. If you cannot bind for a second time then you try to start the server for a second time without having closed the first server socket before. Which code is on line 23? – greenapps Jun 22 '14 at 11:24
  • In line 23 is " Scanner in1 = new Scanner(clientSocket.getInputStream());"Sorry but I know I'm not clear. the reason is that I am in first time trying to do this thing and I have no idea how to begain and what the reasons it does not work. i copy and paste all my code in my question :( but i think i have wrong with the port and the host or somthing.. pleasr try to understand me – Matan Jun 22 '14 at 12:37
  • even with the emulator it doesn't working... – Matan Jun 22 '14 at 17:29
  • Well if you have a NullPointerException on line 23 in your servercode then it does not matter where the client is. Please post the logcat/stacktrace. You can put a return in the servers first catch block. Now you continue but clientSocket is null. You log that. What is the exception message? You should code ` "error: " + e.getMessage();` – greenapps Jun 22 '14 at 18:06
  • It given me = "Address already in use: JVM_Bind" – Matan Jun 22 '14 at 22:36
  • Well what do you thing the message means? The server is not allowed to listen on that port as already another server socket is listening on that port. That is of course your own server which you finished. But you did not close the socket so that socket is still active. Kill your emulator and restart. – greenapps Jun 22 '14 at 22:54
  • I did it . and my error is gone. but the app still not working. – Matan Jun 22 '14 at 22:56
  • If you only say `my app still not working` then who do you think can help you? Provide decent info. Start with the logcat. Name errors. How far does your app come? Which app? – greenapps Jun 23 '14 at 05:53
  • I think the code is good. the problem is with my router setting . i dont know how to set is right – Matan Jun 23 '14 at 12:06
  • If you really use 10.0.2.2 with your emulator then a router does not come in to play. – greenapps Jun 23 '14 at 12:11
  • I know ok i give up. you know where i can find a good tutorials and i try to learn it from start!? – Matan Jun 23 '14 at 23:47
  • If you only woul post the logcat/stacktrace here... – greenapps Jun 24 '14 at 06:33
  • There is noting wrong with your code. I just tried it. If `R.layout.activity_main` is ok, this means activity_main.xml contains all the right widgets then it should run with server in Eclipse and client in emulator. But anyhow just post the LogCat here. Why so reluctant? You wanted help isn't it? – greenapps Jun 24 '14 at 08:16

0 Answers0