0

I am developing an Android application, and I need to send a message from the application to the Java Server.

Java Server works like this:

thread = new Thread(){
            public void run(){

                System.out.println("Server is running...");

                try {
                    ServerSocket socket = new ServerSocket(7000);
                    while(true){
                        Socket s = socket.accept();
                        DataInputStream dis = new DataInputStream(s.getInputStream());
                        System.out.println("Received from client: " + dis.readUTF());
                        dis.close();
                        s.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        thread.start();

In my application I send the message in this way:

mt = new Thread() {
            public void run() {

                try {
                    Socket socket = new Socket("192.168.1.100", 7000);
                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                    dos.writeUTF(song_field.getText().toString());
                    dos.flush();
                    dos.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        mt.start();
        Toast.makeText(context, "Your Message is sent. Thank you!", Toast.LENGTH_SHORT).show();

I can send the message with emulator and my phone successfully, since they are connected to the same wifi connection, but if the device is not connected to the same network, message is not sent to the server. I want everybody to be able to send message to my computer server regardless of their internet connection.

How can I fix this problem?

1 Answer 1

1

In general you'll need to use something like Web Sockets to achieve what you're trying to do where, as would typically be the case, client/server are on different networks. There are a few different Web Socket implementations e.g. https://medium.com/square-corner-blog/web-sockets-now-shipping-in-okhttp-3-5-463a9eec82d1#.w9hrc1icw

EDIT I initially misread question and thought you were trying to asynchronously send message from server to client (which would require something like Web Sockets). If you are just making requests from client to server then a typical solution would be to expose REST API from your server (and using something like Retrofit to make requests from client).

Sign up to request clarification or add additional context in comments.

3 Comments

I see Sir, Well, I don't own any server or website. Can I create my own server with Java without paying any money to receive messages from my application.
You can use something like Google App Engine - cloud.google.com/appengine/docs/java
Sorry for the late "accept the answer". You helped me Sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.