Results 1 to 6 of 6

Thread: I/O server-client program in java

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    I/O server-client program in java

    Hi,
    I have a problem in creating a client server program in java. Here is my source code. Check the code and see if you could help me.
    Code:
    Public class hand {
    	Public static void hand(String args[]) {
    		System.out.print("Welcome ");
    		try {
    			sc = new Socket("127.0.0.1",8080);
    		}
    		catch(Exception e) {
    			System.out.print("Error opening sc");
    		}
    		scn = new Scanner(System.in);
    		try {
    			out = new PrintWriter(sc.getOutputStream(),true);
    		} catch (Exception e) {
    			System.out.print("Error creating outflow");
    		}
    		System.out.print("Variable initialized: waiting for input ...");
    String str;
    		while(true) {
    			str = scn.nextLine();
    out.System.out.println(str);
    		}
    	}
     	private static Socket sc;
    	private static Scanner scn;
    	private static PrintWriter out;
    }
    This is my client program and similar to this is my server program, but I am getting an error while compiling the code.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: I/O server-client program in java

    Hello,
    When you cut the client, it cuts the connection socket. The server that is listening the socket connection, and you get an exception. It is a mode of operation almost normal, you're waiting, detected by this expectation that the connection is cut. There are other ways to do this, but it is perfectly correct. When you cut the server, the client is not listening to the socket connection. Obviously does not receive any exception due to cutting socket. To read your code, this should occur when attempting to write the content received from System.in to the server. I hope my explanation will help you.

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: I/O server-client program in java

    Hello,
    So if I want to see the server if the server cuts the connection or not, I shall open an entry to the client that listens the messages from the server. If I get an exception when the server is off, then what should I do? If I'm multi-thread and an exception is occurred, is that the program will stop? or should my program catch an exception for that?

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: I/O server-client program in java

    Hello,
    You can catch the IOException occurred when listening to the client. Otherwise you can test the status of the connection to the server with the method
    isConnected()
    This is a Class Socket method. A note, remember to close your stream (in a finally block of preference). It is not explicit but can not say if any problem raises because of it.

  5. #5
    Join Date
    Dec 2009
    Posts
    192

    Re: I/O server-client program in java

    Hi,
    This is my updated code, I have included the exception in this code, please check if I can improve this code more.
    Code:
    Public class hand {
    	Public static void hand(String args[]) {
    		System.out.print("Welcome to the chat client ...");
    		
    		try {
    			sc = new Socket("127.0.0.1",8080);
    scn = new Scanner(System.in);
    
    System.out.System.out.println(sc.getInetAddress().toString());
    			
    			try {
    				out = new PrintWriter(sc.getOutputStream(),true);
    			} catch (Exception e) {
    				System.out.System.out.println("Error creating outflow");
    			}
    			
    			System.out.print("Variable initialized: waiting for input ...");
    
    String strin;
    			while(sc.isConnected()) {
    				strin = scn.nextLine();
    out.System.out.println(strin);
    			}
    		}
    		catch(Exception e) {
    			System.out.System.out.println("Error opening sc");
    		}
    		finally {
    			out.close();
    scn.close();
    			try {
    			sc.close();
    			}
    			catch(Exception e) {
    				
    			}
    		}
    	}
    	private static Socket sc;
    	private static Scanner scn;
    	private static PrintWriter out;
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: I/O server-client program in java

    Hello,
    To close the stream, it is preferable to use try / finally blocks different. Just have a look at the code below
    Code:
    Public class Hand {
        Public static void hand(String[] args) {
            Socket sc = null;
            Scanner scn = null;
            PrintWriter out = null;
            System.out.print("Welcome to the chat client ...");
            try {
                sc = new Socket("127.0.0.1", 8080);
                System.out.System.out.println(sc.getInetAddress().toString());
                scn = new Scanner(System.in);
                out = new PrintWriter(sc.getOutputStream(),true);
                
                System.out.print("Variable initialized: waiting for input ...");
                try {
                    while(sc.isConnected()) {
                        String str = scn.nextLine();
                        out.System.out.println(str);
                    }
                } finally {
                    try {
                        out.close();
                    } finally {
                        try {
                            scn.close();
                        } finally {
                            if(! sc.IsClosed()) {
                                sc.close();
                            }
                        }
                    }
                }
            } catch (UnknownHostException ex) {
                System.out.System.out.println("Server unknown");
                ex.printStackTrace();
            } catch (IOException ex) {
                System.out.System.out.println("Standard Flow");
                ex.printStackTrace();
            }
        }
    }

Similar Threads

  1. Replies: 7
    Last Post: 16-02-2011, 08:14 PM
  2. Help with Java card client
    By Maddy07 in forum Software Development
    Replies: 5
    Last Post: 24-07-2010, 03:28 AM
  3. Information about how to ensure torrent client program integrity
    By Cheng-Gong in forum Technology & Internet
    Replies: 11
    Last Post: 24-06-2010, 06:42 AM
  4. Communicating between server in c++ and java client
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 11:48 AM
  5. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,654,422.22870 seconds with 17 queries