StreamCorruptedException when connecting to a socket application over internet <p>I have created a chat room application using java sockets which worked well over the LAN at my class. But when I tried to connect it over the internet a StreamCorruptedException was thrown. When the error was thrown both client and server are connected to the internet using HSPA Modems(Because some one told me socket app will not work over ADSL). I searched for an answer and unfortunately I cannot fix this problem. Can you please look into this?</p> <p>Here's my code.</p> <p>1). Server :</p> <pre><code>import java.io.*; import java.util.*; import java.net.*; public class MyServer{ ArrayList&lt;ClientListener&gt; ctr; String uname; private static MyServer instance; public static void main(String asrgs[]){ MyServer server=MyServer.createInstance(); server.start(); } private MyServer(){ } public static MyServer getInstance(){ return instance; } public static MyServer createInstance(){ if(instance==null){ instance=new MyServer(); return instance; }else{ System.out.println("Server is already running"); return instance; } } public void broadcast(String msg, String uname){ for(int i=0;i&lt;ctr.size();i++){ try{ if(ctr.get(i).uname!=uname){ ObjectOutputStream oostream=ctr.get(i).getOOS(); oostream.writeObject(msg+","+uname); } }catch(IOException ioE){ ioE.printStackTrace(); } } } public void start(){ //Start Server ctr=new ArrayList&lt;ClientListener&gt;(); ServerSocket myServer=null; try{ myServer=new ServerSocket(5008); }catch(Exception e){ System.out.println(e); System.exit(0); } //accept requests Socket socket=null; try{ while(true){ System.out.println("Waiting for a request..."); socket=myServer.accept(); ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream ois=new ObjectInputStream(socket.getInputStream()); InetAddress inet=socket.getInetAddress(); uname=inet.getHostName(); ClientListener ct=new ClientListener(oos,ois,uname); ctr.add(ct); new Thread(ct).start(); System.out.println("User Connected"); //new ChatRoom(oos,ois).setVisible(true); } }catch(IOException e){ System.out.println("No client found.."); } } } </code></pre> <p>2) Client :</p> <pre><code>import java.net.*; import java.io.*; import java.util.*; class MyClient{ public static void main (String[] args){ Socket socket=null; try{ socket=new Socket(args[0],5008); System.out.println("server connected...."); ObjectInputStream ois=new ObjectInputStream(socket.getInputStream()); ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream()); new ChatRoom(oos,ois).setVisible(true); System.out.println("Initiated...."); }catch(IOException e){ // System.exit(0); e.printStackTrace(); } } } </code></pre> <p>3). ClientListener :</p> <pre><code>import java.io.*; import java.util.*; import java.net.*; class ClientListener implements Runnable{ private Socket socket; ObjectOutputStream oos; ObjectInputStream ois; String uname; public ClientListener(Socket socket){ this.socket=socket; } public ClientListener(ObjectOutputStream oos, ObjectInputStream ois, String uname){ this.oos=oos; this.ois=ois; this.uname=uname; } public void run(){ Listen(); } private void Listen(){ while(true){ try{ String msg=(String)ois.readObject(); this.Write(msg,uname); }catch(IOException e){ //JOptionPane.showMessageDialog(this,e.getMessage()); }catch(ClassNotFoundException e){ //JOptionPane.showMessageDialog(this,e.getMessage()); } } } private void Write(String msg,String uname){ MyServer s=MyServer.getInstance(); s.broadcast(msg,uname); } public ObjectOutputStream getOOS(){ return this.oos; } public ObjectInputStream getOIS(){ return this.ois; } } </code></pre> <p>4). ChatRoom(GUI) for Clients:</p> <pre><code>import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; class ChatRoom extends JFrame implements Runnable{ ObjectOutputStream oos; ObjectInputStream ois; JTextArea textArea; JTextField msgText; JButton sendButton; JMenuBar menubar; JMenu menu; ChatRoom(ObjectOutputStream oos, ObjectInputStream ois){ this.oos=oos; this.ois=ois; setSize(400,500); setResizable(false); setDefaultCloseOperation(3);//EXIT_ON_CLOSE setTitle("iChat"); textArea=new JTextArea(); textArea.setEditable(false); JScrollPane textPane=new JScrollPane(textArea); add("Center",textPane); JPanel southPane=new JPanel(new GridLayout(1,2)); msgText=new JTextField(); msgText.requestFocus(); msgText.addKeyListener(new KeyAdapter(){ public void keyReleased(KeyEvent evt){ if(evt.getKeyCode()==KeyEvent.VK_ENTER){ writeMessage(); } } }); southPane.add(msgText); sendButton=new JButton("Send"); sendButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ect){ writeMessage(); } }); southPane.add(sendButton); add("South",southPane); new Thread(this).start(); } private void writeMessage(){ String msg=msgText.getText(); try{ oos.writeObject(msg); textArea.append("Me : "+msg+"\n"); }catch(IOException e){ JOptionPane.showMessageDialog(this,e.getMessage()); } msgText.setText(null); } public void run(){ while(true){ try{ String token=(String)ois.readObject(); int marker=token.indexOf(","); String msg=token.substring(0,marker); String uname=token.substring(marker); textArea.append(uname+" : "+msg+"\n"); }catch(IOException e){ JOptionPane.showMessageDialog(this,e.getMessage()); }catch(ClassNotFoundException e){ JOptionPane.showMessageDialog(this,e.getMessage()); } } } } </code></pre>