Добрый день! Почему то когда запускаую ввожу имя сервера всё работает, но когда ввожу имя порта выводит ошибку "Клиент не подключён к серверу!".
Подскажите что можно сделать!
public class ClientGuiController extends Client{
private ClientGuiModel model = new ClientGuiModel();
private ClientGuiView view = new ClientGuiView(this);
protected SocketThread getSocketThread(){
return new GuiSocketThread();
}
public void run(){
SocketThread socketThread = getSocketThread();
socketThread.run();
}
protected String getServerAddress(){
return view.getServerAddress();
}
protected int getServerPort(){
return view.getServerPort();
}
protected String getUserName(){
return view.getUserName();
}
public ClientGuiModel getModel(){
return this.model;
}
public static void main(String[] args) {
ClientGuiController clientGuiController = new ClientGuiController();
clientGuiController.run();
}
public class GuiSocketThread extends SocketThread{
@Override
protected void processIncomingMessage(String message) {
model.setNewMessage(message);
view.refreshMessages();
}
@Override
protected void informAboutAddingNewUser(String userName) {
model.addUser(userName);
view.refreshUsers();
}
@Override
protected void informAboutDeletingNewUser(String userName) {
model.deleteUser(userName);
view.refreshUsers();
}
@Override
protected void notifyConnectionStatusChanged(boolean clientConnected) {
view.notifyConnectionStatusChanged(clientConnected);
super.notifyConnectionStatusChanged(clientConnected);
}
}
}
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<>();
public static void sendBroadcastMessage(Message message){
for (Map.Entry<String, Connection> pair : connectionMap.entrySet()) {
try {
pair.getValue().send(message);
} catch (IOException e) {
ConsoleHelper.writeMessage("Message wasn't sent");
}
}
}
private static class Handler extends Thread{
private Socket socket;
Handler(Socket socket){
this.socket = socket;
}
private String serverHandshake(Connection connection) throws IOException, ClassNotFoundException {
while(true) {
Message sent = new Message(MessageType.NAME_REQUEST, "Name please");
connection.send(sent);
Message received = connection.receive();
if (received.getType() == MessageType.USER_NAME && !received.getData().isEmpty() && !connectionMap.containsKey(received.getData())) {
connectionMap.put(received.getData(), connection);
Message accepted = new Message(MessageType.NAME_ACCEPTED, "accepted");
connection.send(accepted);
return received.getData();
}
}
}
private void sendListOfUsers(Connection connection, String userName)throws IOException{
for (String a : connectionMap.keySet()){
if (!a.equals(userName)){
connection.send(new Message(MessageType.USER_ADDED, a));
}
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException {
while (true) {
Message message = connection.receive();
if (message != null && message.getType() == MessageType.TEXT) {
sendBroadcastMessage(new Message(MessageType.TEXT, userName + ": " + message.getData()));
} else {
ConsoleHelper.writeMessage("Error!");
}
}
}
public void run(){
ConsoleHelper.writeMessage(socket.getRemoteSocketAddress().toString());
String name = null;
try(Connection connection = new Connection(socket)){
name = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED, name));
sendListOfUsers(connection, name);
serverMainLoop(connection, name);
connectionMap.remove(name);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, name));
ConsoleHelper.writeMessage("Connection is cosed!");
} catch (IOException e ) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
writeMessage("Enter the port.");
try (ServerSocket serverSocket = new ServerSocket(ConsoleHelper.readInt())) {
writeMessage("Server started!");
while (true) {
new Handler(serverSocket.accept()).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
вот код