Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > echoclient reading from server

Reply
Thread Tools

echoclient reading from server

 
 
jimgardener
Guest
Posts: n/a
 
      07-01-2008
i have written an echoclient that connects to a server,takes input
from user and writes to server.I want the client to read from the
server and show it

this is what i wrote

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
public class EchoClient {
int port;
ByteBuffer buf=ByteBuffer.allocate(1024);
public EchoClient(int p)throws IOException{
port=p;
initClient();
}
private void initClient()throws IOException{
InetSocketAddress isa=new
InetSocketAddress(InetAddress.getLocalHost(),port) ;
SocketChannel sc=SocketChannel.open(isa) ;
if(sc!=null)debug("connected to server");
BufferedReader userin=new BufferedReader(new
InputStreamReader(System.in));
String userinput;
while((userinput=userin.readLine())!=null){
buf.clear();
buf.put(userinput.getBytes());
buf.flip();
sc.write(buf);
if(userinput.equals("bye"))break;
}

//how to read from server?


}

public static void main(String[] args) {
if (args.length !=1)debug("EchoClient port");
try{
new EchoClient(Integer.parseInt(args[0]));

}catch(IOException e){
e.printStackTrace();
}

}
public static void debug(String msg){
System.out.println(msg);
}
}


i am not very sure how to do the reading from the server part..can
someone help?

jim
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      07-01-2008
On Tue, 1 Jul 2008 03:25:41 -0700 (PDT), jimgardener wrote:
> i have written an echoclient that connects to a server,takes input
> from user and writes to server.I want the client to read from the
> server and show it


[...]

> i am not very sure how to do the reading from the server part..can
> someone help?


You already seem to know how to write to the server. And if you've
written to the server, then presumably it can read what you wrote. So
why is reading a challenge at the client?

I'm not sure why you chose to set up the connection with a
SocketChannel instead of a "plain" Socket. At the client end the
"traditional way" is probably easier than the stuff in java.nio.

The SocketChannel has methods for reading. Or you can get the Socket
from the SocketChannel, and an InputStream from that.

/gordon

--
 
Reply With Quote
 
 
 
 
jimgardener
Guest
Posts: n/a
 
      07-01-2008

> I'm not sure why you chose to set up the connection with a
> SocketChannel instead of a "plain" Socket. At the client end the
> "traditional way" is probably easier than the stuff in java.nio.



i was learning nio using Greg Travis's ibmalphaworks tutorial on
nio.I tried out the MultiportEcho server program and thought i'd
write the client..

i modified the client code as below

private void initClient()throws IOException{
InetSocketAddress isa=new
InetSocketAddress(InetAddress.getLocalHost(),port) ;
SocketChannel sc=SocketChannel.open(isa) ;
if(sc!=null)debug("connected to server");
BufferedReader userin=new BufferedReader(new
InputStreamReader(System.in));
String userinput;
while((userinput=userin.readLine())!=null){
buf.clear();
buf.put(userinput.getBytes());
buf.flip();
sc.write(buf);
if(userinput.equals("bye"))break;
}

//trying to read from server
while(true){
buf.clear();
int r=sc.read(buf);
if(r <=0)break;
buf.flip();
String text=new String(buf.array());

debug("read from server message of length="+text.length()
+"message:"+text);
}


}


problem here is that the echo would come only after client says bye to
the server..I would like the server to return the echo as soon as i
type in.Is that possible?

Also ,since i am allocating the ByteBuffer as of 1024 bytes..the
returned string ('text' in the above code) is 1024 bytes length and
this is displayed by debug() as a string with a lot of blanks at the
end


i would like to know if these can be rectified
thanks
jim
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      07-01-2008
On Tue, 1 Jul 2008 06:41:31 -0700 (PDT), jimgardener wrote:
> problem here is that the echo would come only after client says bye to
> the server..I would like the server to return the echo as soon as i
> type in.Is that possible?


It's possible to wait for the server to reply anywhere you like. An
idea is to do that after sending each line instead of after sending
the entire user input.

> Also ,since i am allocating the ByteBuffer as of 1024 bytes..the
> returned string ('text' in the above code) is 1024 bytes length and
> this is displayed by debug() as a string with a lot of blanks at the
> end


read() tells you how much it read, and there is a String constructor
that lets you specify how much of the buffer to use.

/gordon

--
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      07-01-2008
"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:486a0a5b$0$2677$(E-Mail Removed)...
> On Tue, 1 Jul 2008 03:25:41 -0700 (PDT), jimgardener wrote:
>> i have written an echoclient that connects to a server,takes input
>> from user and writes to server.I want the client to read from the
>> server and show it

>
> [...]
>
>> i am not very sure how to do the reading from the server part..can
>> someone help?

>
> You already seem to know how to write to the server. And if you've
> written to the server, then presumably it can read what you wrote. So
> why is reading a challenge at the client?
>
> I'm not sure why you chose to set up the connection with a
> SocketChannel instead of a "plain" Socket. At the client end the
> "traditional way" is probably easier than the stuff in java.nio.
>
> The SocketChannel has methods for reading. Or you can get the Socket
> from the SocketChannel, and an InputStream from that.
>
> /gordon


A lot of the tutorials out now will use java.nio for both ends. For example,
http://rox-xmlrpc.sourceforge.net/niotut/

You're quite right that NIO was oriented towards servers: non-blocking I/O,
buffers and selectors. It's not that tough, however, to think of clients
that would wish to avail themselves of non-blocking I/O. If reading from the
connection is lietrally the only thing that the client can do at a certain
point, sure, NIO doesn't change the equation. But a web browser is one
example of a client that can benefit from the newer approach.

AHS


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
DVD ROM not reading DVDs but reading CDs sayeed.afzal@gmail.com Computer Information 0 05-22-2006 10:37 AM
Server to server = Server client to server? - Java 2 07-29-2005 10:46 PM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments