Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Stream issue?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stream issue?

    So I've been grinding over the summer to make sure I'm ahead of my class when the school year starts, and I recently started dabbing into sockets and datagram servers etc. I haven't fully wrapped my head around it so logic would dictate that I would eventually run into a problem... Here I am...

    Ok so I have two classes here; My client singleton that connects to the port and host name I specified and my server singelton that binds to a port. Here they are:


    SERVERSINGLETON:
    package org.com.eng.server;
     
    import javax.swing.*;
    import java.net.*;
    import java.io.*;
     
    public class ServerSingleton implements Runnable {
     
     
     
     
    	public ServerSingleton(int port)throws IOException{
    		try {
     
    		} catch(Exception e){
    			System.out.println("Could not listen on the specified port. Might be in use...");
    		}
     
    		try {
     
    			ServerSocket ServSocket = new ServerSocket(port);
    			Socket ClntSocket;
    			ClntSocket = ServSocket.accept();
     
    			PrintWriter out = new PrintWriter(ClntSocket.getOutputStream(), true);
    			BufferedReader in = 
    			    new BufferedReader(new InputStreamReader(ClntSocket.getInputStream()));
     
    			String userIn = in.readLine();
     
    			if(userIn != null){
    				System.out.println("Recieved Data!");
    			}
     
    			if(userIn.equalsIgnoreCase("wai")){
    				out.println("You are Patrick.");
    				System.out.println("Recieved Data!");
    			}
     
    			while(userIn != null){
    				System.out.println("Recieved Data!");
    			}
     
     
    		} catch (SocketException e){
    			e.printStackTrace();
    			System.out.println("Something goofed when determining the server socket");
    			System.exit(-1);
    		}
    		try {
     
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    	}
     
    	public void run(){
    		try {
    			getServerSingleton();
    		} catch (Exception e){
     
    		}
    	}
     
    	/*
    	 * @author Patrick Worlds
    	 * @return Returns the current ServerSingleton
    	 */
    	public static synchronized ServerSingleton getServerSingleton() throws IOException{
    		if(Server == null)
    			Server = new ServerSingleton(4445);
    		return Server;
    	}
     
    	public Object clone() throws CloneNotSupportedException {
    		JOptionPane.showMessageDialog(null, "Bad boy! >:( Go sit in corner nao!");	
    		throw new CloneNotSupportedException();
    	}
     
    	private static ServerSingleton Server;
     
    }



    CLIENTSINGLETON:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
     
    import java.net.Socket;
     
    import javax.swing.JOptionPane;
     
     
     
    public class ClientSingleton implements Runnable {
     
    	private Socket ClntSocket;
     
     
    	public ClientSingleton(int port, String hostname)throws IOException{
    		try {
     
     
     
    			ClntSocket = new Socket(hostname, port);
     
    			BufferedReader stdIn = new BufferedReader(
                        new InputStreamReader(System.in));
     
    			PrintWriter out = new PrintWriter(ClntSocket.getOutputStream(), true);
    			BufferedReader in = 
    			    new BufferedReader(new InputStreamReader(ClntSocket.getInputStream()));
     
     
    			if(ClntSocket.isConnected() == true){
    				System.out.println("Connection established");
    			}
    			String userIn;
    			String serverOut = in.readLine();
     
    			userIn = stdIn.readLine();
    			while(userIn != null){
    				out.println(userIn);
    				System.out.println("You: " +userIn);
    			}
     
    			while(serverOut != null){
    				System.out.println("Server: " +serverOut);
     
    			}
     
    		} catch (Exception e){
    			e.printStackTrace();
    			System.out.println("Something goofed when determining a connection...");
    			System.exit(-1);
    		}
     
    	}
     
    	public void run(){
    		try {
    			getClientSingleton();
    		} catch (Exception e){
     
    		}
    	}
     
    	/*
    	 * @author Patrick Worlds
    	 * @return Returns the current ClientSingleton
    	 */
    	public static synchronized ClientSingleton getClientSingleton() throws IOException{
    		if(Client == null)
    			Client = new ClientSingleton(4445, "127.0.0.1");
    		return Client;
    	}
     
    	public Object clone() throws CloneNotSupportedException {
    		JOptionPane.showMessageDialog(null, "1 at a time ;)");	
    		throw new CloneNotSupportedException();
    	}
     
    	private static ClientSingleton Client;
    }




    Now a problem arose. That being that every time I attempt to input something (namely "wai") it seems that either the server ignores it, or never even receives it. I'd really love if someone could help me out here. >.<


    -Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stream issue?

    Can you post the console from when you execute the program that shows what it does including the println output?

    How do you execute the code? There are no main() methods
    Last edited by Norm; August 6th, 2012 at 08:11 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stream issue?

    Quote Originally Posted by Norm View Post
    Can you post the console from when you execute the program that shows what it does including the println output?

    How do you execute the code? There are no main() methods
    The console doesn't show anything because it's (Supposed to be) awaiting input from me (the user). As for the main methods:

    Server:
    package org.com.eng.server;
     
    import java.io.IOException;
     
    public class Server {
     
    	public static Thread ServerThread;
     
    	public static void main(String args[]) throws IOException{
    		try{
    			ServerThread = new Thread(new ServerSingleton(4445));
    			ServerThread.start();
    		} catch(Exception e) {
    			System.out.println("Could not start server");
    		}
    	}
     
     
    }

    Client:
    package org.com.eng.client;
     
     
     
    import org.com.eng.client.ClientSingleton;;
     
    public class Client {
     
    	public static Thread ClientThread;
     
    	public static void main(String args[]){
    		try{
    			ClientThread = new Thread(new ClientSingleton(4445, "127.0.0.1"));
    			ClientThread.start();
    		} catch(Exception e){
    			System.out.println("Could not start client");
    		}
     
    	}
     
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stream issue?

    The console doesn't show anything
    How do you know when to enter anything if there is no prompt from the program?
    every time I attempt to input something (namely "wai")
    Where is the "wai" entered? I'd expect to see that on the console.
    Also where is the java command to start the program entered?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stream issue?

    Quote Originally Posted by Norm View Post
    How do you know when to enter anything if there is no prompt from the program?

    Where is the "wai" entered? I'd expect to see that on the console.
    Also where is the java command to start the program entered?

    Well, this bit here is meant to await input from the user:

    BufferedReader stdIn = new BufferedReader(
                        new InputStreamReader(System.in));
     
    			PrintWriter out = new PrintWriter(ClntSocket.getOutputStream(), true);
    			BufferedReader in = 
    			    new BufferedReader(new InputStreamReader(ClntSocket.getInputStream()));
     
     
    			if(ClntSocket.isConnected() == true){
    				System.out.println("Connection established");
    			}
    			String userIn;
    			String serverOut = in.readLine();
     
    			userIn = stdIn.readLine();
    			while(userIn != null){
    				out.println(userIn);
    				System.out.println("You: " +userIn);
    			}
     
    			while(serverOut != null){
    				System.out.println("Server: " +serverOut);
     
    			}

    And I don't enter until a connection is established. The client checks that here:
    			if(ClntSocket.isConnected() == true){
    				System.out.println("Connection established");
    			}

    So yeah, I type "wai" into the console once I see "Connection established" hoping to see a result, but no dice :/.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stream issue?

    Try debugging the code by adding lots of println statements so you see where the code is executing. If it is hanging someplace, the printlns will show you the last place it executed before it hung.

    It be nice to the user if you typed a message asking for input instead of just waiting.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stream issue?

    Quote Originally Posted by Norm View Post
    Try debugging the code by adding lots of println statements so you see where the code is executing. If it is hanging someplace, the printlns will show you the last place it executed before it hung.

    It be nice to the user if you typed a message asking for input instead of just waiting.
    Hmm Good idea! Don't know why I didn't think of that before >.<

Similar Threads

  1. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  2. could not create audio stream from input stream
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 2nd, 2011, 02:08 AM
  3. Object Stream Hanging...
    By Ellipsis in forum Java Networking
    Replies: 2
    Last Post: October 22nd, 2010, 07:20 PM
  4. UTF - 8 / Byte Stream
    By JavaCODER in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 16th, 2010, 02:26 PM
  5. Stream Tokenizer
    By x3rubiachica3x in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2010, 01:05 PM