Results 1 to 2 of 2
  1. #1
    J4VA is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Java Socket Issue!

    Hi,

    EchoClient class creates a socket thereby getting a connection to the Echo server. It reads input from the user on the standard input stream, and then forwards that text to the Echo server by writing the text to the socket. The server echoes the input back through the socket to the client. The client program reads and displays the data passed back to it from the server.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class EchoClient {
        public static void main(String[] args) throws IOException {
    
            Socket echoSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                echoSocket = new Socket("taranis", 7);
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(
                                            echoSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: taranis.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                                   + "the connection to: taranis.");
                System.exit(1);
            }
    
    	BufferedReader stdIn = new BufferedReader(
                                       new InputStreamReader(System.in));
    	String userInput;
    
    	while ((userInput = stdIn.readLine()) != null) {
    	    out.println(userInput);
    	    System.out.println("echo: " + in.readLine());
    	}
    
    	out.close();
    	in.close();
    	stdIn.close();
    	echoSocket.close();
        }
    }
    I have edited the socket to the port 80, in order to get HTTP header. It is working! only I am having a hard time editing the program so it prints out the result straight away without having the user to type a character in order to display a line, I have tried to change the last bit of the code in "while" loop but nothing worked. please help!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Java Socket Issue!

    The readLine() method waits for a new line character. If you use this method, you have no choice but to wait for the line terminator. You can try a different method instead - like read() which reads a single character as an int. If the data coming from the client is simple ascii, then casting the int to a char should give you the character you want to see.

Similar Threads

  1. Issue with blocking on an Socket inboundConenction()
    By hahncj55408 in forum New To Java
    Replies: 9
    Last Post: 08-19-2011, 10:37 PM
  2. Replies: 0
    Last Post: 06-14-2011, 07:51 PM
  3. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  4. Creating Socket Issue
    By castiel in forum Networking
    Replies: 2
    Last Post: 08-25-2009, 02:34 PM
  5. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •