Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple client server question

807569Jul 20 2006 — edited Jul 21 2006
I am trying to make a very simple client server application.

First I start the server which waits for a client to connect.

The client enters some text in the console and the server should simply print this text in the same console.

But that does not work. I just see the input text from the client very quickly and then it dissappears. Afterwards when I type some text i does not get printed.

The client is not meant to see the text printed by the server.
import java.io.*;
import java.net.*;

public class Client
{
	public static void main(String[] args) throws IOException
    {
		Socket echoSocket = null;
        try
        {
        	echoSocket = new Socket("localhost",7);
        }
        catch (UnknownHostException e)
        {
            System.err.println("Don't know about host: localhost.");
            System.exit(1);
        }
        catch (IOException e)
        {
            System.err.println(e);
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
        String userInput;        
        while ((userInput = stdIn.readLine()) != null)
        {
        	DataOutputStream toServer = new DataOutputStream(echoSocket.getOutputStream());
        	toServer.writeBytes(userInput + '\n');
        }
        
        stdIn.close();
        echoSocket.close();
    }
}
import java.net.*;
import java.io.*;

import com.sun.corba.se.internal.POA.ClientSC;
 
public class EchoServer
{
    public static void main(String argv[]) throws IOException
    {
    	String clientSentence;
    	ServerSocket welcome = new ServerSocket(7);    	
    	System.out.println("Waiting"); 	
    	while(true)
    	{
    		Socket connection = welcome.accept();
        	InputStreamReader in = new InputStreamReader(connection.getInputStream());
    		BufferedReader inFromClient = new BufferedReader(in);
    		clientSentence = inFromClient.readLine();
    		System.out.println(clientSentence);    		
    		
    	}
   	
    }
}
What am I missing? Does the server need to open its own instance of the console??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 18 2006
Added on Jul 20 2006
5 comments
141 views