Aim: Write a java application which implements client and server sockets using TCP protocol.
TCPServer.java
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
import java.io.*;
class TCPServerS extends JFrame {
JTextArea jta;
JButton btnSend;
JTextField jtf;
ServerSocket ss;
Socket serverSocket;
JLabel lblIpl;
JTextField tfIp;
JLabel lblPort;
JTextField tfPort;
TCPServerS() {
super("Server");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
setSize(500, 500);
JPanel p1 = new JPanel(new FlowLayout());
JPanel p2 = new JPanel(new FlowLayout());
jta = new JTextArea(10, 20);
jta.setEditable(false);
lblPort = new JLabel("Enter Port");
tfPort = new JTextField(5);
lblPort.setSize(10, 10);
tfPort.setSize(10, 10);
p1.add(lblPort);
p1.add(tfPort);
p1.add(new JScrollPane(jta));
btnSend = new JButton("Send");
btnSend.addActionListener(new SendHandler());
jtf = new JTextField(20);
jtf.addActionListener(new SendHandler());
p2.add(jtf);
p2.add(btnSend);
add(p1);
add(p2);
setVisible(true);
tfPort.addActionListener(new PorHandler());
while (true) {
recieve();
}
}
class PorHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String s = startServer(Integer.parseInt(tfPort.getText()));
System.out.println(s);
} catch (Exception ex) {
}
}
}
private String startServer(int port) throws Exception {
ss = new ServerSocket(port);//enter port number.
serverSocket = ss.accept();
return "Started";
}
private void recieve() {
try {
DataInputStream dis = new DataInputStream(serverSocket.getInputStream());
InetAddress i = serverSocket.getInetAddress();
String host = i.getHostAddress();
String str = dis.readUTF();//return string
jta.append(host+" : "+str + "\n");
} catch (Exception ex) {
}
}
class SendHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
DataOutputStream dos = new DataOutputStream(serverSocket.getOutputStream());
dos.writeUTF(jtf.getText());
jta.append("Me : "+jtf.getText()+"\n");
jtf.setText(null);
} catch (Exception e1) {
}
}
}
}
class ServerMain {
public static void main(String[] args) {
TCPServerS ch = new TCPServerS();
}
}
TCPClient.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
class TCPClient extends JFrame {
JTextArea jta;
JButton btnSend;
JButton btnConnect;
JTextField jtf;
JLabel lblPort;
JLabel lblIP;
JTextField tfPort;
JTextField tfIp;
JLabel lblConnect;
Socket clientSocket;
TCPClient() {
super("Client");
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 2));
setSize(460, 320);
JPanel p1 = new JPanel();
lblConnect = new JLabel();
lblIP = new JLabel("IP Address :");
lblPort = new JLabel("Port :");
tfIp = new JTextField(10);
tfPort = new JTextField(10);
jta = new JTextArea(10, 35);
jta.setEditable(false);
btnConnect = new JButton("Connect");
btnSend = new JButton("Send");
btnSend.addActionListener(new SendHandler());
btnConnect.addActionListener(new ConnectHandler());
jtf = new JTextField(20);
jtf.addActionListener(new SendHandler());
p1.add(lblIP);
p1.add(tfIp);
p1.add(lblPort);
p1.add(tfPort);
p1.add(btnConnect);
p1.add(lblConnect);
p1.add(new JScrollPane(jta));
p1.add(jtf);
p1.add(btnSend);
add(p1);
setVisible(true);
while (true) {
recieve();
}
} catch (Exception e3) {
}
}
public String connect(String ip, int port) throws Exception {
clientSocket = new Socket(ip, port);
return "connected";
}
public void recieve() {
try {
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
InetAddress i = clientSocket.getInetAddress();
String h = i.getCanonicalHostName();
String str = dis.readUTF();
jta.append(h + ": " + str + "\n");
} catch (Exception e5) {
}
}
class SendHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeUTF(jtf.getText());
jta.append("Me:" + jtf.getText() + "\n");
jtf.setText(null);
} catch (Exception e1) {
}
}
}
class ConnectHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String str = connect(tfIp.getText(), Integer.parseInt(tfPort.getText()));
lblConnect.setText(str);
System.out.print(str);
} catch (Exception e2) {
}
}
}
}
class ClientMain {
public static void main(String[] args) {
TCPClient c = new TCPClient();
}
}
TCPServer.java
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
import java.io.*;
class TCPServerS extends JFrame {
JTextArea jta;
JButton btnSend;
JTextField jtf;
ServerSocket ss;
Socket serverSocket;
JLabel lblIpl;
JTextField tfIp;
JLabel lblPort;
JTextField tfPort;
TCPServerS() {
super("Server");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
setSize(500, 500);
JPanel p1 = new JPanel(new FlowLayout());
JPanel p2 = new JPanel(new FlowLayout());
jta = new JTextArea(10, 20);
jta.setEditable(false);
lblPort = new JLabel("Enter Port");
tfPort = new JTextField(5);
lblPort.setSize(10, 10);
tfPort.setSize(10, 10);
p1.add(lblPort);
p1.add(tfPort);
p1.add(new JScrollPane(jta));
btnSend = new JButton("Send");
btnSend.addActionListener(new SendHandler());
jtf = new JTextField(20);
jtf.addActionListener(new SendHandler());
p2.add(jtf);
p2.add(btnSend);
add(p1);
add(p2);
setVisible(true);
tfPort.addActionListener(new PorHandler());
while (true) {
recieve();
}
}
class PorHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String s = startServer(Integer.parseInt(tfPort.getText()));
System.out.println(s);
} catch (Exception ex) {
}
}
}
private String startServer(int port) throws Exception {
ss = new ServerSocket(port);//enter port number.
serverSocket = ss.accept();
return "Started";
}
private void recieve() {
try {
DataInputStream dis = new DataInputStream(serverSocket.getInputStream());
InetAddress i = serverSocket.getInetAddress();
String host = i.getHostAddress();
String str = dis.readUTF();//return string
jta.append(host+" : "+str + "\n");
} catch (Exception ex) {
}
}
class SendHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
DataOutputStream dos = new DataOutputStream(serverSocket.getOutputStream());
dos.writeUTF(jtf.getText());
jta.append("Me : "+jtf.getText()+"\n");
jtf.setText(null);
} catch (Exception e1) {
}
}
}
}
class ServerMain {
public static void main(String[] args) {
TCPServerS ch = new TCPServerS();
}
}
TCPClient.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
class TCPClient extends JFrame {
JTextArea jta;
JButton btnSend;
JButton btnConnect;
JTextField jtf;
JLabel lblPort;
JLabel lblIP;
JTextField tfPort;
JTextField tfIp;
JLabel lblConnect;
Socket clientSocket;
TCPClient() {
super("Client");
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 2));
setSize(460, 320);
JPanel p1 = new JPanel();
lblConnect = new JLabel();
lblIP = new JLabel("IP Address :");
lblPort = new JLabel("Port :");
tfIp = new JTextField(10);
tfPort = new JTextField(10);
jta = new JTextArea(10, 35);
jta.setEditable(false);
btnConnect = new JButton("Connect");
btnSend = new JButton("Send");
btnSend.addActionListener(new SendHandler());
btnConnect.addActionListener(new ConnectHandler());
jtf = new JTextField(20);
jtf.addActionListener(new SendHandler());
p1.add(lblIP);
p1.add(tfIp);
p1.add(lblPort);
p1.add(tfPort);
p1.add(btnConnect);
p1.add(lblConnect);
p1.add(new JScrollPane(jta));
p1.add(jtf);
p1.add(btnSend);
add(p1);
setVisible(true);
while (true) {
recieve();
}
} catch (Exception e3) {
}
}
public String connect(String ip, int port) throws Exception {
clientSocket = new Socket(ip, port);
return "connected";
}
public void recieve() {
try {
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
InetAddress i = clientSocket.getInetAddress();
String h = i.getCanonicalHostName();
String str = dis.readUTF();
jta.append(h + ": " + str + "\n");
} catch (Exception e5) {
}
}
class SendHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeUTF(jtf.getText());
jta.append("Me:" + jtf.getText() + "\n");
jtf.setText(null);
} catch (Exception e1) {
}
}
}
class ConnectHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
String str = connect(tfIp.getText(), Integer.parseInt(tfPort.getText()));
lblConnect.setText(str);
System.out.print(str);
} catch (Exception e2) {
}
}
}
}
class ClientMain {
public static void main(String[] args) {
TCPClient c = new TCPClient();
}
}
No comments:
Post a Comment