Java套接字。服务器-客户端通信

标签 java sockets server client

我正在尝试在没有 gui 的情况下将带有 gui 的客户端连接到服务器。连接正在完成,但我看不到这两个应用程序之间的任何消息。 (我应该在客户端获取 SERVER HERE,在服务器获取 CLIENT HERE)

客户端连接代码:

@Override
public void ClientRunning(){
    try {
       connectToServer();
        setStreams();
        ClientRun();

    }catch(EOFException oefException){
        showMessage("\n Client terminated the connection\n");
    }catch(IOException ioException){
        ioException.printStackTrace();
    }finally{
        close();
    }
}



public void connectToServer() throws IOException{
    showMessage("Attempting Connection... \n");
    connection = new Socket(InetAddress.getByName(serverIP),6789);
    showMessage("Connected to: "+ connection.getInetAddress().getHostName());
}


public void setStreams() throws IOException{
   output = new PrintWriter(connection.getOutputStream(),true);
   output.flush();
   input= new BufferedReader(new InputStreamReader(connection.getInputStream()));

    showMessage("\n Streams are now set. \n");
}

public void close(){
    showMessage("\n closing...");
    try{
      output.close();
      input.close();
      connection.close();

    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}
public void showMessage(final String text){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            cwindow.append(text);
        }
    });
}

public void sendMessage(String message){
    output.write("CLIENT - "+message);
    output.flush();
    showMessage("\nCLIENT - "+message);
}

private void ClientRun() throws IOException{
    String message="CLIENT HERE!";
    sendMessage(message);
    do{
        try{
            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");
    }

(输入和输出在 GUI 类中定义,该 Client 类扩展到该类。定义为“ protected BufferedReader 输入; protected PrintWriter 输出;")

此外,服务器代码:

public class ServerClass {


private ServerSocket server;
private Socket connection;
private BufferedReader input;
private BufferedWriter output;


public void startServer(){
    try{
        server=new ServerSocket(6789,100);
        while(true){
            try{
                waitForConnection();
                setStreams();
                ServerRunning();
            }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");

            }finally{
            close();

        }
        }

    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}

private void waitForConnection() throws IOException{
    showMessage("Waiting for someone to connect... \n");
    connection=server.accept();
    showMessage("Now connected to "+ connection.getInetAddress().getHostName());
}

private void setStreams() throws IOException{
   output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
   output.flush();
   input= new BufferedReader(new InputStreamReader(connection.getInputStream()));

    showMessage("\n Streams are now set. \n");
}
public void ServerRunning() throws IOException{
    String message="SERVER HERE!";
    sendMessage(message);
    do{
        try{

            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");

  }
private void close(){
    showMessage("\n Closing connections... \n");
    try{
        output.close();
        input.close();
        connection.close();

}catch(IOException ioException){
    ioException.printStackTrace();
}
}
private void showMessage(String text){
    System.out.println(text);
}

private void sendMessage(String message){
    try{
       output.write("SERVER - "+message);
        output.flush();
        showMessage(message);
    }catch(IOException ioException){
        System.out.println("\n ERROR!");
    }
}

连接似乎没问题,所以我没有弄错。任何帮助将不胜感激。

PS:我也在服务器上试过PrintWriter,也在stream语句中试过try catch,问题依旧。

最佳答案

您正在使用需要换行符的 readLine()。要么将 \n 添加到您要发送的消息中,要么不要使用 readLine()

关于Java套接字。服务器-客户端通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098904/

相关文章:

java - 如何看懂java String源码

java - connect(localhost) 抛出异常,connect(私有(private)地址) 阻塞

go - 我如何捕获 netcat 反向 shell?

java - 迭代循环至少 1000 次

java.lang.NoSuchFieldError : EDGE when running JUnit test

java - 在 IBM Websphere 环境中设置负载平衡器服务器

java - 如何检查 Java 上的 SSLSocket 连接是否正常?

java - RxJava : Observing messages emitted from a socket

linux - 跨 Linux 多服务器配置的冗余守护进程

java - Java 中并发的 gremlin-server 和图形查询