Display connected client in jtextarea - java

I want to be able to detect any connected clients and display them on my JTextArea.
Here is my server code
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class serveur extends Thread {
final static int port = 9632;
private Socket socket;
private JTextArea clien;
private String res;
public static void main(String[] args) {
// window
final int windowX = 640; //pixels
final int windowY = 500; //pixels
final FlowLayout LAYOUT_STYLE = new FlowLayout();
JFrame window = new JFrame("admin");
window.setSize(windowX, windowY);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel liste = new JLabel("Clients connectés ");
JTextArea clien = new JTextArea(20,50);
clien.setEditable(false);
JButton captureButton = new JButton("Capture d'écran");
JButton partageButton = new JButton("Partage d'écran");
JButton envoiButton = new JButton("Envoi de fichier");
JButton lancementButton = new JButton("Lancement d'une application");
JButton redémarrageButton = new JButton("Redémarrage de la machine");
JButton infoButton = new JButton("Plus d'information");
Container c = window.getContentPane();
c.setLayout(LAYOUT_STYLE);
c.add(captureButton);
c.add(partageButton);
c.add(envoiButton);
c.add(redémarrageButton);
c.add(infoButton);
c.add(lancementButton);
c.add(liste);
c.add(clien);
c.add(new JSeparator(SwingConstants.VERTICAL));
window.setVisible(true);
//serveur
try{
ServerSocket socketServeur = new ServerSocket(port);
System.out.println("Lancement du serveur");
while (true) {
Socket socketClient = socketServeur.accept();
serveur t = new serveur(socketClient);
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//socketServeur.setOption("reuseAddress", true);
public serveur(Socket socket) {
this.socket = socket;
}
public void traitements() {
try {
clien.append(socket.getInetAddress().getHostName());
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
traitements();
}
}
And the client code :
import java.net.*;
import java.io.*;
public class client {
final static int port = 9632;
public static void main(String[] args) {
Socket socket;
try {
socket = new Socket(InetAddress.getLocalHost(), port);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I guess the problem is with the correct placement of this line
clien.append(socket.getInetAddress().getHostName());
Any suggestions

in the traitements () just before client.append you need to add the folowing code :
BufferedReader brBufferedReader1 = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Client.append(brBufferedReader1.readLine());

Related

Stopping a thread after a client disconnects

I'm having this problem where the thread that is used to get information from the client will not shutdown when that user has disconnected. The cpu usage goes up go to 100% and the program gets stuck in an infinite loop.
The problem is on the server side but I have searched and searched and I have found no way to solve this issue. I will post the whole server and client class but i think the issue is centered around the Transfer thread and the SendToAll method.
SERVER:
package serverSide;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server implements Runnable{
public final static int Port = 3000;
public static ServerSocket ss;
public static ArrayList<ClientInfo> Client;
public static String Message = "";
public static void main(String[] args) throws IOException {
#SuppressWarnings("unused")
Server server = new Server();
}
public Server() throws IOException{
ss = new ServerSocket(Port);
Client = new ArrayList<ClientInfo>();
new Thread(this).start();
}
public void run(){
try {
while (true){
//Accept new client
Socket s = ss.accept();
//Get client info
DataInputStream dis = new DataInputStream(s.getInputStream());
String Name = dis.readUTF();
//Give client storage
int Result = CheckClients();
if (Result == -1){
Result = Client.size();
Client.add(new ClientInfo(Name,Result,s));
} else {
Client.set(Result, new ClientInfo(Name,Result,s));
}
//Run get input
Transfer transfer = new Transfer(Client.get(Result).ClientID);
transfer.start();
}
} catch (IOException e) {
System.out.println("Connection Error");
}
}
public static void SendToAll() throws IOException{
for (int i=0;i<Client.size();i++){
DataOutputStream dos = new DataOutputStream(Client.get(i).ClientSocket.getOutputStream());
dos.writeUTF(Message);
}
Message = "";
}
public int CheckClients(){
int Result = -1;
for(int i = 0;i < Client.size() + 1;i++){
try{
if (Client.get(i) == null){
Result = i;
break;
}
} catch (IndexOutOfBoundsException e){
break;
}
}
return Result;
}
}
class Transfer extends Thread{
int ClientNumber;
public Transfer(int ClientNumber){
this.ClientNumber = ClientNumber;
}
public void run(){
while(true){
DataInputStream dis;
try {
dis = new DataInputStream(Server.Client.get(ClientNumber).ClientSocket.getInputStream());
String FromClient = dis.readUTF();
Server.Message = FromClient;
System.out.println(Server.Message);
Server.SendToAll();
} catch (IOException e) {
try {
this.join();
} catch (InterruptedException e1) {
}
break;
}
}
}
}
class ClientInfo {
public ClientInfo(String Name,int ClientID,Socket ClientSocket){
this.Name = Name;
this.ClientID = ClientID;
this.ClientSocket = ClientSocket;
}
public String Name;
public int ClientID;
public Socket ClientSocket;
}
CLIENT:
package clientSide;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.Window.Type;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientWindow {
private JFrame frmClient;
private JTextArea txtLog;
private JTextField txtMessage;
public String ServerAddress;
public String Name;
public String Password;
public Socket s;
public final int Port = 3000;
public static void main(String[] args,String sa,String n,String p) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientWindow window = new ClientWindow(sa,n,p);
window.frmClient.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void ConnectToServer(JTextArea txtLog){
try {
s = new Socket(ServerAddress,Port);
System.out.println("Connected");
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(Name);
GetResponse gr = new GetResponse(txtLog,s);
gr.start();
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(this.frmClient,"Unable to open connection to " + ServerAddress + ". Unknown host.","Error" ,JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
} catch (IOException e) {
JOptionPane.showMessageDialog(this.frmClient,"Unable to open connection to " + ServerAddress + ". Unknown host.","Error" ,JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
public ClientWindow(String sa,String n,String p) throws InterruptedException {
ServerAddress = sa;
Name = n;
Password = p;
initialize();
}
private void initialize() {
frmClient = new JFrame();
frmClient.setResizable(false);
frmClient.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\joshu.DESKTOP-ECFG8JL\\workspace\\ChatSocket\\res\\icons\\Custom-Icon-Design-Flatastic-11-Connection.ico"));
frmClient.setType(Type.POPUP);
frmClient.setTitle("Connect");
frmClient.getContentPane().setBackground(new Color(15, 80, 120));
frmClient.setBounds(100, 100, 870, 650);
frmClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmClient.getContentPane().setLayout(null);
txtLog = new JTextArea();
txtLog.setEditable(false);
txtLog.setForeground(Color.WHITE);
txtLog.setFont(new Font("Calibri Light", Font.PLAIN, 24));
txtLog.setColumns(1);
txtLog.setBorder(new LineBorder(new Color(0, 153, 255)));
txtLog.setBackground(new Color(15, 80, 120));
txtLog.setBounds(7, 87, 845, 450);
frmClient.getContentPane().add(txtLog);
txtMessage = new JTextField();
txtMessage.setHorizontalAlignment(SwingConstants.LEFT);
txtMessage.setForeground(Color.WHITE);
txtMessage.setFont(new Font("Calibri Light", Font.PLAIN, 24));
txtMessage.setColumns(1);
txtMessage.setBorder(new LineBorder(new Color(0, 153, 255)));
txtMessage.setBackground(new Color(15, 80, 120));
txtMessage.setBounds(7, 559, 659, 40);
frmClient.getContentPane().add(txtMessage);
JButton btnSend = new JButton("Send");
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DataOutputStream dos;
try {
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(Name + ": " + txtMessage.getText());
txtMessage.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
btnSend.setForeground(Color.WHITE);
btnSend.setFont(new Font("Calibri Light", Font.BOLD, 25));
btnSend.setBackground(new Color(51, 153, 204));
btnSend.setBounds(676, 559, 176, 40);
Border emptyBorder = BorderFactory.createEmptyBorder();
btnSend.setBorder(emptyBorder);
frmClient.getContentPane().add(btnSend);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(new Color(51, 153, 204));
panel.setBounds(0, 0, 864, 75);
frmClient.getContentPane().add(panel);
JLabel lblIP = new JLabel("Connected to: " + ServerAddress);
lblIP.setVerticalAlignment(SwingConstants.TOP);
lblIP.setHorizontalAlignment(SwingConstants.LEFT);
lblIP.setForeground(new Color(15, 80, 120));
lblIP.setFont(new Font("Calibri Light", Font.BOLD, 40));
lblIP.setBounds(12, 12, 853, 50);
panel.add(lblIP);
ConnectToServer(txtLog);
}
}
class GetResponse extends Thread{
JTextArea txtLog;
Socket s;
public GetResponse(JTextArea txtLog,Socket s){
this.txtLog = txtLog;
this.s = s;
}
public void run(){
while(true){
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String Input = dis.readUTF();
txtLog.setText(txtLog.getText() + "\n" + Input);
} catch (IOException e) {
}
}
}
}

Cannot send objects through java ObjectOutputStream

I am creating a multiclient chat server program. Messages are sent as Message objects. The ServerClient class is not recieving the messages, though, and I don't know why.
Client:
package client;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.*;
import constraints.Constraints;
import message.Message;
import tools.Tools;
import window.ApplicationWindow;
#SuppressWarnings("serial")
public class Client extends ApplicationWindow {
private static final int portNumber = 4444;
private boolean send = false;
private String userName;
private String serverHost;
private int serverPort;
private GridBagLayout layout;
private JTextArea textArea;
private Constraints c_textArea;
private JTextField textField;
private Constraints c_textField;
public static void main(String[] args){
String readName = System.getProperty("user.name");
Client client = new Client(readName, portNumber);
client.startClient();
}
private Client(String userName, int portNumber){
super("ChatApp");
Tools.setLookAndFeel();
this.userName = userName;
this.serverPort = portNumber;
try {
this.serverHost = InetAddress.getLocalHost().getHostAddress();
}catch(Exception e) {
}
layout = new GridBagLayout();
setLayout(layout);
textArea = new JTextArea();
textArea.setColumns(75);
textArea.setRows(20);
c_textArea = new Constraints(0,1);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll, c_textArea);
textField = new JTextField();
textField.setColumns(50);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
send = true;
}
});
c_textField = new Constraints(0,2);
add(textField, c_textField);
pack();
setVisible(true);
setResizable(false);
}
public void output(String message) {
this.textArea.setText(this.textArea.getText() + "\n" + message);
}
private void startClient() {
try{
Socket socket = new Socket(serverHost, serverPort);
Thread.sleep(1000);
ServerThread serverThread = new ServerThread(socket, userName, this);
Thread serverAccessThread = new Thread(serverThread);
serverAccessThread.start();
while(serverAccessThread.isAlive() && this.isRunning()) {
if (send) {
Message message = new Message(textField.getText(), "[" + "]", this.userName);
serverThread.addNextMessage(message);
send = false;
textField.setText("");
}
Thread.sleep(200);
}
}catch(IOException ex){
output("Could not connect to server!");
}catch(InterruptedException ex){
output("Connection interrupted!");
}
}
public void setHost(String serverHost) {
this.serverHost = serverHost;
}
}
ServerClient:
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
import message.Message;
public class ServerClient implements Runnable {
private Socket socket;
private PrintWriter clientOut;
private ChatServer server;
private boolean running;
public ServerClient(ChatServer server, Socket socket) {
this.server = server;
this.socket = socket;
this.running = true;
}
private PrintWriter getWriter(){
return clientOut;
}
public boolean isRunning() {
return this.running;
}
public void stop() {
this.running = false;
}
#Override
public void run() {
try{
this.clientOut = new PrintWriter(socket.getOutputStream(), false);
clientOut.flush();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
while(!socket.isClosed()){
try {
if (in.available() > 0) {
Object i = in.readObject();
if (!i.equals(null)) {
Message input = (Message) i;
String message = input.getText();
String time = input.getTimestamp();
String user = input.getUser();
for(ServerClient thatClient : server.getClients()){
PrintWriter thatClientOut = thatClient.getWriter();
if(thatClientOut != null){
thatClientOut.write(time + " " + user + ": " + message + "\r\n");
thatClientOut.flush();
}
}
}
}
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (SocketException ex) {
System.out.println(ex);
break;
}
}
in.close();
this.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
//public void fileWrite()
}
ChatServer:
package server;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
//import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import constraints.Constraints;
import tools.Tools;
import window.ApplicationWindow;
import message.Message;
#SuppressWarnings("serial")
public class ChatServer extends ApplicationWindow {
private static final int portNumber = 4444;
private ArrayList<Command> commands;
private int serverPort;
private String serverHost;
private List<ServerClient> clients;
ServerSocket serverSocket;
private GridBagLayout layout;
private JTextArea textArea;
private Constraints c_textArea;
private JTextField textField;
private Constraints c_textField;
public static void main(String[] args){
ChatServer server = new ChatServer(portNumber);
server.startServer();
}
public ChatServer(int portNumber){
super("Server " + portNumber);
this.serverPort = portNumber;
try {
this.serverHost = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
this.serverHost = "";
}
Tools.setLookAndFeel();
initActions();
layout = new GridBagLayout();
setLayout(layout);
textArea = new JTextArea();
textArea.setColumns(75);
textArea.setRows(20);
c_textArea = new Constraints(0,1);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll, c_textArea);
textField = new JTextField();
textField.setColumns(50);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
process(textField.getText());
}
});
c_textField = new Constraints(0,2);
add(textField, c_textField);
pack();
setVisible(true);
setResizable(false);
}
public List<ServerClient> getClients(){
return clients;
}
private void startServer(){
clients = new ArrayList<ServerClient>();
serverSocket = null;
try {
InetAddress addr = InetAddress.getByName(this.serverHost);
serverSocket = new ServerSocket(serverPort, 50, addr);
acceptClients(serverSocket);
} catch (IOException e){
output("[ERROR] COULD NOT LISTEN ON PORT: " + serverPort);
this.stop();
}
}
private void acceptClients(ServerSocket serverSocket){
output("[BEGIN] SERVER STARTING ON PORT: " + serverSocket.getLocalSocketAddress());
while (this.isRunning()) {
try{
Socket socket = serverSocket.accept();
output("[ACCEPT] ACCEPTED CLIENT AT: " + socket.getRemoteSocketAddress());
ServerClient client = new ServerClient(this, socket);
Thread thread = new Thread(client);
thread.start();
clients.add(client);
} catch (IOException ex) {
output("[ERROR] ACCEPT FAILED ON: " + serverPort);
}
}
}
private void initActions() {
this.commands = new ArrayList<Command>();
this.commands.add(new Command() {
public void run() {
output("[COMMAND] IP: " + serverSocket.getInetAddress().toString());
}
public String getTrigger() {
return "-ip";
}
});
this.commands.add(new Command() {
public void run() {
for (ServerClient client : clients) {
if (!client.isRunning()) {
clients.remove(client);
}
}
output("[COMMAND] CLIENTS: " + clients.size());
}
public String getTrigger() {
return "-clients";
}
});
}
public boolean process(String command) {
for (Command c : this.commands) {
if (c.getTrigger().equals(command)) {
c.run();
textField.setText("");
return true;
}
}
return false;
}
public void output(String message) {
this.textArea.setText(this.textArea.getText() + "\n" + message);
}
}
ServerThread:
package client;
import java.io.*;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Scanner;
import message.Message;
#SuppressWarnings("unused")
public class ServerThread implements Runnable {
private Socket socket;
private String userName;
private boolean isAlived;
private final LinkedList<Message> messagesToSend;
private boolean hasMessages = false;
private Client client;
public ServerThread(Socket socket, String userName, Client client){
this.socket = socket;
this.userName = userName;
this.client = client;
messagesToSend = new LinkedList<Message>();
}
public void addNextMessage(Message message) {
synchronized (messagesToSend) {
hasMessages = true;
messagesToSend.push(message);
}
}
#Override
public void run(){
print("Welcome :" + userName);
print("Local Port :" + socket.getLocalPort());
print("Server = " + socket.getRemoteSocketAddress() + ":" + socket.getPort());
try {
ObjectOutputStream serverOut = new ObjectOutputStream(socket.getOutputStream());
serverOut.flush();
ObjectInputStream serverIn = new ObjectInputStream(socket.getInputStream());
while(!socket.isClosed()){
if (serverIn.available() > 0) {
try {
if (serverIn.readObject() != null) {
Message input = (Message) serverIn.readObject();
}
} catch (ClassNotFoundException e) {
System.out.println(e);
}
}
if(hasMessages){
Message nextSend;
synchronized(messagesToSend){
nextSend = messagesToSend.pop();
hasMessages = !messagesToSend.isEmpty();
}
serverOut.writeObject(nextSend);
serverOut.flush();
}
}
serverOut.close();
serverIn.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
public void print(String message) {
this.client.output(message);
}
}
I know that the client is able to connect to the server, and both can get messages and successfully call the writeObject() method. However, the message never gets recieved. Can someone help me?
while(!socket.isClosed()){
if (serverIn.available() > 0) {
try {
if (serverIn.readObject() != null) {
Message input = (Message) serverIn.readObject();
}
} catch (ClassNotFoundException e) {
System.out.println(e);
}
}
if(hasMessages){
Message nextSend;
synchronized(messagesToSend){
nextSend = messagesToSend.pop();
hasMessages = !messagesToSend.isEmpty();
}
serverOut.writeObject(nextSend);
serverOut.flush();
}
}
This is all nonsense.
isClosed() returns true when you have closed the socket. Not when the peer disconnects.
Calling available() here and merely spin-looping while it is false is just literally a waste of time.
ObjectInputStream.readObect() only returns null if you send a null.
You are calling it twice, throwing the first object read away, and then trying to read the next object. So you are throwing away every odd-numbered object.
It should be more like this:
for (;;) {
try {
Message input = (Message) serverIn.readObject();
// process input here
} catch (EOFException e) {
break;
} catch (ClassNotFoundException e) {
System.out.println(e);
}
}
if(hasMessages){
Message nextSend;
synchronized(messagesToSend){
nextSend = messagesToSend.pop();
hasMessages = !messagesToSend.isEmpty();
}
serverOut.writeObject(nextSend);
serverOut.flush();
}
}
You should probably split this into a reading thread and a writing thread, as writeObject() and flush() can also block. Same remarks apply to the other place where you are using isClosed(), available(), testing for null, etc.
The hasMessages flag is also not going to work. It will remain false even though a message got added subsequently. It would be better to delete it and just rely on messagesToSend.isEmpty(), inside a synchronized block:
synchronized(messagesToSend){
while (!messagesToSend.isEmpty()){
Message nextSend = messagesToSend.pop();
serverOut.writeObject(nextSend);
}
}
serverOut.flush();

Understanding command line arguments

Basically I am just writing a socket.
For some reason I keep getting this error though
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Main.ChatClient.main(ChatClient.java:143)
which is line " String server = args[0]; "
What does the args need to be to fix this issue?
package Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Observable;
import java.util.Observer;
// Class to manage Client chat Box.
public class ChatClient {
Main a = new Main();
/** 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;
public ChatFrame(ChatAccess chatAccess) {
this.chatAccess = chatAccess;
chatAccess.addObserver(this);
buildGUI();
}
/** Builds the user interface */
private void buildGUI() {
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
add(new JScrollPane(textArea), BorderLayout.CENTER);
Box box = Box.createHorizontalBox();
add(box, BorderLayout.SOUTH);
inputTextField = new JTextField();
sendButton = new JButton("Send");
box.add(inputTextField);
box.add(sendButton);
// Action for the inputTextField and the goButton
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");
}
});
}
}
public static void main(String[] args) {
System.out.println("troll");
String server = args[0];
System.out.println("reached here");
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);
}
}
}
Given:
String server = args[0];
I'd suggest you provide the server name as your first argument to your program
With
public static void main(String[] args)
args is the array of command line args passed in.

Text not showing up in Jframes in my GUI

So I've been working on this program off and on for a few days, its a basic client server chat room. I'm trying to have it so that when you launch the client, the gui that pops up has text inside of the portnumber, servername, and textbox JTextFields. Yesterday this was the case but I've changed things and now the gui appears without text in the text fields. That code is in the displaysettings method which runs at the beginning of the try catch block. Anyone know why it isn't working?
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class clienttry2 extends JFrame implements ActionListener {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JButton send = new JButton("Send");
JButton connect = new JButton("Connect");
JLabel server = new JLabel("Server");
JLabel port = new JLabel("Port");
JButton disconnect = new JButton("Disconnect");
static JTextField servername = new JTextField(10);
static JTextField portnumber = new JTextField(10);
static JTextField textbox = new JTextField(40);
JTextArea chatbox = new JTextArea(20,45);
static Boolean isconnected = false;
static Boolean sending = false;
static Socket server1;
static ObjectInputStream in;
static ObjectOutputStream out;
public clienttry2(){
setTitle("Chat Room");
setLayout(new java.awt.FlowLayout());
setSize(600,500);
panel1.add(chatbox); //has all the chats
panel2.add(textbox); //area to type new messages into
panel2.add(send); send.addActionListener(this);//send button
panel3.add(server);
panel3.add(servername);
panel3.add(port);
panel3.add(portnumber);
panel3.add(connect); connect.addActionListener(this);
panel3.add(disconnect); disconnect.addActionListener(this); disconnect.setEnabled(false);
add(panel1);
add(panel2);
add(panel3);
}
public static void main(String[] args)throws Exception {
Client display = new Client();
display.setVisible(true);
try{
displaysettings();
connect();
setup();
String message;
message = (String) in.readObject();
System.out.println(message);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private static void displaysettings() {
portnumber.setText("3333");
servername.setText("localhost");
textbox.setText("This is where you type your messages to send to others on the server!");
}
private static void connect() throws UnknownHostException, IOException {
server1 = new Socket("localhost", 3333);
}
private static void setup() throws IOException {
out = new ObjectOutputStream(server1.getOutputStream());
out.flush();
in = new ObjectInputStream(server1.getInputStream());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==connect)
{
System.out.println("connected");
isconnected = true;
connect.setEnabled(false);
disconnect.setEnabled(true);
}
if(e.getSource()==send)
{
System.out.println("sending chat");
sending = true;
}
if(e.getSource()==disconnect)
{
try {
server1.close();
out.close();
isconnected = false;
connect.setEnabled(true);
disconnect.setEnabled(false);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Swing components will not update unless told to. After you set the text, call:
textbox.repaint();
to force the GUI to update.

add jbutton at runtime

I am building an application on Socket programming in swing in which server listens for clients and if a client is connected I want
A button should be added for each client if connected on server screen
Add a listener on each button.For example add Send Message function for each client
I have created a thread in server which listens for client connections but I can not add jbutton at runtime.
Please reply.
Is that what you need ? :
import javax.swing.*;
import java.awt.event.*;
public class NewButtonOnRunTime {
static JPanel panel;
static JFrame frame;
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame = new JFrame("Add Buttons");
JButton button = new JButton("Simulate new Client");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JButton jb = new JButton("A new Client");
jb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "This is an button action");
}
});
panel.add(jb);
frame.revalidate();
}
});
panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Assuming you already have some kind of isConnected bool,
if(isConnected){
JButton runTimeButton = new JButton();
runTimeButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//do all the things you need to do in here,
//alternatively, define this inner class somewhere else and pass in
// an instance of it
}
});
frame.add(runTimeButton);
}
Based on ServerSocket communication in java (i.e. TCP), if a client is connected to the server with a particular port say 5555, no other clients will not be able to connect to the server with the same port at the same time because the port is already in use. So, each client should be using different port if you want to get simultaneous communication with the clients.
Look at this sample code, it adds JButton at runtime when a new client is connected :
(run client code and server code in separate machine connected together)
Server code
package com.test.socket;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ServerFrame extends JFrame {
private Map<Integer, SocketVO> serverSocketMap;
private int startingPort = 5555;
public ServerFrame() {
}
public ServerFrame(String title) {
super(title);
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setLayout(new FlowLayout());
initComponents();
serverSocketMap = new HashMap<Integer, SocketVO>();
keepStartServerSocket();
setVisible(true);
}
private JTextArea area = null;
private void initComponents() {
area = new JTextArea(20, 20);
add(area);
// addDynamicButton();
}
private void addDynamicButton(final int port) {
JButton button = new JButton("Client on " + port + " port");
add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
Socket socket = serverSocketMap.get(port).getSocket();
try {
String content = area.getText();
socket.getOutputStream().write(content.getBytes());
System.out.println("seding text area content to " + port);
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
class SocketVO {
private ServerSocket serverSocket;
private Socket socket;
public ServerSocket getServerSocket() {
return serverSocket;
}
public void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
private void keepStartServerSocket() {
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
System.out.println("waiting for a client at port "
+ startingPort);
openServerSocket(startingPort);
startingPort++;
}
}
}).start();
}
private void openServerSocket(final int port) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
final Socket socket = serverSocket.accept();
SocketVO socketVO = new SocketVO();
socketVO.setServerSocket(serverSocket);
socketVO.setSocket(socket);
serverSocketMap.put(port, socketVO);
addDynamicButton(port);
checkForCosing(port, socket);
} catch (IOException e) {
e.printStackTrace();
}
}
private void checkForCosing(final int port, final Socket socket) {
new Thread(new Runnable() {
#Override
public void run() {
InputStream inputStream = null;
try {
inputStream = socket.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] b = new byte[1024];
int read = 0;
try {
StringBuilder builder = new StringBuilder();
while ((read = inputStream.read(b)) != -1) {
builder.append(new String(b));
}
if (builder.toString().equals("close")) {
closeServerSocket(port);
System.out.println("server port " + port + " closed");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
/**
* the method to close corresponding sockets
*
* #param port
*/
private void closeServerSocket(int port) {
Socket socket = serverSocketMap.get(port).getSocket();
ServerSocket serverSocket = serverSocketMap.get(port).getServerSocket();
try {
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerFrame("Server Socket Frame");
}
}
Client code :
package com.test.socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private int startingPort = 5550;
private void bindToServer() {
while(true) {
try {
Socket socket = new Socket("127.0.0.1", startingPort);
InputStream inputStream = null;
try {
inputStream = socket.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
byte[] b = new byte[1024];
int read = 0;
try {
StringBuilder builder = new StringBuilder();
while((read = inputStream.read(b)) != -1) {
builder.append(new String(b));
}
System.out.println("message from server : "+builder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
startingPort++;
}
if (startingPort == 5580) {
}
}
}
public static void main(String[] args) {
new Client().bindToServer();
}
}
As per your last comment:
I have tested the program with two clients. The server is connected to two clients successfully. But buttons are not added at runtime for each client.
You are able to connect with multiple client and you have added a button also but it's not visible on the server.
Calling validate() again on JFrame will solve your problem after adding new JButton.
public class Test extends JFrame {
private JButton field[];
public JButton[] getField() {
return field;
}
public void test(int n) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
field = new JButton[n];
for (int i = 0; i < field.length; i++) {
field[i] = new JButton("" + i + "");
field[i].addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("clicked button");
}
});
panel.add(field[i]);
}
add(panel);
}
public static void main(String[] args) {
Test test = new Test();
test.setSize(300, 300);
test.setVisible(true);
test.setLocationRelativeTo(null);
test.test(5);
}
}

Categories