Hello everyone,


I am using read method of InputStream to read a stream from a remote machine. The network connection is not very stable (for example, a wireless network whose the signal strength is relatively low). I am wondering if read method returns -1 (which indicates the end of the stream has been reached), and if I invoke read method again on the same stream, is it possible to read any more data?

I think maybe I can read some more data even if read returns -1 in one time because the connection is not very stable. I am looking for your comments to my problem in my specific situation.


regards,
George

Hi,
I am not sure I understood what you want? Are you trying to write a program in which you deal with server and clients? If yes you will need to know how to use server socket and client socket. Besides InputStreamReader, you will need BufferedReader for your client and for the server you will need PrintWriter to output the stream sent by the client.

Let me know if this is maybe what you are looking for then I may give you a small example.

Dounia

Thanks Dounia,

Hi,
I am not sure I understood what you want? Are you trying to write a program in which you deal with server and clients?

Yes, you are correct.

If yes you will need to know how to use server socket and client socket. Besides InputStreamReader, you will need BufferedReader for your client and for the server you will need PrintWriter to output the stream sent by the client.

Let me know if this is maybe what you are looking for then I may give you a small example.

Dounia

I am using a similar approach as you mentioned above, almost the same.

My problem is that, I am facing a condition where network connection is "fragile" (not stable). I am wondering what do you think the best solution of retrieving data from a remote machine in a "fragile" network connection environment.

For example, if I invoke read method of class InputStreamReader and it returns -1, is it possible that there is more data (since network connection is not very stable)? if so, how can I retrieve them?


regards,
George

Hi,
(I am wondering if read method returns -1 (which indicates the end of the stream has been reached), and if I invoke read method again on the same stream, is it possible to read any more data?)
I don't think using -1 is an effective idea to check the end of the stream. The following example would give you an idea of how to keep reading streams and how to read more data. Two files one is called WelcomeClient.java and the otherone is WelcomeServer.java.

/**
  * A simple client that connects to WelcomeServer
  *
  * To run type  java WelcomeClient <host> <port>
  * do this after you start the server.
  *
  * When testing typically client and server will run on the same
  * machine. To connect to the server running on the same machine use
  * localhost as the host name.
  *
  * 
  */


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

public class WelcomeClient{
	private Socket client;
	private BufferedReader nin;

	public static void main(String [] args ) {
		if ( args.length != 2 ) {
			System.out.println("USAGE: java WelcomeClient <HOST> <PORT> ");
			System.exit(0);
		}

		new WelcomeClient().start(args[0], Integer.parseInt(args[1]) );

	}

	public void start( String ip, int port) {

		try{
			// Get a socket to the server
			client = new Socket(ip,port);
			// Get an InputStream to server
			nin = new BufferedReader( new InputStreamReader( client.getInputStream() ));
			//Get data from the server
			System.out.println( nin.readLine() );
			System.out.println( nin.readLine() );
			// Note: Since the server is sending two lines the client
			// needs to read the two lines.
			// Close Stream
			nin.close();
			// Close Connection
			client.close();
		}catch (UnknownHostException e) {
			System.err.println("Can't find hostname");
		}catch (IOException e) {
			System.err.println("Problem getting I/O Streams : host");
		}
	}
}


/**
  * A Simple server that listens on Port 3000
  * When the client connects the date
  * and welcome message is sent to the client
  *
  * Note: the server is sending two messages to the client.
  *
  * 
  */

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

public class WelcomeServer{
	private ServerSocket server;
	private Socket connection;
	private PrintWriter nout;

	public static void main(String [] args ) {
		if( args.length != 1 ) {
			System.out.println("USAGE: java WelcomeServer <port> ");
			System.exit(0);
		}
		// create welcomeServer object passing in the port number.
		// the invoking the start method.
		new WelcomeServer(Integer.parseInt(args[0])).start();
	}

	public WelcomeServer(int port) {
		try{
			server = new ServerSocket(port);
		}catch(IOException exp){
			System.out.println(exp);
		}
		System.out.println("Server Started ");
	}

	public void start(){
		// infinite loop
                       //Everytime i put semicolon between the parentheses
                      //i keep getting the wink smilies.
		for(;;put two semicolons in here) {
			try{
				// listen for a connection
				connection = server.accept();
				// Get output stream.
				// The server wants to send a message to the client, so
				// we need an output stream.
				nout = new PrintWriter( connection.getOutputStream(), true );
				// Send message to Client
				nout.println("Welcome to my Server");
				nout.println("The Date and Time is " + new java.util.Date() );
				// Close connection
				nout.flush();
				nout.close();
				connection.close();
			}catch(IOException e){
				System.out.println(e);
			}
		}
	}
}

:) Hope this would help and good luck.

Dounia

Thanks Dounia,

:) Hope this would help and good luck.

Dounia

Your sample is very helpful. My scenario is like this, after server sends "Welcome to my Server" (and client receives this message), the connection of server is not available (for example, the cable of server is not connected), and then after a relatively long time (for example, 10 minutes) the connection is available again. And the server will continue to send information about date and time.

In your sample, the client will fail to receive date and time message since the read operation is timeout.

My application will work in an environment where connection strength is relatively low. I am wondering how to make server and client communication smoothly after a relatively long timeout. For example, in my above case I want my client be able to receive date and time message even if the timeout value is very long. But your sample only works in normal case which does not consider to work in a long timeout network environment.


regards,
George

Thanks for all the people who helped me on this thread.


regards,
George

This article has been dead for over six months. Start a new discussion instead.