non blocking client server chat application in java using nio
Posted
by
Amith
on Stack Overflow
See other posts from Stack Overflow
or by Amith
Published on 2012-06-19T06:27:19Z
Indexed on
2012/09/21
3:38 UTC
Read the original article
Hit count: 217
I built a simple chat application using nio channels. I am very much new to networking as well as threads. This application is for communicating with server (Server / Client chat application).
My problem is that multiple clients are not supported by the server. How do I solve this problem? What's the bug in my code?
public class Clientcore extends Thread
{
SelectionKey selkey=null;
Selector sckt_manager=null;
public void coreClient()
{
System.out.println("please enter the text");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
SocketChannel sc = null;
try
{ sc = SocketChannel.open();
sc.configureBlocking(false);
sc.connect(new InetSocketAddress(8888));
int i=0;
while (!sc.finishConnect())
{
}
for(int ii=0;ii>-22;ii++)
{
System.out.println("Enter the text");
String HELLO_REQUEST =stdin.readLine().toString();
if(HELLO_REQUEST.equalsIgnoreCase("end"))
{
break;
}
System.out.println("Sending a request to HelloServer");
ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());
sc.write(buffer);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (sc != null)
{
try
{
sc.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
} }
public void run()
{
try
{
coreClient();
}
catch(Exception ej)
{
ej.printStackTrace();
}}}
public class ServerCore extends Thread
{
SelectionKey selkey=null;
Selector sckt_manager=null;
public void run()
{
try
{
coreServer();
}
catch(Exception ej)
{
ej.printStackTrace();
}
}
private void coreServer()
{
try
{
ServerSocketChannel ssc = ServerSocketChannel.open();
try
{
ssc.socket().bind(new InetSocketAddress(8888));
while (true)
{
sckt_manager=SelectorProvider.provider().openSelector();
ssc.configureBlocking(false);
SocketChannel sc = ssc.accept();
register_server(ssc,SelectionKey.OP_ACCEPT);
if (sc == null)
{
}
else
{
System.out.println("Received an incoming connection from " + sc.socket().getRemoteSocketAddress());
printRequest(sc);
System.err.println("testing 1");
String HELLO_REPLY = "Sample Display";
ByteBuffer buffer = ByteBuffer.wrap(HELLO_REPLY.getBytes());
System.err.println("testing 2");
sc.write(buffer);
System.err.println("testing 3");
sc.close();
}}}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (ssc != null)
{
try
{
ssc.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
catch(Exception E)
{
System.out.println("Ex in servCORE "+E);
}
}
private static void printRequest(SocketChannel sc) throws IOException
{
ReadableByteChannel rbc = Channels.newChannel(sc.socket().getInputStream());
WritableByteChannel wbc = Channels.newChannel(System.out);
ByteBuffer b = ByteBuffer.allocate(1024); // read 1024 bytes
while (rbc.read(b) != -1)
{
b.flip();
while (b.hasRemaining())
{
wbc.write(b);
System.out.println();
}
b.clear();
}
}
public void register_server(ServerSocketChannel ssc,int selectionkey_ops)throws Exception
{
ssc.register(sckt_manager,selectionkey_ops);
}}
public class HelloClient
{
public void coreClientChat()
{
Clientcore t=new Clientcore();
new Thread(t).start();
}
public static void main(String[] args)throws Exception
{
HelloClient cl= new HelloClient();
cl.coreClientChat();
}}
public class HelloServer
{
public void coreServerChat()
{
ServerCore t=new ServerCore();
new Thread(t).start();
}
public static void main(String[] args)
{
HelloServer st= new HelloServer();
st.coreServerChat();
}}
© Stack Overflow or respective owner