1

I'm trying to send an Object from my phone to my PC(Windows) using a TCP socket via WiFi. When I try the same code between two PCs, it works without any error. But when I put the client code to the android device, it fails to send date using writeObject method. But writeUTF command works. It gives the "Software caused connection abort: recv failed" error. Below is the Code. Please help..

Server(in PC):

public class Test {

public static void main(String arg[]) {
    ServerSocket serverSocket = null;
    Socket socket = null;
    ObjectInputStream in = null;
    ObjectOutputStream out = null;


    try {
        serverSocket = new ServerSocket(8888);
        System.out.println("Listening :8888");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while (true) {
        try {
            socket = serverSocket.accept();
            in = new ObjectInputStream(socket.getInputStream());
            out = new ObjectOutputStream(socket.getOutputStream());
            out.flush();

            System.out.println("ip: " + socket.getInetAddress());
            Message msg = (Message) in.readObject();  //Message captured from chat client.
            System.out.println(msg.type + " message received from " + msg.sender + " Containing " + msg.content);
            out.writeObject(new Message("Ack", "Server", "Message Received", "Client"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } 



        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
}

Client (in Android Device):

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bb=(Button)findViewById(R.id.button1);

    bb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new Send().execute();

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
 private class Send extends AsyncTask<Void, Void, Void> {
        Socket socket = null;
        ObjectOutputStream out = null;
        ObjectInputStream in = null;

     protected Void doInBackground(Void... arg0) {

            try {
                socket = new Socket("192.168.43.92", 8888); //use the IP address of the server
                out = new ObjectOutputStream(socket.getOutputStream());
                out.flush();

                out.writeObject(new Message("Chat", "Server", "Hello World", "Server")); //This method is used to write something to the server.
                out.flush();

                Message msg = (Message) in.readObject();
                System.out.println(msg.type + " message received from " + msg.sender + " Containing " + msg.content);


            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            finally {

                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }            





         return null;            
     }

     protected void onProgressUpdate(Integer... progress) {
         //setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         //showDialog("Downloaded " + result + " bytes");
     }
 }

}

Message(in Both Sides):

public class Message implements Serializable{

private static final long serialVersionUID = 1L;
public String type, sender, content, recipient;

public Message(String type, String sender, String content, String recipient){
    this.type = type; this.sender = sender; this.content = content; this.recipient = recipient;
}

@Override
public String toString(){
    return "{type='"+type+"', sender='"+sender+"', content='"+content+"', recipient='"+recipient+"'}";
}
}
  • Duplicate of [Official reasons for "Software caused connection abort: socket write error"](http://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error) – user207421 Dec 04 '13 at 05:48

2 Answers2

0

Is the network between the client and server setup properly via your WiFi? Download one of those ping & telnet test apps and use it to test your network connection.

Telnet is a useful TCP debugging app. If you have a server listening on 11.22.33.44 port 1234, you should be able to telnet 11.22.33.44 1234

gerrytan
  • 40,313
  • 9
  • 84
  • 99
-1

Maybe, you need to add this functions into Message class:

private void writeObject(java.io.ObjectOutputStream stream)
         throws IOException {
     stream.writeObject(type);
     stream.writeObject(sender);
     stream.writeObject(content);
     stream.writeObject(recipient);
 }


 private void readObject(java.io.ObjectInputStream stream)
         throws IOException, ClassNotFoundException {
     type = (String) stream.readObject();
     sender = (String) stream.readObject();
     content = (String) stream.readObject();
     recipient = (String) stream.readObject();

 }

http://developer.android.com/reference/java/io/Serializable.html

código
  • 53
  • 2
  • 5
  • Completely unnecessary given the existing definition of his `Message` class. This is not even as good as what happens automatically, and it's a network problem, not a Serialization problem. -1 – user207421 Dec 04 '13 at 06:06