Results 1 to 2 of 2
Thread: Java Socket Issue!
- 10-21-2012, 01:30 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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.
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!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(); } }
- 10-30-2012, 05:22 PM #2
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
-
Issue with blocking on an Socket inboundConenction()
By hahncj55408 in forum New To JavaReplies: 9Last Post: 08-19-2011, 09:37 PM -
communication issue between Java server (socket) and Javascript page (Jquery) by http
By linonil in forum Advanced JavaReplies: 0Last Post: 06-14-2011, 06:51 PM -
Socket and how to launch thread for receiving socket messages
By newbiejava in forum New To JavaReplies: 1Last Post: 07-02-2010, 01:18 PM -
Creating Socket Issue
By castiel in forum NetworkingReplies: 2Last Post: 08-25-2009, 01:34 PM -
writting extended ascii chars on socket........or Endianness Issue......??
By sachinj13 in forum Threads and SynchronizationReplies: 8Last Post: 09-23-2008, 02:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks