Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems creating ObjectInputStream from Socket InputStream

843790Dec 10 2006 — edited Dec 11 2006
I'm not really sure what is going wrong so that's why I'm posting this here:


This is the 'offending code'
[edit]
Note:
After switching in and out initialization to first creating the ObjectOutStream it stalled on the oout.
Ofcourse I can get the InputStream and OutputStream normally (to their respective object types).
[edit]
	protected void InitializeObjectStreams() {
		try {
			oin = new ObjectInputStream(this.socket.getInputStream());
			oout = new ObjectOutputStream(this.socket.getOutputStream());

			keyin = new ObjectInputStream(keypin);
			keyout = new ObjectOutputStream(keypout);
	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
I have a client and a server which use the same method to create these streams. I start the server as soon as socket is accepted I send the socket to my object handling the whole communication thing. Before the communication thread starts running I'm making these ObjectStreams. However neither the client nor the server actually go beyond the call to:
oin = new ObjectInputStream(this.socket.getInputStream());
Ofcourse when I terminate either one of the programs it will continue (after an exception ofcourse). Now as far as I know socket.getInputStream() does not do any waiting on input; however it 'seems' to be doing just that.

This is the class that uses this method:
package net.alex.pong.game.networking;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import net.alex.pong.Commons;
import net.alex.pong.Controller;
import net.alex.pong.UserSettings;

public class PongClient extends PongStreamManager {

	public PongClient(Socket socket, Controller controller) {
		super(socket, controller);
	}

	public void run() {
		running = true;
		InitializePipes();
		InitializeObjectStreams();
		System.out.println("Client Thread Running");
		while (running) {
			Object recieved;
			try {
				if (this.state == ProtocolState.NOCONNECTION) {
					System.out.println("Sending HELO");
					sendObject(PongProtocol.HELO);
				}
				while ((recieved = oin.readObject()) != null) {
					handleRecievedObject(recieved);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		System.out.println("Client Thread Stopped");
	}
	
	@Override
	protected synchronized void handleProtocol(PongProtocol prot) {
		PongProtocol response;
		switch (this.state) {
		case NOCONNECTION:
			switch (prot) {
			case HELO:
				console.appendLine("Got Server Response...");
				this.state = this.state.getNextState();
				console.appendLine("Sending version string");
				response = PongProtocol.VERSION;
				response.setAttachment(Commons.version);
				sendObject(response);
				break;
			case ERR:
				console.appendLine("Error during initial hanshake");
				console.appendLine("Server replied: \""+prot.getAttachment()+"\"");
				sendObject(PongProtocol.DISC);
				this.controller.disconnect();
				break;
			}
			break;
		case HANDSHAKE:
			switch (prot) {
			case VERSION:
				console.appendLine("Connection accepted Server version: "+prot.getAttachment());
				response = PongProtocol.NAME;
				response.setAttachment(UserSettings.getUserSettingsInstance().getUsername());
				this.state = this.state.getNextState();
				sendObject(response);
				break;
			case ERR:
				console.appendLine("Client and server versions are incompatible");
				console.appendLine("Server returned \""+prot.getAttachment()+"\"");
				response = PongProtocol.DISC;
				sendObject(response);
				break;
			default:
				response = PongProtocol.ERR;
				response.setAttachment("Unknown Command For This State");
				break;
			}
			break;
		case INTRODUCTION:
			switch (prot) {
			case NAME:
				if (prot.getAttachment() == "") {
					response = PongProtocol.ERR;
					response.setAttachment("Username Can't Be Empty");
					console.appendLine("Server's Username Was Empty");
				} else {
					setUsername(prot);
					response = PongProtocol.READY;
					this.state = this.state.getNextState();
					console.appendLine("Recieved Server username: "+prot.getAttachment());
				}
				sendObject(response);
				break;
			default:
				response = PongProtocol.ERR;
				response.setAttachment("Unknown Command For This State");
				sendObject(response);
				break;
			}
		case READY:
			switch (prot) {
			case WAIT:
				response = PongProtocol.OK;
				sendObject(response);
				console.appendLine("Waiting for server to get ready...");
				break;
			case READY:
				response = PongProtocol.READY;
				sendObject(response);
				this.state = this.state.getNextState();
				console.appendLine("Game Time!");
				startPlaying();
				break;
			default:
				response = PongProtocol.ERR;
				response.setAttachment("Unknown Command For This State");
				sendObject(response);
				break;
			}
		}
	}

}
You see that in run() I call the method (which is defined in the super class StreamManager), but it does not go into the loop as it just stalls on setting the streams.

What am I doing wrong?

I hope I was descriptive enough,

Regards,

Alex

Message was edited by: Alex
Alex_R
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2007
Added on Dec 10 2006
2 comments
665 views