V
V
Vadim Gush2014-10-23 17:41:28
Java
Vadim Gush, 2014-10-23 17:41:28

Why is there no connection to the server in Java?

Wrote a program like Server-Client. I start the server and the first client. Everything works fine, but as soon as a second client is connected, the first one stops receiving messages. Here is the code:
Client.java

public class Client extends JFrame implements Runnable{
  
  private static boolean bconnect = true;
  static private Random random = new Random();
  static private ObjectOutputStream output;
  static private ObjectInputStream input;
  static private String inObject = null;
  static private int height = 600;
  static private Socket connect;
  
  public static void main(String[] args) {
    
    new Thread(new Client("Client")).start();
  }
  
  public Client(String name) {
       //Здесь находится интерфейс программы
  }

  public void run() {
    createConnection();
  }
  
  public static void createConnection() {
    try {
      while (true) {
        connect = new Socket(InetAddress.getByName(Settings.getIP()), Settings.getPort());
        output = new ObjectOutputStream(connect.getOutputStream());
        input = new ObjectInputStream(connect.getInputStream());
        
        if (bconnect) {
          sendData("<font color=green>Новый клиент " + Settings.getName() + ":<br>подключился к серверу</font>");
          bconnect = false;
        }
        
        inObject = (String)input.readObject();
        
          if (inObject != null && inObject.equals("&&&$client_recd;") == false) {
            Messages.newMessage(inObject);
            last_message.setText(Messages.list());
          }
      }
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (HeadlessException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
  
  public static void sendData(Object obj) {
    try {
      output.writeObject(Settings.getName() + ": " + obj);
      output.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}

Server.java
public class Server extends JFrame implements Runnable{
  
  static private int height = 950;
  static private String inObject = null;
  static private ServerSocket server;
  private static Socket connect;

  public static void main(String[] args) {
    
    new Thread(new Server("Server")).start();
    
  }
  
  public Server(String name) {
    //Здесь находится интерфейс программы
  }
  
  public void run() {
    
    try {
      server = new ServerSocket(SettingsServer.getPort(), SettingsServer.getMaxUsers());
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    
               //Создаю бесконечный цикл для подключения новых клиентов

    while (true) {
      try {
        setConnect(server.accept());
      } catch (IOException e) {
        e.printStackTrace();
      }
    
      new Thread(new IncomingHandler(getConnect())).start();
    }
  }
  
  public static Socket getConnect() {
    return connect;
  }

  public static void setConnect(Socket connect) {
    Server.connect = connect;
  }

IncomingHandler.java
public class IncomingHandler implements Runnable{
  
  static private int counter = 0;
  static private String inObject;
  static private Socket incomingConnect;
  static private ObjectOutputStream output;
  static private ObjectInputStream input;
  private static Socket incoming;
  
  public IncomingHandler(Socket incoming) {
    this.incoming = incoming;
  }

  public void run() {
    
    incomingConnect = Server.getConnect();
    
    try {
      output = new ObjectOutputStream(incomingConnect.getOutputStream());
      input = new ObjectInputStream(incomingConnect.getInputStream());
      
      inObject = (String)input.readObject();
      
      if (inObject != null) {
        ++counter;
        
        ServerMessages.newMessage(counter + ": " + inObject);
        Server.last_message.setText(ServerMessages.list());
        
        sendData(inObject);
      }
      
      inObject = null;
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
  
  public static void sendData(Object obj) {
    try {
      ServerMessages.newMessage("Message №" + counter + " resend.");
      Server.last_message.setText(ServerMessages.list());
      output.writeObject(obj);
      output.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Power, 2014-10-24
@Power

You have all class fields declared with the static modifier, but they should be without it.
And in the code you have strange duplications and extra code, for example, instead of

while (true) {
      try {
        setConnect(server.accept());
      } catch (IOException e) {
        e.printStackTrace();
      }
    
      new Thread(new IncomingHandler(getConnect())).start();
    }

should be something like
while (true) {
      try {
        Socket accepted = server.accept();
        new Thread(new IncomingHandler(accepted)).start();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

and then you don't need methods
public static Socket getConnect() {
    return connect;
  }

  public static void setConnect(Socket connect) {
    Server.connect = connect;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question