10 Replies - 2874 Views - Last Post: 02 June 2012 - 04:00 PM Rate Topic: -----

#1 d_man10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-May 12

Client/Server ObjectInput and ObjectOutput Streams.

Posted 31 May 2012 - 01:08 PM

I am creating a simple client and server program. I have it running and it accepts the connection from the client. All I currently want the program to do is when the client is started, the user is prompted for a username then the client sends the username to the server and the server prints the username. I have added messages to prompt me when if gets to certain parts of the code and my client will not prompt me with the message after I create the ObjectInput and ObjectOutput Streams. Here is my code:
import java.io.*;
import java.net.*;

import javax.swing.JOptionPane;

public class Client {
	private Socket socket;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private String username = null;
	private static String host = "localhost";
	private static int port = 2000;

	private Client(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public boolean start() {
		try {
			socket = new Socket(host, port);
		}
		catch (Exception ec) {
			System.out.println("Error connectiong to server:" + ec);
			return false;
		}

		String msg = "Connection accepted " + socket.getInetAddress() + ":"
				+ socket.getPort();
		System.out.println(msg);
		try {
			in = new ObjectInputStream(socket.getInputStream());
			out = new ObjectOutputStream(socket.getOutputStream());
			out.flush();
			System.out.println("Streams created.");
		} catch (IOException eIO) {
			System.out.println("Exception creating new Input/output Streams: "
					+ eIO);
			return false;
		}
		try {
			username = JOptionPane.showInputDialog("Enter a username");
			out.writeObject(username);
			System.out.println(username + " sent to the server.");
		} catch (IOException eIO) {
			System.out.println("Exception doing login : " + eIO);
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		Client client = new Client(host, port);
		client.start();
	}
}


import java.io.*;
import java.net.*;

public class Server {
	private ServerSocket sSocket;
	private Socket socket;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private String username;
	private static int port = 2000;
	private boolean keepGoing = true;

	public void run() {
		while (keepGoing) {
			try {
				sSocket = new ServerSocket(port);
				System.out.println("Waiting on connection...");
				socket = sSocket.accept();
				System.out.println("Connection accepted.");
				in = new ObjectInputStream(socket.getInputStream());
				out = new ObjectOutputStream(socket.getOutputStream());
			} catch (IOException io) {
				io.printStackTrace();
			}
			try {
				username = (String) in.readObject();
				System.out.println(username);
			} catch (ClassNotFoundException cnf) {
				cnf.printStackTrace();
			} catch (IOException io) {
				io.printStackTrace();
			}
		}
	}

	public void stop() {
		keepGoing = false;
		try {
			in.close();
			out.close();
			socket.close();
			sSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


This is what I get as a result:
Client result:
Connection accepted localhost/127.0.0.1:2000

Server result:
Waiting on connection...
Connection accepted.



So my question is, why is my client not displaying "Streams created." If the streams aren't created I can not finish executing my code.

Is This A Good Question/Topic? 0
  • +

Replies To: Client/Server ObjectInput and ObjectOutput Streams.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 31 May 2012 - 01:53 PM

Before your Client can create a ObjectInputStream the Server has to have create its ObjectOutputStream

So the Server HAS to create in that order

		out = new ObjectOutputStream(socket.getOutputStream());
		in = new ObjectInputStream(socket.getInputStream());


if the Client creates in that order
		in = new ObjectInputStream(socket.getInputStream());
		out = new ObjectOutputStream(socket.getOutputStream());


So server will create out and wait for Client to create its in
Client will create its in and thus Server can continue creating its in
which allow the Client to create its out.

Happy networking

This post has been edited by pbl: 31 May 2012 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 d_man10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-May 12

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 31 May 2012 - 04:11 PM

Thank you, I didn't think about the order at all to be honest. Now I have one error that I can't seem to get past.
I get this:
IOException #1 java.net.BindException: Address already in use: JVM_Bind

To be honest I believe it's because at first I did not close the sockets so they may still be open.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 31 May 2012 - 05:25 PM

Your Server is listening on the ServerSocket in a Thread
this is not a good design. When you'll shutdown the thread will still be running.

The server should not wait for connection in a thread

    while(true) {
        Socket s = serverSocket.accept();
        ... now fire a Thread to handle that new client
    }



so if you <CTRL>Y or <CTRL>C or stop by any other means your server it will free up the ServerSocket

View Postd_man10345, on 31 May 2012 - 07:11 PM, said:

Thank you, I didn't think about the order at all to be honest.


The ObjectInputStream() constructor documentation is quite clear about that

http://docs.oracle.c...io.InputStream)


Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Was This Post Helpful? 0
  • +
  • -

#5 d_man10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-May 12

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 31 May 2012 - 08:42 PM

Alright, I currently have it working. I apologize for being so much trouble, I'm new to the whole socket and Object streams topic. I appreciate the help a lot.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 31 May 2012 - 08:58 PM

No problem at all
We are here answering questions like that because.... don't remember why but if it works for you
glad I could help
Was This Post Helpful? 0
  • +
  • -

#7 d_man10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-May 12

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 01 June 2012 - 08:42 AM

Alright, one more question. I'm trying not to drain you of all your knowledge. You said using a thread was a bad idea/design, what would I need to use to accept connections from multiple clients?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 01 June 2012 - 06:33 PM

I wrote a very basic one that will show you a thread for a client. Actually was not really really necessary because only one message is exchanged but the principle is there

http://www.dreaminco...snippet1917.htm

And a full chat room if you want to see more

http://www.dreaminco...r-gui-optional/
with 25,000 views in 6 months I guess it should work
Was This Post Helpful? 0
  • +
  • -

#9 d_man10345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-May 12

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 02 June 2012 - 09:29 AM

OK thank you again. I'm happy to say I got the server and client to work with multiple connections and I have a user files with passwords and stored properties on the server that is read by the server and checks with the data sent from the client and either connects the user to a chat room or denies access. I'm fairly proud to be honest, but I couldn't have done it without you. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 02 June 2012 - 11:56 AM

Quote

I'm trying not to drain you of all your knowledge.


I wouldn't worry. That's not a stream that's going to run dry anytime soon
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client/Server ObjectInput and ObjectOutput Streams.

Posted 02 June 2012 - 04:00 PM

Thanks g00se much appreciated :)
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1