J
jimgardener
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
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