How to set up socket between Android App (client) and Hercules Server

  android, java, tcp, tcpclient

https://www.coderzheaven.com/2017/05/01/client-server-programming-in-android-send-message-to-the-client-and-back/

I copied the Client’s code into an android app from this tutorial, and this is what happens.
When I click connect to server on the client side, all that happens is the client number increases in the Hercules window, but nothing else happens, other than the two messages updating with the time on the app

client count

app messages

When I try to send a message from the Hercules terminal to the app, nothing happens either, but the sent data window updates in Hercules

Java code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


public static final int SERVERPORT = 11111111; // Obviously this is my real port #

public static final String SERVER_IP = "xxx.xxx.x.xxx"; // Obviously this is my real IP
private ClientThread clientThread;
private Thread thread;
private LinearLayout msgList;
private Handler handler;
private int clientTextColor;
private EditText edMessage;

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

    setTitle("Client");
    clientTextColor = ContextCompat.getColor(this, R.color.colorPrimaryDark);
    handler = new Handler();
    msgList = findViewById(R.id.msgList);
    edMessage = findViewById(R.id.edMessage);
}

public TextView textView(String message, int color) {
    if (null == message || message.trim().isEmpty()) {
        message = "<Empty Message>";
    }
    TextView tv = new TextView(this);
    tv.setTextColor(color);
    tv.setText(message + " [" + getTime() + "]");
    tv.setTextSize(20);
    tv.setPadding(0, 5, 0, 0);
    return tv;
}

public void showMessage(final String message, final int color) {
    handler.post(new Runnable() {
        @Override
        public void run() {
            msgList.addView(textView(message, color));
        }
    });
}

@Override
public void onClick(View view) {

    if (view.getId() == R.id.connect_server) {
        msgList.removeAllViews();
        showMessage("Connecting to Server...", clientTextColor);
        clientThread = new ClientThread();
        thread = new Thread(clientThread);
        thread.start();
        showMessage("Connected to Server...", clientTextColor);
        return;
    }

    if (view.getId() == R.id.send_data) {
        String clientMessage = edMessage.getText().toString().trim();
        showMessage(clientMessage, Color.BLUE);
        if (null != clientThread) {
            clientThread.sendMessage(clientMessage);
        }
    }
}

class ClientThread implements Runnable {

    private Socket socket;
    private BufferedReader input;

    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);

            while (!Thread.currentThread().isInterrupted()) {

                this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String message = input.readLine();
                if (null == message || "Disconnect".contentEquals(message)) {
                    Thread.interrupted();
                    message = "Server Disconnected.";
                    showMessage(message, Color.RED);
                    break;
                }
                showMessage("Server: " + message, clientTextColor);
            }

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

    void sendMessage(final String message) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (null != socket) {
                        PrintWriter out = new PrintWriter(new BufferedWriter(
                                new OutputStreamWriter(socket.getOutputStream())),
                                true);
                        out.println(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}

String getTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    return sdf.format(new Date());
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (null != clientThread) {
        clientThread.sendMessage("Disconnect");
        clientThread = null;
    }
}

}

Source: Android Questions

LEAVE A COMMENT