package serverchat;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class myFrame
extends JFrame{
/** Creates a new instance of myFrame */
private server ChatServer;
public myFrame() {
setTitle("Server");
setSize(560,400);
cp.
add(new JLabel("Server History"));
cp.add(myChatHistory);
cp.
add(new JLabel("Chat Box : "));
cp.add(myUserHistory);
cp.add(Send);
cp.add(Start);
ChatServer=new server();
ChatServer.start();
}
});
ChatServer.SendMassage(ServerAddress.getHostName()+" < Server > "+UserText.getText());
}
});
setDefaultCloseOperation
(JFrame.
EXIT_ON_CLOSE);
setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main
(String[] args
) {
// TODO code application logic here
new myFrame();
}
public class server
extends Thread {
private static final int PORT=9999;
private ByteBuffer ReadBuffer;
private ByteBuffer WriteBuffer;
public ServerSocketChannel SSChan;
private Selector ReaderSelector;
private CharsetDecoder asciiDecoder;
public server() {
ReadBuffer=ByteBuffer.allocateDirect(300);
WriteBuffer=ByteBuffer.allocateDirect(300);
asciiDecoder = Charset.forName( "US-ASCII").newDecoder();
}
public void InitServer() {
try {
SSChan=ServerSocketChannel.open();
SSChan.configureBlocking(false);
System.
out.
println(ServerAddress.
toString());
SSChan.socket().bind(new InetSocketAddress(ServerAddress,PORT));
ReaderSelector=Selector.open();
ChatBox.setText(ServerAddress.getHostName()+"<Server> Started. \n");
ex.printStackTrace();
}
}
public void run() {
InitServer();
while(true) {
acceptNewConnection();
ReadMassage();
try {
ex.printStackTrace();
}
}
}
public void acceptNewConnection() {
SocketChannel newClient;
try {
while ((newClient = SSChan.accept()) != null) {
ChatServer.addClient(newClient);
sendBroadcastMessage(newClient,"Login from: " +newClient.socket().getInetAddress());
SendMassage(newClient,ServerAddress.getHostName()+"<server> welcome you !\n Note :To exit" +
" from server write 'quit' .\n");
}
e.printStackTrace();
}
}
public void addClient(SocketChannel newClient) {
Clients.add(newClient);
try {
newClient.configureBlocking(false);
newClient.
register(ReaderSelector,SelectionKey.
OP_READ,
new StringBuffer());
ex.printStackTrace();
}
}
public void SendMassage
(SocketChannel client ,
String messg
) {
prepareBuffer(messg);
channelWrite(client);
}
public void SendMassage
(String massg
) {
if(Clients.size()>0) {
for(int i=0;i<Clients.size();i++) {
SocketChannel client=(SocketChannel)Clients.get(i);
SendMassage(client,massg);
}
}
}
public void prepareBuffer
(String massg
) {
WriteBuffer.clear();
WriteBuffer.put(massg.getBytes());
WriteBuffer.putChar('\n');
WriteBuffer.flip();
}
public void channelWrite(SocketChannel client) {
long num=0;
long len=WriteBuffer.remaining();
while(num!=len) {
try {
num+=client.write(WriteBuffer);
ex.printStackTrace();
}
}
WriteBuffer.rewind();
}
public void sendBroadcastMessage
(SocketChannel client,
String mesg
) {
prepareBuffer(mesg);
while (i.hasNext()) {
SocketChannel channel = (SocketChannel)i.next();
if (channel != client) {
channelWrite(channel);
}
}
}
public void ReadMassage() {
try {
ReaderSelector.selectNow();
Set readkeys
=ReaderSelector.
selectedKeys();
while(iter.hasNext()) {
SelectionKey key=(SelectionKey) iter.next();
iter.remove();
SocketChannel client=(SocketChannel)key.channel();
ReadBuffer.clear();
long num=client.read(ReadBuffer);
if(num==-1) {
client.close();
Clients.remove(client);
sendBroadcastMessage(client,"logout: " +
client.socket().getInetAddress());
} else {
ReadBuffer.flip();
String data
= asciiDecoder.
decode(ReadBuffer
).
toString();
ReadBuffer.clear();
str.append(data);
if ((line.indexOf("\n") != -1) || (line.indexOf("\r") != -1)) {
line = line.trim();
if (line.endsWith("quit")) {
client.close();
Clients.remove(client);
ChatBox.append("Logout: " + client.socket().getInetAddress());
sendBroadcastMessage(client,"Logout: "
+ client.socket().getInetAddress());
ChatBox.append(""+'\n');
} else {
ChatBox.append(client.socket().getInetAddress() + ": " + line);
sendBroadcastMessage(client,client.socket().getInetAddress()
+ ": " + line);
ChatBox.append(""+'\n');
str.delete(0,str.length());
}
}
}
}
ex.printStackTrace();
}
}
}
}