'Creating a simple Hello world Android Client-Server program

To keep it simple, all I'm trying to do is to send a message from my Android Emulator to a Java console server on Eclipse that contains a simple string, then I want the server to reply with another string. Ofcourse; Ill connect to host 10.0.2.2 and use any open port (in my case 4000). The server gets a "connected" msg everytime I click on connecting button on the emulator, but I cant find a way to deliver packets or messages.

In case you want to read some code, here's my (what I think is) too long code for a simple task.

Client package com.example.clienttest;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    Thread m_objThreadClient;
    Socket clientSocket;
    TextView serverMessage;
    EditText clientMessage;
    String sIn, sOut;
    ObjectOutputStream oos;
    ObjectInputStream ois;

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

        serverMessage = (TextView) findViewById(R.id.textView1);
        clientMessage = (EditText) findViewById(R.id.editText1);

    }


    public void Start(View view) throws IOException{
        final String Text;
        String send = " ";
        Text = clientMessage.getText().toString();
        m_objThreadClient = new Thread( new Runnable(){
            public void run()
            {
                try {
                    clientSocket = new Socket("10.0.2.2", 4000);
                    oos = new ObjectOutputStream (clientSocket.getOutputStream());
                    oos.writeObject(Text);
                    oos.flush();

                } catch (IOException e) {
                    serverMessage.setText(e.getMessage());
                }

            }
        });
        serverMessage.setText(send);

        m_objThreadClient.start();

    }

}

and server

import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import java.util.Hashtable;

public class Server2 {

    @SuppressWarnings("resource")
    public static void main (String[] args) throws IOException {

        ServerSocket server = null;  
        try {
            server = new ServerSocket(4000); 
        } catch (IOException e) {
            System.err.println("Could not start up on: " + "4000" + "Maby server is already open? Or a portforwording messup?");
            System.err.println(e);
            System.exit(1);
        }

        Socket client = null;
        while(true) {
            try {
                client = server.accept();
                System.out.print("connected");
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.err.println(e);
            }

            Thread t = new Thread(new ClientConn(client));
            t.start();
        }
    }
}


class ClientConn implements Runnable {
    private Socket client;
    ObjectInputStream ois;
    ObjectOutputStream oos;

    ClientConn(Socket client) {
        this.client = client;
        try {
            ois = new ObjectInputStream(client.getInputStream());
            String Recv;
            Recv = ois.readLine();

            oos = new ObjectOutputStream(client.getOutputStream());
            oos.writeChars("Welcome");
            oos.flush();

            oos.close();

        } catch (IOException e) {
            System.err.println(e);
            return;
        }
    }

    public void run() {
        String msg, response;
        try {
            System.out.print("checking");
            while ((msg = ois.readLine()) != null) {
                System.out.print(msg);
            }
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public void sendMsg(String msg) throws IOException {
        oos.writeChars(msg);
    }
}

Thanks~



Solution 1:[1]

Some thoughts that may or may not solve the problem:

The method readLine() for the ObjectInputStream is deprecated. Try using a DataInputStream instead. Something like :

try {
        outToClient = new DataOutputStream(socket.getOutputStream());
        inFromClient = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

And then :

inFromClient.readLine();

Note that the read line will continue to read until it encounters a new line character '\n'. So readLine() will block until it reads a newline.

Try these things and see if they help

EDIT: It appears readLine() for DataInputStream is deprecated as well, but there are several others methods that should work. For example readUTF()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BenMorel