Connecting Android socket client to C socket server

I want to send some data over an android app to a c server.

I have followed this tutorial to get the basic framework.

I replaced the following code

        socket = new Socket(dstAddress, dstPort);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
        byte[] buffer = new byte[1024];

        int bytesRead;
        InputStream inputStream = socket.getInputStream();

         * notice: will block if no data return
        while ((bytesRead = != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
            response += byteArrayOutputStream.toString("UTF-8");

with my own

     System.out.println("Connecting to " + serverName + " on port " + port);
     Socket client = new Socket(serverName, port);
     System.out.println("Just connected to " + client.getRemoteSocketAddress());

     PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));

     outToServer.println("Hello from " + client.getLocalSocketAddress());

     BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
     System.out.println("Server Says : " + in.readLine());

I tested my java code and it correctly sends and receives data, but when I try to do the same in android, I would get stuck at

Socket client = new Socket(serverName, port);

I can see from the Android Monitor it prints

I/System.out: Connecting to on port 5001

And then it hangs.

Source: Active questions tagged c – Stack Overflow

Powered by WPeMatico