Aim: Write a java application which implements client and server sockets using UDP protocol
UDPclient.java
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class UDPClient {
JFrame frame;
JPanel panel;
JTextField field1, field2;
JTextArea area;
JScrollPane pane;
JLabel label;
JButton button;
public static void main(String[] args) {
UDPClient u = new UDPClient();
}
public UDPClient() {
frame = new JFrame("Text Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
panel = new JPanel();
panel.setLayout(null);
label = new JLabel("Desination IP:");
label.setBounds(10, 20, 100, 30);
panel.add(label);
field1 = new JTextField(20);
field1.setBounds(125, 25, 150, 20);
panel.add(field1);
label = new JLabel("Destination Port:");
label.setBounds(10, 50, 100, 30);
panel.add(label);
field2 = new JTextField(10);
field2.setBounds(125, 55, 100, 20);
panel.add(field2);
area = new JTextArea();
pane = new JScrollPane(area);
pane.setBounds(10, 100, 300, 200);
panel.add(pane);
button = new JButton("Send");
button.setBounds(235, 310, 75, 30);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
new SendRequest();
}
});
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true); }
public class SendRequest {
SendRequest() {
try {
DatagramSocket socket;
DatagramPacket packet;
InetAddress address;
socket = new DatagramSocket();
String dip = field1.getText();
address = InetAddress.getByName(dip);
String port = field2.getText();
int pnum = Integer.parseInt(port);
String mess = area.getText();
byte message[] = mess.getBytes();
System.out.println(mess + "Byte " + message);
packet = new DatagramPacket(message, message.length, address, pnum);
socket.send(packet);
area.setText("");
socket.close();
} catch (IOException io) {
}
}}
}
UDPserver.java
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class UDPServer {
JFrame frame;
JPanel panel;
JButton button1, button2;
JTextArea area;
JScrollPane pane;
Thread thread;
DatagramSocket socket;
public static void main(String[] args) {
UDPServer u = new UDPServer();
}
public UDPServer() {
frame = new JFrame("Text Server");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
panel = new JPanel();
panel.setLayout(null);
area = new JTextArea();
button1 = new JButton("Start");
button1.setBounds(210, 10, 75, 40);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
new StartThread(); }
});
panel.add(button1);
button2 = new JButton("Stop");
button2.setBounds(300, 10, 75, 40);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread.interrupted();
socket.close();
area.append("Server is stopped\n");
button1.setEnabled(true);
button2.setEnabled(false);
}
});
button2.setEnabled(false);
panel.add(button2);
pane = new JScrollPane(area);
pane.setBounds(10, 60, 365, 250);
panel.add(pane);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
public class StartThread implements Runnable {
StartThread() {
thread = new Thread(this);
thread.start();
button1.setEnabled(false);
button2.setEnabled(true);
}
public void run() {
try {
byte[] buffer = new byte[1024];
int port = 3333;
try {
socket = new DatagramSocket(port);
while (true) {
try {
area.append("Server is started\n");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
InetAddress client = packet.getAddress();
int clientPort = packet.getPort();
String message = new String(packet.getData(),0,packet.getLength()) ;
area.append(" Received " + message + " from " + client+":"+clientPort);
} catch (UnknownHostException ue) {
}
}
} catch (java.net.BindException b) {
}
} catch (IOException e) {
System.err.println(e);
}
}
}
}
Output:
No comments:
Post a Comment