0

This could just be me being stupid, but I'm opening up two different applications on my computer that connect to the same port of a server running on the computer. Now, it's fine with one application connected, but once the other one is launched it starts throwing a bunch of exceptions about java.io.StreamCorruptedException: invalid type code: AC.. Is this because I'm trying to have the 1 server + 2 clients running on the same machine?

Edit: Code:

Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),12352);
SocketThread sthread = new SocketThread(socket);
...
sthread.start();

Inside the socket thread:

 this.ois = new ObjectInputStream(socket.getInputStream()); <-- ois is a variable for the ObjectInputStream
 ...
 Packet packet = (Packet) ois.readObject(); <-- Error appears on this line.

Edit #2: Second - server code:

while(true)
{
        Socket player = server.accept();
        System.out.println(player.getRemoteSocketAddress().toString() + " connected.");
        startSocket(player);
}

public static void startSocket(Socket player) throws IOException
{
    new SocketThread(player).start();
}

Now, there is no errors on the server.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • No, multiple clients should be able connect to a server. (Think about this site, StackOverflow. You don't think there's only one person connected at a time, do you?) The server side of the code is badly written if it throws errors when it gets more than one connection. – markspace Jul 26 '14 at 22:47
  • Well, when I try to launch two clients, which connect with seperate sockets, and are completely seperate applications, they will both get the above error about 'invalid type code', which appears when doing ObjectInputStream.readObject() after opening the second version of the application. (This happens in both applications.) – VoidWhisperer Jul 26 '14 at 22:56

1 Answers1

0

No, any number of clients should be able to connect to a single server.

One of my implementation has code looks a bit like this. And I'm able to connect any number of clients to this server from local as well as remote machines.

ServerSocket soc = new ServerSocket(port);

while (true) {
  try {
    if (StopService) {
      soc.close();
      break;
    }

    Socket s = soc.accept();
    new Thread(new ConnectionHandler(s)).start(); 
  } catch (SocketTimeoutException ex) {
  } catch (IOException ex) {
    ex.printStackTrace();
  }
}

Just make sure that after accepting connection you have it sent to a separate thread for handling so that your server socket thread can accept() the next connection if its waiting.

Those errors do sound a bit weird. If you already have multiple threads going, maybe when you accept multiple connections both threads try to unintentionally modify something in a shared object or maybe one thread somehow gets access to the other thread's socket and manipulates it in unexpected way.

If you could shed some light on your code responsible for creation of sockets what the bit where the error actually occurs, a better answer can be given.

EDIT:

Sorry my rep does not permit to add comments yet.

How about the Server code which sends the data to client.

klandaika
  • 357
  • 3
  • 11
  • The error seems to be caused by how object is being written to stream. How does the server write the object to stream to send to the client? – klandaika Jul 26 '14 at 23:31
  • Well, I realize my incredibly stupid error now... Great... I think. I was creating a new objectoutputstream on something that already had one. – VoidWhisperer Jul 26 '14 at 23:32
  • Glad our discussion got you to your answer. – klandaika Jul 26 '14 at 23:33
  • @VoidWhisperer Well it isn't really. It's all correct, but the answer to your specific problem is as you said: don't open a second ObjectOutputStream. – user207421 Jul 27 '14 at 05:43