The client connects to the server and then enters the following loop. It takes one line of input from the console, sends it to the server. If the input line is a single ‘x’ character, the client exits.
The server
At first, it prints out its own socket address to the screen. When it receives a connection, it prints the peer’s socket address to the screen (so you can check who connected to the server), then enters the following loop. It receives a line of message from the client, prints the received message to the screen.
Non-blocking mode
The client should use buffers and a non-blocking socket channel.
Blocking mode
The server should use buffers and a blocking socket channel.
The issue
The issue that I am having is that the buffer, in the server side, is not flushing and is printing the old input with the new input. Example:
Message received: hello
Message received: helloworld
Message received: helloworldthis
Message received: helloworldthisis
Message received: helloworldthisis Edwin
And it should be printing: hello
world
this
is
Edwin.
This is the server soure code:
public class EchoServer_1
{
// private static CharBuffer buffer = CharBuffer.allocate(1024);
private static ByteBuffer buffer = ByteBuffer.allocate(1024);
private static StringBuffer reqString = new StringBuffer();
public static void main(String[] args) throws IOException
{
ServerSocketChannel serverSocketChannel = null;
int serverPort = 10007;
try{
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress (serverPort));
//Blocking mode
serverSocketChannel.configureBlocking(true);
System.out.println("Port number: " + serverPort);
System.out.println("Waiting for connection ..");
SocketChannel sc = serverSocketChannel.accept();
System.out.println ("Incoming connection from: "
+ sc.socket().getRemoteSocketAddress( ));
// Read message from client
while (true)
{
buffer.clear();
sc.read(buffer);
buffer.flip();
while(buffer.hasRemaining()) {
char c = (char) buffer.get();
if (c == '\r' || c == '\n') break;
reqString.append(c);
}
log("Message received: " + reqString);
}
}catch (IOException ex)
{
if (serverSocketChannel != null)
serverSocketChannel.close();
System.err.println("Client has disconneted");
}
}
private static void log(String str) {
System.out.println(str);
}
}
This is the client source code:
public class EchoClient_1
{
private static Scanner stdIn = new Scanner(System.in);
// private static CharBuffer buffer = CharBuffer.allocate(1024);
private static ByteBuffer buffer = ByteBuffer.allocate(256);
public static void main(String[] args) throws IOException
{
String hostname = new String ("127.0.0.1");
int port = 10007;
try{
InetSocketAddress address = new InetSocketAddress(hostname, port);
SocketChannel sC = SocketChannel.open(address);
sC.configureBlocking(false);
log("Local address: " + hostname + " connecting to Server on port " + port
+ "...");
String userInput;
System.out.println("Enter lines of characters: " );
while ((userInput = stdIn.nextLine()) != null)
{
//Checks if user wants to exit
if (userInput.equalsIgnoreCase("x"))
{
buffer.put (userInput.getBytes());
buffer.flip();
sC.write(buffer);
buffer.clear();
System.out.println("Closing ..");
sC.close();
break;
}
buffer.put (userInput.getBytes());
buffer.flip();
sC.write(buffer);
buffer.clear();
}
sC.close();
}catch (IOException ex)
{
}
}
private static void log(String str) {
System.out.println(str);
}
}
You are never clearing reqString
. You should do that after you log it.