[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Buffered Stream wrap doesnt work via socket, why is that?

Posted on 2009-02-20
14
Medium Priority
?
233 Views
Last Modified: 2013-11-23
I'm trying to use the below stream wrap (see code) when transporting a serialized object from a client to a server and vice versa. However it halts the application. If I dont wrap the socket streams using the Buffered Streams there is no problem at all. Why is that? The wrapping works fine when writing/reading from files on the disk.
ObjectOutputStream out = new ObjectOutputStream(
   new BufferedOutputStream(socket.getOutputStream()));
ObjectInputStream in = new ObjectInputStream(
   new BufferedInputStream(socket.getInputStream()));

Open in new window

0
Comment
Question by:Suppai
  • 6
  • 5
  • 3
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690736
It shouldn't 'halt' it. It might slow it down due to the extra buffering
0
 
LVL 92

Expert Comment

by:objects
ID: 23690737
try changing the order

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690800
Can you also be more specific about this halting behaviour? e.g. what does this print?
ObjectOutputStream out = new ObjectOutputStream(
   new BufferedOutputStream(socket.getOutputStream()));
System.out.println("Got output");

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Suppai
ID: 23690868
Well no it doesnt get through the creation of the streams. The connection is establish and then when it comes to the stream creation the gui halts and neither client or server gets on from there (see code). As mentioned before it works smoothly without the buffered streams.

that is when running the code attached it only gets to write the connected status on both sides, then it halts.

BTW: is there a way to keep the formatting when pasting in code in the code window, when copy-pasting from eclipse, it just disrupts it totally because of the word wrapping.
//client side
public void run() {		
	try{
	SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
			System.out.println("Client: connection established");
			ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
			ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));		
			System.out.println("Client: IO Streams created");
			ServerRequest request = new ServerRequest(urlList,wordList,frequency);
			out.writeObject(request);
			out.flush();
			System.out.println("Client: request sent to server");
			
			ServerReport report = (ServerReport)in.readObject();
			System.out.println("Client: report received from server");
			if (report!=null) {
				System.out.println("Client: Received object: " + report);
			}			
			out.close();
			in.close();
		}catch(Exception e){	
			System.out.println(e.getMessage());
		}
	}	
 
//Server side
public void start(){
		SSLServerSocket serverSocket=null;
		try{
			SSLServerSocketFactory socketFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
			serverSocket = (SSLServerSocket)socketFactory.createServerSocket(SERVER_PORT);
			SSLSocket socket = (SSLSocket)serverSocket.accept();
			System.out.println("Server: Connection Established");
			ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
			ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			System.out.println("Server: IO Streams Created");
			ServerRequest req = (ServerRequest)in.readObject();
			System.out.println("Server: request received from client: " + req);
			if (req!=null) {				
				System.out.println("Server: received object: " + req);
				out.writeObject(new ServerReport("Server: request received from client: "  + req));
				out.flush();
				System.out.println("Server: object written to client");
			}		
			out.close();
			in.close();
		}catch(Exception e){	
			System.out.println("Server: " + e.getMessage());
		}		
	}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 23690927
what oder do you open them on the server?
0
 
LVL 92

Expert Comment

by:objects
ID: 23690939
it maybe the server thats the wrong way around it should open the output stream first, and the client open the input stream first

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690999
>>System.out.println("Client: IO Streams created");

If the above doesn't get printed, network problems could be involved
0
 

Author Comment

by:Suppai
ID: 23691219
Im running on localhos and it works without the buffered stream wrapping.  
In fact I made it work by waiting to initialize the streams until right before they were used like attached (see code). Why is this different from when not using buffered streams? Shouldnt it work in exactly the same way?
//Client side
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
			System.out.println("Client: connection established");
			ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));				
			System.out.println("Client: IO Streams created");
			ServerRequest request = new ServerRequest(urlList,wordList,frequency);
			out.writeObject(request);
			out.flush();
			System.out.println("Client: request sent to server");			
			ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));	
			ServerReport report = (ServerReport)in.readObject();
			System.out.println("Client: report received from server");
			if (report!=null) {
				System.out.println("Client: Received object: " + report);
			}			
			out.close();
			in.close();
}
 
//Server side
public void start(){
		SSLServerSocket serverSocket=null;
		try{
			SSLServerSocketFactory socketFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
			serverSocket = (SSLServerSocket)socketFactory.createServerSocket(SERVER_PORT);
			SSLSocket socket = (SSLSocket)serverSocket.accept();
			System.out.println("Server: Connection Established");			
			ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			System.out.println("Server: IO Streams Created");
			ServerRequest req = (ServerRequest)in.readObject();
			System.out.println("Server: request received from client: " + req);
			ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
			if (req!=null) {				
				System.out.println("Server: received object: " + req);				
				out.writeObject(new ServerReport("Server: request received from client: "  + req));
				out.flush();
				System.out.println("Server: object written to client");
			}		
			out.close();
			in.close();
		}catch(Exception e){	
			System.out.println("Server: " + e.getMessage());
		}		
	}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23691283
If that's now working, it's probably because you've got the client and server reading and writing in the correct order, which you possibly didn't have before. read() is a blocking call so it will halt the app thread until there are data to read
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23691294
As for your formatting in the code snippet window, it looks OK to me apart from the odd line here or there
0
 

Author Comment

by:Suppai
ID: 23693738
So if I want to be able to send a response twice to the client from the server how can I do this then if it has to be in the write order (server.out, client.in, client.out, server.in etc. etc.)? e.g. a few status messages in a row? Should separate input and output threads be created in the client for haandlign this or how would one do this normally?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 750 total points
ID: 23693819
Well you just have to get things in the right order. I agree this is a pain since there's so much repetitive coding involved.

I was once going to write something where the protocol ordering could be controlled declaratively, say by xml files, but i'm afraid i haven't got round to it ... ;-)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 750 total points
ID: 23695543
It is nothing to do with how many messages you send, its just the initial creation of the streams that needs to be created in the approproate order to avoid a deadlock as I mentioned in my earlier comments.

0
 
LVL 92

Expert Comment

by:objects
ID: 23695889
just to clarify the problem has absolutely nothing to do with your protocol.
Let me know if you have any questions

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question