Server.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server extends JFrame{
JTextArea jtxt ;
JTextField jfld ;
JPanel jp ;
JScrollPane jsp ;
ArrayList
DatagramPacket dp ;
DatagramSocket ds ;
ReceiveClients clientThread ;
String message ;
private Runnable runnable = new Runnable(){
public void run() {
clientThread.stop() ;
clientDtls.clear() ;
if(! ds.isClosed()) ds.close() ;
System.out.println("\n\nServer shutdown gracefully.......\n\n") ;
}
};
public Server(){
setServer() ;
clientDtls = new ArrayList
clientDtls.ensureCapacity(20) ;
clientThread = new ReceiveClients(clientDtls,ds) ;
initGUI() ;
}
private void initGUI(){
this.setVisible(true) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
this.setTitle("SERVER>>>>>>") ;
this.setLocation(200,200) ;
jtxt = new JTextArea(30,20) ;
jfld = new JTextField(20) ;
jp = new JPanel(true) ;
jsp = new JScrollPane(jtxt) ;
jfld.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent ke){
if(ke.getKeyChar() == '\n'){
message = jfld.getText() ;
jfld.setText("") ;
jfld.setEditable(false) ;
if(! message.equals(""))
sendPackets() ;
jfld.setEditable(true) ;
jtxt.append(message+"\n") ;
}
}
}
);
jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS)) ;
jp.add(jsp) ;
jp.add(jfld) ;
jtxt.setEditable(false) ;
jtxt.setFocusable(false) ;
this.getContentPane().add(jp) ;
this.pack() ;
}
public void setServer(){
try {
ds = new DatagramSocket(2000) ;
dp = new DatagramPacket(new byte[256],256) ;
} catch (SocketException e) {
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(runnable)) ;
}
private void sendPackets(){
int size = clientDtls.size() ;
ClientDtls cd ;
int i = 0 ;
while(i
dp.setAddress(cd.getAddress()) ;
dp.setPort(cd.getPort()) ;
dp.setData(message.getBytes()) ;
dp.setLength(message.length()) ;
try {
ds.send(dp) ;
} catch (IOException e) {
e.printStackTrace();
i++ ;
continue ;
}
i++ ;
}
}
public static void main(String[] args){
Server server = new Server() ;
}
}
class ReceiveClients implements Runnable{
private volatile boolean flag ;
private ArrayList
private DatagramSocket ds ;
private DatagramPacket input ;
public ReceiveClients(ArrayList
clientDtls = ref ;
this.ds = ds ;
flag = true ;
input = new DatagramPacket(new byte[256],256) ;
Thread t = new Thread(this,"Receive_Clients") ;
t.start() ;
}
public void run() {
while(flag){
try {
ds.receive(input) ;
String request = new String(input.getData(),0,input.getLength(),"ASCII") ;
request = request.trim() ;
InetAddress ia = input.getAddress() ;
int port = input.getPort() ;
ClientDtls cd = new ClientDtls(ia,port) ;
if(request.equals("CONNECT")){
//if(! clientDtls.contains(sa))
clientDtls.add(cd) ;
}
else if(request.equals("RELEASE")){
clientDtls.remove(cd) ;
}
}catch(SocketException se){
System.out.println("Trying to close the server.....") ;
stop() ;
return ;
}
catch (IOException e) {
e.printStackTrace();
continue ;
}
}
}
public void stop(){
flag = false ;
}
}
class ClientDtls{
private InetAddress address ;
private int port ;
public ClientDtls(InetAddress address,int port){
this.address = address ;
this.port = port ;
}
public boolean equals(ClientDtls cd){
if(cd.getAddress().equals(address)){
if(cd.getPort() == port)
return true ;
}
return false ;
}
public int getPort(){
return port ;
}
public InetAddress getAddress(){
return address ;
}
}
Client.java
import javax.swing.* ;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Client extends JFrame{
JTextArea jtxt ;
JPanel jp ;
JScrollPane jsp ;
DatagramPacket dp ;
DatagramSocket ds ;
private Runnable run = new Runnable(){
public void run() {
if(! ds.isClosed()) ds.close() ;
releaseClient() ;
System.out.println("\nSystem shutdown gracefully......\n\n") ;
}
};
public Client(){
startClient() ;
initGUI() ;
}
private void initGUI(){
this.setVisible(true) ;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
this.setTitle("CLIENT>>>>>>") ;
this.setLocation(300,200) ;
jtxt = new JTextArea(30,20) ;
jp = new JPanel(true) ;
jsp = new JScrollPane(jtxt) ;
jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS)) ;
jp.add(jsp) ;
jtxt.setEditable(false) ;
jtxt.setFocusable(false) ;
this.getContentPane().add(jp) ;
this.pack() ;
}
private void startClient(){
try {
ds = new DatagramSocket() ;
} catch (SocketException e) {
e.printStackTrace();
}
String connect = "CONNECT" ;
dp = new DatagramPacket(new byte[256],256) ;
dp.setData(connect.getBytes()) ;
dp.setLength(connect.length()) ;
try {
dp.setAddress(InetAddress.getLocalHost()) ;
} catch (UnknownHostException e) {
e.printStackTrace();
}
dp.setPort(2000) ;
try {
ds.send(dp) ;
} catch (IOException e) {
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(run)) ;
}
private void releaseClient(){
String message = "RELEASE" ;
dp.setData(message.getBytes()) ;
dp.setLength(message.length()) ;
try {
ds.send(dp) ;
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeMessage(){
String message = "" ;
try {
message = new String(dp.getData(),0,dp.getLength(),"ASCII") ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
jtxt.append(message+"\n") ;
}
public void receiveClient(){
while(true){
dp.setData(new byte[256]) ;
dp.setLength(256) ;
try {
ds.receive(dp) ;
writeMessage() ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
Client client = new Client() ;
client.receiveClient() ;
}
}
No comments:
Post a Comment