LaVOZs

The World’s Largest Online Community for Developers

'; Errno 32 Broken pipe when Python server sends data to Java client - LavOzs.Com

I have written a Java program that connects to a server and waits for the server to send something. The problem is that in the server there is this error message when it tries to send data:

connection is closed with this error [Errno 32] Broken pipe

In my Java code I have a loop to make the client retry connecting if the server is not ready. Can this loop cause the error?

I also tried to use readUTF, but then I got this error:

W/System.err: java.io.EOFException
W/System.err:     at libcore.io.Streams.readFully(Streams.java:83)
        at java.io.DataInputStream.readFully(DataInputStream.java:99)
        at java.io.DataInputStream.decodeUTF(DataInputStream.java:178)
        at java.io.DataInputStream.decodeUTF(DataInputStream.java:173)
        at java.io.DataInputStream.readUTF(DataInputStream.java:169)
        at com.example.info.MainActivity$1Serveur$2.run(MainActivity.java:185)

This is my Java client code:

final Thread receivingserveur = new Thread() {
    public void run() {
        int i=0;
        while(i<3) {//loop that retry if he fail to connect but i don't think that it's the best way to do it.
            try {
                System.out.println( "start client" );
                final Socket sock2 = new Socket( "192.168.1.32", 1234 );

                sock2.setTcpNoDelay( true );

                dis = new DataInputStream( sock2.getInputStream() );
                Byte message_entrant21;

                    message_entrant21 = dis.readByte();// i think that this line is waiting to receive something because the code that is after his executed only after the python code close the socket
                    i=4;// don't need to retry so but i biger than 3
                    handler.post( new Runnable() {

                        @Override
                        public void run() {
                            TextView tv = (TextView) findViewById( R.id.textView4 );
                            tv.setMovementMethod( LinkMovementMethod.getInstance() );
                            tv.setText( Html.fromHtml( String.valueOf( sock2.isBound() )+String.valueOf( sock2.isConnected() ) ) );

                        }
                    } );

                handler.post( new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText( getApplicationContext(), " received data", Toast.LENGTH_SHORT ).show();
                    }
                } );

                Thread.sleep(1000);
                handler.post( new Runnable() {
                    @Override
                    public void run() {
                        TextView tv = (TextView) findViewById( R.id.textView4 );
                        tv.setMovementMethod( LinkMovementMethod.getInstance() );
                        tv.setText( Html.fromHtml( "close sock" ) );
                        Toast.makeText( getApplicationContext(), "close socket", Toast.LENGTH_LONG ).show();
                    }
                } );
                sock2.close();

            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i += 1;//he failed to connect so retry
        }
    }
};

And this is my Python server code:

import socket

def client(data):
    # coding: utf-8

    socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket.bind(('', 1234))
    print("creat a serveur")

    socket.listen(5)
    print("listen")
    client, address = socket.accept()
    print(client, address)
    print "{} connected".format( address )
    try:
            socket.sendall('b\hello')
    except Exception as e:
            print(e)

    print "Close"
    client.close()
    socket.close()
Related
Real differences between “java -server” and “java -client”?
JVM_Bind error when client socket is closed by the server and then new socket opened by the client on the same port
How to prevent errno 32 broken pipe?
Java : Client-Server Simple Application with IP Blacklist
Server socket finishes when client closes connection
Java server-client game
setText on button from another activity android
Android client PC server java socket connection fail