Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaserver;
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- import javax.swing.*;
- public class JavaServer extends JFrame {
- private JTextField userText;
- private JTextArea chatWindow;
- private ServerSocket server;
- private int clientNo = 0;
- private ArrayList<Connection> AllConnections = new ArrayList<Connection>();
- //constructor
- public JavaServer(){
- super("Messenger");
- userText = new JTextField();
- userText.addActionListener(
- new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- showMessage("\nSERVER - " + e.getActionCommand());
- for (int i = 0; i < AllConnections.size(); i++) {
- try {
- AllConnections.get(i).output.writeObject("SERVER - " + e.getActionCommand());
- AllConnections.get(i).output.flush();
- } catch (IOException ex) {
- showMessage("\nError occured in sending message");
- }
- }
- userText.setText("");
- }
- }
- );
- add(userText, BorderLayout.NORTH);
- chatWindow = new JTextArea();
- add(new JScrollPane(chatWindow));
- setSize(300,150);
- setVisible(true);
- }
- public void startRunning(){
- try{
- server = new ServerSocket(6789, 100);
- while(true){
- showMessage("\nWaiting for a connection");
- Socket newConnection = server.accept();
- try{
- AllConnections.add(new Connection(newConnection));
- clientNo++;
- }catch(IOException iOException){
- showMessage("\n Error: A client failed to connect");
- }
- }
- }catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- private class Connection extends Thread{
- private int ClientID = clientNo;
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private Socket clientSocket;
- //Creates a thread for the client
- Connection(Socket clientThread) throws IOException{
- clientSocket = clientThread;
- output = new ObjectOutputStream(clientThread.getOutputStream());
- output.flush();
- input = new ObjectInputStream(clientThread.getInputStream());
- showMessage("\n Stream are now setup!");
- start();
- }
- //Starts conversation
- @Override
- public void run(){
- String message = "";
- showMessage("\nCLIENT " + ClientID + " - has joined the server");
- do{
- try{
- message = (String) input.readObject();
- //Display message to server
- showMessage("\nCLIENT " + ClientID + " - " + message);
- //Send message to all clients
- sendMessage(ClientID, message);
- }catch(ClassNotFoundException classNotFoundException){
- showMessage("\n idk what that user send!");
- }catch(EOFException ex){
- //Client properly closed connection
- break;
- }catch(SocketException ex){
- //Client prematurely closed connection
- break;
- }catch (IOException ex) {
- ex.printStackTrace();
- }
- }while(!message.equals("END"));
- showMessage("\nCLIENT " + ClientID + " - has left the connection");
- //When chat is over. close all connections.
- try {
- output.close();
- input.close();
- clientSocket.close();
- AllConnections.remove(this);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- //updates chatWindow
- private void showMessage(final String text){
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run(){
- chatWindow.append(text);
- }
- }
- );
- }
- //Send message to all clients
- private void sendMessage(int ClientID, final String text){
- for (int i = 0; i < AllConnections.size(); i++) {
- try {
- AllConnections.get(i).output.writeObject(text);
- AllConnections.get(i).output.flush();
- } catch (IOException ex) {
- showMessage("\nError occured in sending message");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement