Simple Client-Server program using Swing GUI - java

I'm making a simple, no thread Client-Server program where GUI has one button on both server and client side. When client presses button it changes text on a button to "C" and sends to server "C" string, so the button on the server side changes text to "C". Server works similarly to client but sends "S" instead of "C". They work in turns: when it's the client's turn, server's button is locked and he cannot change his button. Client always starts first.
When client presses button it works fine, but when server presses button it changes button to "S" on the server side but not on client's side. I know what I'm doing wrong.
Server code:
public class Serv implements ActionListener
{
private JButton button;
private boolean myTurn;
private ServerSocket sock;
private Socket s;
private BufferedReader input;
private PrintStream output;
public Serv() throws UnknownHostException, IOException
{
button = new JButton();
myTurn = false;
sock = new ServerSocket(9001);
s = null;
button = new JButton();
}
public void createGUI()
{
JFrame frame = new JFrame("TicTacToe - Server");
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(100, 100));
button = new JButton("");
button.setPreferredSize(new Dimension(100, 100));
button.setFont(new Font(button.getFont().getName(), button.getFont().getStyle(), 70));
button.setActionCommand("1");
button.addActionListener(this);
mainPanel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void startMyGame() throws IOException
{
createGUI();
s = sock.accept();
input = new BufferedReader(new InputStreamReader(s.getInputStream()));
output = new PrintStream(s.getOutputStream(), true);
while(true)
{
if(myTurn == false)
{
myTurn = true;
String out = input.readLine();
button.setText(out);
}
}
}
public static void main(String args[])
{
Serv tc = null;
try
{
tc = new Serv();
tc.startMyGame();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
tc.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
private void close() throws IOException
{
this.sock.close();
this.input.close();
this.output.close();
}
#Override
public void actionPerformed(ActionEvent e)
{
if(myTurn == true)
{
if(e.getActionCommand().equals("1"))
{
JButton b = (JButton) e.getSource();
b.setText("S");
output.println("S");
myTurn = false;
}
}
}
}
Client code:
public class Cli implements ActionListener
{
private JButton button;
private boolean myTurn;
private Socket sock;
private BufferedReader input;
private PrintStream output;
public Cli() throws UnknownHostException, IOException
{
button = new JButton();
myTurn = true;
sock = new Socket("127.0.0.1", 9001);
input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
output = new PrintStream(sock.getOutputStream(), true);
}
public void createGUI()
{
JFrame frame = new JFrame("TicTacToe - Client");
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(100, 100));
button = new JButton("");
button.setPreferredSize(new Dimension(100, 100));
button.setFont(new Font(button.getFont().getName(), button.getFont().getStyle(), 70));
button.setActionCommand("1");
button.addActionListener(this);
mainPanel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void startMyGame() throws IOException
{
createGUI();
while(true)
{
if(myTurn == false)
{
myTurn = true;
String out = input.readLine();
button.setText(out);
}
}
}
private void close() throws IOException
{
this.sock.close();
this.input.close();
this.output.close();
}
public static void main(String args[])
{
Cli tc = null;
try
{
tc = new Cli();
tc.startMyGame();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
tc.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(myTurn == true)
{
if(e.getActionCommand().equals("1"))
{
JButton b = (JButton) e.getSource();
if(!b.getText().equals("X") || !b.getText().equals("O"))
{
b.setText("C");
output.println("C");
myTurn = false;
}
}
}
}
}
I have deleted imports so the codes would be shorter.

Current issues with your code:
You're creating a Swing GUI and running it off of the Swing event dispatch thread or EDT. GUI's should be started on the event thread so that all Swing code is guaranteed to run on a single thread.
You've got a long-running while loop, and it is making Swing mutational calls, updating the state of a JButton. If this code were running on the Swing event thread, it would block/freeze the GUI. This block should be explicitly called in a background thread that is not the EDT, and all Swing calls should be queued onto the event thread as per the Lesson: Concurrency in Swing tutorial.
You're using a non-volatile boolean in different threads, risking the variable not being changed when it should be changed
You appear to be closing your streams immediately, preventing adequate communication between concerns.
Working on a cleaner example.......
For example:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.*;
public class SimpleServerClient {
private static final int PORT = 9001;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
SimpleServer server = new SimpleServer(PORT, "Server", false);
SimpleClient client = new SimpleClient(PORT, "Client", true);
server.createGui();
client.createGui();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
interface SimpleGui {
void sendLine(String nextLine);
}
// background thread handles listening to the Scanner
// which scans a Socket's InputStream
class MyWorker extends SwingWorker<Void, Void> {
public static final String LINE = "line";
private Scanner inputScanner;
private SimpleGui gui;
private String line = "";
public MyWorker(Scanner inputScanner, SimpleGui gui) {
this.inputScanner = inputScanner;
this.gui = gui;
}
#Override
protected Void doInBackground() throws Exception {
while (inputScanner.hasNext()) {
// get line from Scanner
// use the setter method in case we want to use a PropertyChangeListener later
setLine(inputScanner.nextLine());
// send line to the GUI
gui.sendLine(getLine());
}
return null;
}
public String getLine() {
return line;
}
// again rigged up to allow use of PropertyChangeListeners
public void setLine(String line) {
this.line = line;
firePropertyChange(LINE, null, line);
}
}
// code that both the client and server GUI classes share
abstract class DefaultGui implements SimpleGui {
// this guy ***must**** be volitile!
private volatile boolean myTurn;
protected Scanner inputScanner;
protected PrintStream out;
protected JButton button = new JButton("Blank");
protected Socket socket;
protected String name;
protected int port;
public DefaultGui(int port, String name, boolean myTurn) {
this.port = port;
this.name = name;
this.myTurn = myTurn;
}
#Override
public void sendLine(String nextLine) {
button.setText(nextLine);
myTurn = true;
}
public void createGui() {
button.addActionListener(e -> actionPerformed(e));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.add(button);
JFrame frame = new JFrame(getName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
protected void actionPerformed(ActionEvent e) {
if (!myTurn) {
return;
}
out.println(getName());
button.setText(getName());
myTurn = false;
}
public String getName() {
return name;
}
}
class SimpleServer extends DefaultGui {
private ServerSocket serverSocket;
public SimpleServer(int port, String name, boolean myTurn) throws IOException {
super(port, name, myTurn);
serverSocket = new ServerSocket(port);
new Thread(() -> {
try {
// accept() blocks the current thread, so must be called on a background thread
socket = serverSocket.accept();
inputScanner = new Scanner(socket.getInputStream());
out = new PrintStream(socket.getOutputStream(), true);
new MyWorker(inputScanner, this).execute();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
class SimpleClient extends DefaultGui {
public SimpleClient(int port, String name, boolean myTurn) throws IOException {
super(port, name, myTurn);
socket = new Socket("localhost", port);
inputScanner = new Scanner(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
new MyWorker(inputScanner, this).execute();
}
}

Related

JComboBox dropdown stays empty even when I add elements to it

In the BuildGUI method in class ChatFrame, there is a JCombobox named dropdown which I tried to populate with an arraylist from class populate. But whenever I run the class, list successfully fetches the data I want to add to JComboBox but it doesn't add it to the combobox.
The combobox stays empty. I tried to add element to it by dropdown.addItem("String");. It worked but why it is not working when I use araylist from another class or same class using for loop?
Here's the code:
// Class to manage Client chat Box.
public class ChatClient {
/** Chat client access */
static class ChatAccess extends Observable {
private Socket socket;
private OutputStream outputStream;
#Override
public void notifyObservers(Object arg) {
super.setChanged();
super.notifyObservers(arg);
}
/** Create socket, and receiving thread */
public void InitSocket(String server, int port) throws IOException {
socket = new Socket(server, port);
outputStream = socket.getOutputStream();
Thread receivingThread = new Thread() {
#Override
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
notifyObservers(line);
} catch (IOException ex) {
notifyObservers(ex);
}
}
};
receivingThread.start();
}
private static final String CRLF = "\r\n"; // newline
/** Send a line of text */
public void send(String text) {
try {
outputStream.write((text + CRLF).getBytes());
outputStream.flush();
} catch (IOException ex) {
notifyObservers(ex);
}
}
/** Close the socket */
public void close() {
try {
socket.close();
} catch (IOException ex) {
notifyObservers(ex);
}
}
}
/** Chat client UI */
static class ChatFrame extends JFrame implements Observer {
private JTextArea textArea;
private JTextField inputTextField;
private JButton sendButton;
private ChatAccess chatAccess;
private JList<String> list;
public DefaultComboBoxModel<String> mod;
private JScrollPane jscrollpane;
public static JComboBox<String> dropdown;
//clientThread ct=new clientThread();
public static ArrayList<String> usr=new ArrayList<String>();
public static String array[]=new String[usr.size()];
public ChatFrame(ChatAccess chatAccess) {
this.chatAccess = chatAccess;
chatAccess.addObserver(this);
buildGUI();
}
/** Builds the user interface */
private void buildGUI() {
for(int j =0;j<usr.size();j++){
array[j] = usr.get(j);
System.out.print("testing"+array[j]);
}
//usr=clientThread.acces();
//System.out.print("test"+usr);
mod = new DefaultComboBoxModel<String>();
list = new javax.swing.JList<>(mod);
list.setBounds(30, 30, 30, 30);
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
add(new JScrollPane(textArea), BorderLayout.WEST);
jscrollpane=new JScrollPane();
jscrollpane.setViewportView(list);
add(new JScrollPane(list), BorderLayout.EAST);
Box box = Box.createHorizontalBox();
add(box, BorderLayout.SOUTH);
inputTextField = new JTextField();
dropdown=new JComboBox(array);
dropdown.setBounds(24, 138, 72, 20);
dropdown.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXX");
sendButton = new JButton("Send");
box.add(inputTextField);
box.add(sendButton);
box.add(dropdown);
box.add(populate.dl);
ActionListener sendListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = inputTextField.getText();
if (str != null && str.trim().length() > 0)
chatAccess.send(str);
inputTextField.selectAll();
inputTextField.requestFocus();
inputTextField.setText("");
}
};
inputTextField.addActionListener(sendListener);
sendButton.addActionListener(sendListener);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
chatAccess.close();
}
});
}
/** Updates the UI depending on the Object argument */
public void update(Observable o, Object arg) {
final Object finalArg = arg;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(finalArg.toString());
textArea.append("\n");
}
});
}
}
static class populate{
public static ArrayList<String> naam=new ArrayList<String>();
public static JComboBox<String> dl=new JComboBox<String>();
public void getarr(String name) {
naam.add(name);
System.out.print("populate"+naam);
dl=new JComboBox<String>();
dl.addItem(name);
//ChatFrame.usr.add(name);
//System.out.print("chatframeclass"+ChatFrame.usr);
}
}
public static void main(String[] args) {
String server = args[0];
int port =2222;
ChatAccess access = new ChatAccess();
JFrame frame = new ChatFrame(access);
frame.setTitle("MyChatApp - connected to " + server + ":" + port);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
try {
access.InitSocket(server,port);
} catch (IOException ex) {
System.out.println("Cannot connect to " + server + ":" + port);
ex.printStackTrace();
System.exit(0);
}
}
}
The problem with your code is that array is empty when you are adding it to dropdown in the following line:
dropdown=new JComboBox(array);
You can verify this by printing array e.g. System.out.println(Arrays.toString(array)); before adding it to dropdown.
Given below is the minimal reproducible example of how adding a String array to JComboBox<String> works:
String[]arr= {"Hello","Hi","Bye"};
dropdown = new JComboBox(arr);
In the GUI, you will see that dropdown has been populated with the elements of arr[]. If you replace the above code as follows
String[]arr= {};
dropdown = new JComboBox(arr);
you will find that dropdown has become empty.

GUI is not responding to button clicks

I'm trying to write a simple port scanner in Java. It works, but the entire window stops responding to clicks once the scanning begins. This won't allow me to use the stop button to pause the operation or exit the window. Here is my code:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.swing.*;
import java.awt.event.*;
public class GUIScanner {
JFrame frame = new JFrame("Port Scanner");
JTextField textField = new JTextField("",20);
JPanel panel = new JPanel();
JButton button = new JButton("Scan");
JButton button2 = new JButton("Stop");
JLabel label = new JLabel("");
boolean stopped = false;
public GUIScanner() {
initialize();
}
public static void main(String[] args) {
GUIScanner scanner = new GUIScanner();
}
public void initialize() {
panel.add(textField);
panel.add(button);
panel.add(button2);
frame.add(panel);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String address = textField.getText().toString();
scanHost(address, 200);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!stopped) {
stopped = true;
button2.setText("Resume");
} else {
stopped = false;
button2.setText("Stop");
}
}
});
}
public void scanHost(String ip, int timeout) {
while(!stopped) {
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!stopped) {
stopped = true;
button2.setText("Resume");
} else {
stopped = false;
button2.setText("Stop");
}
}
});
for(int port = 0; port <= 65535; port++) {
frame.setTitle("Scanning port " + port + " of 65535");
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
System.out.println(port);
} catch (IOException e) {
}
}
}
}
}
I'm not sure if the issue is a threading issue or if it is something else in my code. I'm fairly inexperienced with Java. I'm using Eclipse Oxygen if that helps.
Use a SwingWorker to run another thread in the background. This prevents Swing from being blocked when you pause the background thread. Here is the corrected version of the port scanner code:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.swing.*;
import java.awt.event.*;
public class GUIScanner {
JFrame frame = new JFrame("Port Scanner");
JTextField textField = new JTextField("", 20);
JPanel panel = new JPanel();
JButton button = new JButton("Scan");
JButton button2 = new JButton("Stop");
JLabel label = new JLabel("");
boolean stopped = false;
PausableSwingWorker<Void, String> scanningWorker;
abstract class PausableSwingWorker<K, V> extends SwingWorker<K, V> {
private volatile boolean isPaused;
final Object lock = new Object();
public final void pause() {
if (!isPaused() && !isDone()) {
isPaused = true;
}
}
public final void resume() {
if (isPaused() && !isDone()) {
isPaused = false;
synchronized(lock) {
lock.notify();
}
}
}
public final boolean isPaused() {
return isPaused;
}
}
public GUIScanner() {
initialize();
}
public static void main(String[] args) {
GUIScanner scanner = new GUIScanner();
}
public void initialize() {
panel.add(textField);
panel.add(button);
panel.add(button2);
frame.add(panel);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String address = textField.getText().toString();
scanHost(address, 200);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!stopped) {
stopped = true;
button2.setText("Resume");
scanningWorker.pause();
} else {
stopped = false;
button2.setText("Stop");
scanningWorker.resume();
}
}
});
}
public void scanHost(String ip, int timeout) {
scanningWorker = new PausableSwingWorker<Void, String>() {
#Override
public Void doInBackground() {
for (int port = 0; port <= 65535; port++) {
if (!isPaused()) {
frame.setTitle("Scanning port " + port + " of 65535");
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
System.out.println("Port " + port + " is open");
} catch (IOException e) {
}
}
else {
synchronized(lock) {
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
#Override
public void done() {
}
};
scanningWorker.execute();
}
}
Welcome to the wonderful world of "Honey, I blocked the Event Dispatching Thread".
Start by taking a look at Concurrency in Swing for more of an overview.
Essentially, Swing is single threaded and NOT thread safe. This means you should never perform long running or blocking operations within the context of the EDT, but also, you should not update the UI from outside of the context of the EDT.
Instead, you should consider using something like SwingWorker, which allows you to run blocking/long running operations in the background, but provides functionality to publish and process updates in the EDT

Problems when closing and reopening a JFrame that uses a SwingWorker to send and receive messages

I am developing a messaging application with sockets. I have an initial JFrame that allows the user to choose a contact to connect to, and lets them choose if they'd like to be the client or the server. When they make this choice, it calls a method in which creates the chat window frame, and also uses a SwingWorker to initialise a connection, create an action listner on the send buton of the chat window so that it sends messages when pressed, and creates a SocketListener that is always listening while the window is open.
This works just fine, however if I close the chat window and then reopen it by choosing server or client again, nothing happens - the frame loads up however I cannot send any messages on it.
I'm not sure what the problem is here, could it be that i'm not disposing of the swing worker or the frame properly? Any help would be greatly appreciated and apologies for my lack of knowledge.
Here is the relevent code for this:
public static void server() {
JButton server = new JButton();
server.setText("Server");
server.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String name = nameChoice;
String ip = ipChoice;
String port = portChoice;
try {
server2(port);
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
panel.add(server);
}
public static void server2(String port) throws Exception {
gui.frame();
SwingWorker worker = new SwingWorker() {
#Override
protected Void doInBackground() throws Exception {
sockets.serverSetup(port);
gui.sendButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String msg = gui.newMsg.getText();
gui.newMsg.setText(null);
try {
sockets.sendMsg(msg);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
while(gui.open==true) {
sockets.receiveMsg();
}
sockets.closeConnection();
System.out.println("Closed");
return null;
}
};
worker.execute();
}
network
package com.company;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
public class network {
static ServerSocket s;
static Socket s1;
static OutputStream s1out;
static DataOutputStream dos;
static InputStream s1in;
static chatWindowScreen gui = new chatWindowScreen();
static String st;
public static Vector sentVec = new Vector();
public static Vector receivedVec = new Vector();
public static Vector messages = new Vector();
public static void serverSetup(String port) throws IOException {
System.out.println("Test");
int portInt = Integer.parseInt(port);
s = new ServerSocket(portInt);
s1 = s.accept();
System.out.println("Server set up");
//sendMsg("Hi");
}
public static void clientSetup(String port, String ip) throws IOException {
System.out.println("Test");
int portInt = Integer.parseInt(port);
s1 = new Socket(ip, portInt);
System.out.println("Client set up");
//receiveMsg();
}
public static void closeConnection() throws IOException {
dos.close();
s1out.close();
s1.close();
}
public static void sendMsg(String message) throws IOException {
System.out.println("Test");
s1out = s1.getOutputStream();
dos = new DataOutputStream(s1out);
dos.writeUTF(message);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String t1 = sdf.format(cal.getTime());
message = t1 + " - " + message;
sentVec.add(message);
receivedVec.add("");
messages.add(message);
gui.sent.setText("Sent \n");
for (int i = 0; i < sentVec.size(); i++) {
gui.sent.append(sentVec.get(i) + "\n");
}
gui.received.setText("Received \n");
for (int i = 0; i < receivedVec.size(); i++) {
gui.received.append(receivedVec.get(i) + "\n");
}
System.out.println("complete");
}
public static void receiveMsg() throws IOException {
System.out.println("Test");
s1in = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1in);
st = (dis.readUTF());
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String t1 = sdf.format(cal.getTime());
st = t1 + " - " + st;
receivedVec.add(st);
sentVec.add("");
messages.add(st);
gui.received.setText("Received \n");
for (int i = 0; i < receivedVec.size(); i++) {
gui.received.append((String) receivedVec.get(i) + "\n");
}
gui.sent.setText("Sent \n");
for (int i = 0; i < sentVec.size(); i++) {
gui.sent.append((String) sentVec.get(i) + "\n");
}
}
public static Vector getSentVec() {
return sentVec;
}
public static Vector getReceivedVec() {
return receivedVec;
}
}
chatWindowScreen
package com.company;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.Vector;
public class chatWindowScreen {
static JFrame frame = new JFrame();
static JPanel panel = new JPanel();
static JTextArea sent;
static JTextArea received;
static JTextArea newMsg;
static JButton sendButton;
public static String msg;
static boolean open = true;
static network sockets = new network();
public static boolean close = false;
public void frame() throws Exception {
System.out.println("GUI opened");
frame.setTitle("Messaging Application");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent e) {
}
#Override
public void windowClosing(WindowEvent e) {
}
#Override
public void windowClosed(WindowEvent e) {
System.out.println("Closed");
panel.removeAll();
open=false;
}
#Override
public void windowIconified(WindowEvent e) {
}
#Override
public void windowDeiconified(WindowEvent e) {
}
#Override
public void windowActivated(WindowEvent e) {
}
#Override
public void windowDeactivated(WindowEvent e) {
}
});
sentMsg();
receivedMsg();
msgBox();
sendBtn();
save();
frame.add(panel);
frame.setVisible(true);
}
public static void sentMsg() {
sent = new JTextArea(15, 20);
sent.setText("Sent" + "\n");
sent.setLineWrap(true);
sent.setEditable(false);
JScrollPane pane = new JScrollPane(sent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(pane);
}
public static void receivedMsg() {
received = new JTextArea(15, 20);
received.setText("Received" + "\n");
received.setEditable(false);
received.setLineWrap(true);
JScrollPane pane1 = new JScrollPane(received, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(pane1);
}
public static void msgBox() {
newMsg = new JTextArea(1, 15);
newMsg.setText("");
newMsg.setLineWrap(true);
JScrollPane pane2 = new JScrollPane(newMsg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(pane2);
}
public static void sendBtn() {
sendButton = new JButton();
sendButton.setText("Send!");
/*sendButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
msg = newMsg.getText();
newMsg.setText(null);
SwingWorker worker = new SwingWorker() {
#Override
protected Object doInBackground() throws Exception {
sockets.sendMsg(msg);
return null;
}
};
worker.execute();
try {
sockets.sendMsg(msg);
} catch (IOException e1) {
e1.printStackTrace();
}
msg = null;
}
});*/
panel.add(sendButton);
}
public static void save(){
JButton save = new JButton();
save.setText("Save Conversation");
save.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
saveToFile save = new saveToFile();
}
});
panel.add(save);
}
}

Java error: java.net.BindException: Address already in use: JVM_Bindin server run class

When I run the client program I get this error. I checked to see if the port number is being used and already tried changing the port multiple times.
the server uses three classes:
the main and gui
class ServerGui extends Server implements ActionListener
{
public ServerGui() throws Exception
{
JFrame jfrm = new JFrame("Check in Desk");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(500, 500);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
//menu bar
JMenuBar jmb = new JMenuBar();
JMenu jmView = new JMenu("View");
// items
JMenu jmEdit = new JMenu("Edit");
// items
JMenu jmHelp = new JMenu("Help");
// items
jmb.add(jmView);
jmb.add(jmEdit);
jmb.add(jmHelp);
//add menuBar
jfrm.setJMenuBar(jmb);
}
public void actionPerformed(ActionEvent ae)
{
String comString = ae.getActionCommand();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try{
new ServerGui();
}
catch(Exception e)
{
System.out.println(e + "damn");
}
}
});
}
}
The server class
public class Server extends Thread
{
public Server() throws IOException
{
this.start();
}
public void run()
{
try
{
while(true)
{
ServerSocket socketOnWhichToListenForClients= new ServerSocket(9876);
Socket socketBackToClient = socketOnWhichToListenForClients.accept();
new ClientHandler(socketBackToClient);
}
}
catch(Exception e)
{
System.out.println(e + "in server run class");
}
}
}
client handler
public class ClientHandler extends Thread
{
private Socket socketBackToClient;
public ClientHandler(Socket socket)
{
socketBackToClient = socket;
this.start();
}
public void run()
{
try
{
InputStream is = socketBackToClient.getInputStream();
BufferedReader message = new BufferedReader(new InputStreamReader(is));
String input = message.readLine();
System.out.println(input);
socketBackToClient.close();
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
The client gui
public class ClientGui extends Client implements ActionListener
{
//private String name = "";
public ClientGui() throws Exception
{
name = JOptionPane.showInputDialog("Enter your name.");
if(name.equals(""))
return;
JFrame jfrm = new JFrame("Check in Desk");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 170);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
jfrm.setResizable(false);
//buttons
Font font1 = new Font("SansSerif",Font.PLAIN, 24);
Font font2 = new Font("SansSerif",Font.PLAIN, 20);
JButton jb1 = new JButton("Help");
jb1.setPreferredSize(new Dimension(100, 100));
jfrm.add(jb1);
JButton jb2 = new JButton("Check In");
jb2.setPreferredSize(new Dimension(125, 100));
jfrm.add(jb2);
jb1.setFont(font1);
jb2.setFont(font2);
//menu bar
JMenuBar jmb = new JMenuBar();
JMenu jmTools = new JMenu("Tools");
JMenuItem jmiName = new JMenuItem("Name");
JMenuItem jmiIP = new JMenuItem("Your IP");
jmTools.add(jmiName);
jmTools.add(jmiIP);
// items
JMenu jmEdit = new JMenu("Edit");
// items
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
// items
jmb.add(jmTools);
jmb.add(jmEdit);
jmb.add(jmHelp);
//add menuBar
jfrm.setJMenuBar(jmb);
jmiName.addActionListener(this);
jmiIP.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String confirmHelp = "";
String confirmCheckIn = "";
if(ae.getActionCommand().equals("Name"))
JOptionPane.showMessageDialog(null,"Your name is " + getClientName());
if(ae.getActionCommand().equals("Your IP"))
JOptionPane.showMessageDialog(null,"Your IP is " + getIpAddress());
if(ae.getActionCommand().equals("Help"))
{
confirmHelp = JOptionPane.showInputDialog("Are You Sure You Want Help?");
if(confirmHelp != null && confirmHelp.equalsIgnoreCase("yes"))
{
Help();
}
}
if(ae.getActionCommand().equals("Check In"))
{
confirmCheckIn = JOptionPane.showInputDialog("Are You Sure You Want To Check In?");
if(confirmCheckIn != null && confirmCheckIn.equalsIgnoreCase("yes"))
{
CheckIn();
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try{
new ClientGui();
Socket socket = new Socket(InetAddress.getLocalHost(), 9876); //new Client(); // add constructor with name or pc numb
}
catch(Exception e)
{
System.out.println("client closed");
}
}
});
}
}
The client
public class Client
{
public String name = "";
Socket clientSocket;
public Client() throws Exception
{
try
{
// clientSocket = new Socket(InetAddress.getLocalHost(), 9);
OutputStream os = clientSocket.getOutputStream();
PrintWriter pwrite = new PrintWriter(os, true);
pwrite.print("yolo");
}
catch(Exception e)
{
//System.out.println("Error cant connect"); add later
}
}
public void Help()
{
System.out.println("you pressed help");
}
public void CheckIn()
{
System.out.println("you pressed Check In");
}
public String getIpAddress()
{
String str = "";
try{
InetAddress ip = InetAddress.getLocalHost();
str = ip.toString();
}
catch(UnknownHostException e)
{
System.out.println("can not find IP address");
}
return str;
}
public String getClientName()
{
return name;
}
}
The problem is that you keep recreating the ServerSocket inside the while loop. Create it once, before the loop.
The problem is at below lines where same port is used in loop to create ServerSocket that results into below exception.
try {
while (true) {
ServerSocket socketOnWhichToListenForClients = new ServerSocket(9876);
}
} catch (Exception e) {
System.out.println(e + "in server run class");
}
Look at at the exception:
java.net.BindException: Address already in use: JVM_Bindin server run class
Don't forget to close the ServerSocket in the end. Use finally block to close it.

java how to get a string to a server (jave.net/sockets)

I've been working on building a chat server to learn about threading and using sockets. I used head first java as a guide (and I'm still confused about a couple of things, which is why I have this question probably :D) and the code below is what I have.
If you read my code, you'll see that I have clients connect to the server by clicking on a "connect" button. When they do that, a new jframe pops up that asks for their preferred name for the chat session. When they input that, it's saved in a String in the client code. My question is, how do I get this name over to the server, and make it so that every time the client sends a message, their name is displayed before their message?
Thank you for the help!!!
(ALSO, if anyone knows a good link that can explain sockets and how socket/client interaction occurs through threads, that would be great!)
server code:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class GameServerOne {
ArrayList clientOutputStreams;
String clientName;
private class ClientThreadHandler implements Runnable {
BufferedReader clientIncomingReader;
Socket socket1;
public ClientThreadHandler(Socket clientSocket2) {
try {
socket1 = clientSocket2;
InputStreamReader clientInputReader = new InputStreamReader(socket1.getInputStream());
clientIncomingReader = new BufferedReader(clientInputReader);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
String message;
try {
while ((message = clientIncomingReader.readLine()) != null) {
tellEveryone(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while(it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void setUpServer() {
clientOutputStreams = new ArrayList();
try {
ServerSocket gameSocket = new ServerSocket(5151);
//System.out.println("Server Connected");
while (true) {
Socket clientSocket = gameSocket.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
writer.println("User connected");
writer.flush();
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientThreadHandler(clientSocket));
t.start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new GameServerOne().setUpServer();
}
}
Client code:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameClientOne {
Socket gameSocket;
JFrame inputNameFrame;
JFrame clientFrame;
JTextArea chatArea;
JTextField messageInput;
BufferedReader gameChatReader;
PrintWriter gameChatWriter;
JTextField nameField;
boolean sessionNamePicked = false;
String userSessionName;
BufferedReader gameBufferedReader;
public GameClientOne () {
setUpGUI();
}
public void setUpGUI() {
clientFrame = new JFrame("Game Window");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel southPanel = new JPanel(new BorderLayout());
JPanel centerPanel = new JPanel(new BorderLayout());
chatArea = new JTextArea();
chatArea.setWrapStyleWord(true);
chatArea.setLineWrap(true);
chatArea.setEditable(false);
JScrollPane chatScroller = new JScrollPane(chatArea);
messageInput = new JTextField();
messageInput.addKeyListener(new MessageInputFieldListener());
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
JPanel westPanel = new JPanel(/*new GridLayout(20, 1)*/);
JPanel eastPanel = new JPanel();
JButton connectButton = new JButton("Connect");
connectButton.addActionListener(new ConnectButtonListener());
connectButton.setPreferredSize(new Dimension(100, 28));
JPanel northPanel = new JPanel(new FlowLayout());
JLabel northTitleLabel = new JLabel("Game Screen");
westPanel.add(connectButton);
chatScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) ;
chatScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
northPanel.add(northTitleLabel);
centerPanel.add(BorderLayout.CENTER, chatScroller);
southPanel.add(BorderLayout.CENTER, messageInput);
southPanel.add(BorderLayout.EAST, sendButton);
mainPanel.add(BorderLayout.SOUTH, southPanel);
mainPanel.add(BorderLayout.CENTER, centerPanel);
mainPanel.add(BorderLayout.WEST, westPanel);
mainPanel.add(BorderLayout.EAST, eastPanel);
mainPanel.add(BorderLayout.NORTH, northPanel);
clientFrame.add(mainPanel);
clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientFrame.setSize(450, 600);
clientFrame.setVisible(true);
messageInput.requestFocusInWindow();
}
public void setUpInputNameFrame() {
inputNameFrame = new JFrame("Insert Name");
JPanel mainPanel = new JPanel(new BorderLayout());
nameField = new JTextField();
nameField.addKeyListener(new NameFieldListener());
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new SubmitButtonListener());
JLabel inputNameLabel = new JLabel("Please pick a name for the chat session");
JPanel northPanel = new JPanel(new FlowLayout());
JPanel southPanel = new JPanel(new BorderLayout());
northPanel.add(inputNameLabel);
southPanel.add(BorderLayout.CENTER, nameField);
southPanel.add(BorderLayout.EAST, submitButton);
mainPanel.add(BorderLayout.NORTH, northPanel);
mainPanel.add(BorderLayout.SOUTH, southPanel);
inputNameFrame.add(mainPanel);
inputNameFrame.setSize(300, 150);
inputNameFrame.setVisible(true);
nameField.requestFocusInWindow();
}
public void connectToGameServer() {
try {
gameSocket = new Socket("127.0.0.1", 5151);
InputStreamReader gameSocketReader = new InputStreamReader(gameSocket.getInputStream());
gameBufferedReader = new BufferedReader(gameSocketReader);
gameChatWriter = new PrintWriter(gameSocket.getOutputStream());
chatArea.append("Connected with name: " + userSessionName + "\n");
Thread incomingTextThread = new Thread(new IncomingTextReader());
incomingTextThread.start();
//System.out.println("Connected to Game");
} catch (Exception e) {
System.out.println("Could not connect to game...");
e.printStackTrace();
}
}
//ALL LISTENER CLASSES BELOW HERE
//main chat page listeners
private class ConnectButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!sessionNamePicked) {
setUpInputNameFrame();
} else {
chatArea.append("You already connected!\n");
}
}
}
private class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!sessionNamePicked) {
chatArea.append("Please connect first\n");
messageInput.setText("");
messageInput.requestFocusInWindow();
} else {
try {
gameChatWriter.println(messageInput.getText());
gameChatWriter.flush();
} catch (Exception ex2) {
ex2.printStackTrace();
}
//chatArea.append(userSessionName + ": " + messageInput.getText() + "\n");
messageInput.setText("");
messageInput.requestFocusInWindow();
}
}
}
private class MessageInputFieldListener implements KeyListener {
public void keyPressed(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!sessionNamePicked) {
chatArea.append("Please connect first\n");
messageInput.setText("");
} else {
try {
gameChatWriter.println(messageInput.getText());
gameChatWriter.flush();
} catch (Exception ex2) {
ex2.printStackTrace();
}
//chatArea.append(userSessionName + ": " + messageInput.getText() + "\n");
messageInput.setText("");
}
}
}
}
//pick chat session name listeners: nameField/submitButton
private class NameFieldListener implements KeyListener {
public void keyPressed(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (!nameField.getText().isEmpty() && e.getKeyCode() == KeyEvent.VK_ENTER) {
userSessionName = nameField.getText();
sessionNamePicked = true;
connectToGameServer();
inputNameFrame.dispose();
messageInput.requestFocusInWindow();
}
}
}
private class SubmitButtonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (!nameField.getText().isEmpty()) {
userSessionName = nameField.getText();
sessionNamePicked = true;
connectToGameServer();
inputNameFrame.dispose();
messageInput.requestFocusInWindow();
}
}
}
//Runnable implementation for threads
private class IncomingTextReader implements Runnable {
public void run() {
String incomingMessage;
try {
while ((incomingMessage = gameBufferedReader.readLine()) != null) {
chatArea.append(incomingMessage + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
GameClientOne gameClient = new GameClientOne();
}
}
If you want to send more than just a message as you are now, you are going to need a protocol to decide what to do with the string you receive, instead of just sending it to all the other clients.
For example, on the client side to send the client's name you might write NAME:(client's name), and then on the server you would split the string at the : and check if the first part is NAME, and if so set the clients name to the rest of the string. You should think about what other types of data you might want to send aswell and design a protocol that works for it.
Here's an example of how to do the above:
On the client you could add the line gameChatWriter.write("NAME:"+name) (create name first of course) imediatly after the connection is made. Then on the server, instead of calling tellEveryone(message) when the message is recieved create a method called processInput and call it, which might look like this:
private void processInput(String message){
String[] commands=message.split(':');
if(commands[0].equals("NAME")){
clientName=commands[1];
}
else{
tellEveryone(message);
}
}
Right now you only have one String clientName and you will obviously need more for the other clients you want to talk to so I would recommend making the thread handler into a seperate class which you would instantiate for each client and could hold things like that.
After you get this to work you could also create a seperate method to write other things that you want to send to either the client or server and a method to process input on the client side as well. Hope this helps.

Categories