Question: Im trying to set up the client and server to the GUI I have created. Right now the client and server programs work well but
Im trying to set up the client and server to the GUI I have created. Right now the client and server programs work well but I am having a hard time trying to set it up with the GUI.
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Client
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Frame();
}
});
try
{
Socket sock = new Socket("localhost", 444);
SendThread sendThread = new SendThread(sock);
Thread thread = new Thread(sendThread);
thread.start();
RecieveThread recieveThread = new RecieveThread(sock);
Thread thread2 = new Thread(recieveThread);
thread2.start();
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
class RecieveThread implements Runnable
{
Socket sock = null;
BufferedReader recieve = null;
public RecieveThread(Socket sock)
{
this.sock = sock;
}// end constructor
public void run()
{
try
{
recieve = new BufferedReader(new InputStreamReader(this.sock.getInputStream()));// get inputstream
String msgRecieved = null;
GUI.dispincmsg(msgRecieved);
while ((msgRecieved = recieve.readLine()) != null)
{
System.out.println("From Server: " + msgRecieved);
System.out.println("Please enter something to send to server..");
}
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}// end run
}// end class recievethread
class SendThread implements Runnable
{
Socket sock = null;
PrintWriter print = null;
BufferedReader brinput = null;
public SendThread(Socket sock)
{
this.sock = sock;
}// end constructor
public void run()
{
try
{
if (sock.isConnected())
{
System.out.println("Client connected to "+ sock.getInetAddress() + " on port " + sock.getPort());
this.print = new PrintWriter(sock.getOutputStream(), true);
while (true)
{
System.out.println("Type your message to send to server..type 'EXIT' to exit");
brinput = new BufferedReader(new InputStreamReader(System.in));
String msgtoServerString = null;
msgtoServerString = brinput.readLine();
this.print.println(msgtoServerString);
this.print.flush();
if (msgtoServerString.equals("EXIT"))
break;
}// end while
sock.close();
}
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}// end run method
}// end class
import java.io.*;
import java.net.*;
import java.lang.*;
public class Server
{
public static void main(String[] args) throws IOException
{
final int port = 444;
System.out.println("Server waiting for connection on port " + port);
ServerSocket ss = new ServerSocket(port);
Socket clientSocket = ss.accept();
System.out.println("Recieved connection from " + clientSocket.getInetAddress() + " on port "
+ clientSocket.getPort());
// create two threads to send and recieve from client
RecieveFromClientThread recieve = new RecieveFromClientThread(clientSocket);
Thread thread = new Thread(recieve);
thread.start();
SendToClientThread send = new SendToClientThread(clientSocket);
Thread thread2 = new Thread(send);
thread2.start();
}
}
class RecieveFromClientThread implements Runnable
{
Socket clientSocket = null;
BufferedReader brBufferedReader = null;
public RecieveFromClientThread(Socket clientSocket)
{
this.clientSocket = clientSocket;
}// end constructor
public void run()
{
try
{
brBufferedReader = new BufferedReader(new InputStreamReader(
this.clientSocket.getInputStream()));
String messageString;
while (true)
{
while ((messageString = brBufferedReader.readLine()) != null)
{// assign
// message
// from
// client
// to
// messageString
if (messageString.equals("EXIT"))
{
break;// break to close socket if EXIT
}
System.out.println("From Client: " + messageString);// print
// the
// message
// from
// client
System.out.println("Please enter something to send back to client..");
}
this.clientSocket.close();
System.exit(0);
}
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}// end class RecieveFromClientThread
class SendToClientThread implements Runnable
{
PrintWriter pwPrintWriter;
Socket clientSock = null;
public SendToClientThread(Socket clientSock)
{
this.clientSock = clientSock;
}
public void run()
{
try
{
pwPrintWriter = new PrintWriter(new OutputStreamWriter(
this.clientSock.getOutputStream()));// get outputstream
while (true) {
String msgToClientString = null;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));// get userinput
msgToClientString = input.readLine();// get message to send to
// client
pwPrintWriter.println(msgToClientString);// send message to
// client with
// PrintWriter
pwPrintWriter.flush();// flush the PrintWriter
System.out.println("Please enter something to send back to client..");
}// end while
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}// end run
}// end class SendToClientThread
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
public class GUI extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
JFrame window = new JFrame("Hello World");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(500,500));
setResizable(false);
this.setLayout(new BorderLayout());
JPanel BottomPanel = new JPanel();
JPanel BottomPanel1 = new JPanel();
JPanel topPanel = new JPanel();
JPanel centerPanel1 = new JPanel();
JPanel centerPanel2 = new JPanel();
JTextArea textArea;
JTextArea textArea1;
JTextArea textArea11;
JLabel label = new JLabel("User Name:", JLabel.LEFT);
JLabel label1 = new JLabel("Online Users", JLabel.CENTER);
JLabel label2 = new JLabel("Lawrence", JLabel.LEFT);
JLabel label3 = new JLabel("CS 936", JLabel.CENTER);
BottomPanel.setPreferredSize(new Dimension(6500,55));
BottomPanel1.setPreferredSize(new Dimension(6500,55));
topPanel.setPreferredSize(new Dimension(25,25));
centerPanel1.setMaximumSize(new java.awt.Dimension(270, 65));
centerPanel1.setMinimumSize(new java.awt.Dimension(270, 65));
centerPanel1.setPreferredSize(new java.awt.Dimension(270, 65));
BottomPanel.setMaximumSize(new java.awt.Dimension(370, 60));
BottomPanel.setMinimumSize(new java.awt.Dimension(370, 60));
BottomPanel.setPreferredSize(new java.awt.Dimension(370, 60));
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, BottomPanel, BottomPanel1);
JSplitPane sp3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerPanel1, centerPanel2);
sp3.setResizeWeight(0.5);
this.add(sp3, BorderLayout.CENTER);
window.add(label);
this.add(topPanel, BorderLayout.NORTH);
this.add(sp2, BorderLayout.SOUTH);
textArea= new JTextArea(2,22);
BottomPanel.add(textArea);
BottomPanel1.add(label3);
centerPanel2.add(label1);
topPanel.add(label);
topPanel.add(label2);
textArea1= new JTextArea(21,30);
centerPanel1.add(textArea1);
textArea11= new JTextArea(22,20);
centerPanel2.add(textArea11);
JButton button = new JButton("Send Message");
BottomPanel.add(button);
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Clicked !");
SendThread client = new SendThread(null);
client.run();
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void dispincmsg(String msgRecieved)
{
System.out.println("From Server: " + msgRecieved);
System.out.println("Please enter something to send to server..");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
