java - 客户端-服务器应用程序 JAVA,服务器不接收数据

标签 java swing client-server

客户端向服务器发送数据时出现问题。当我从服务器向客户端发送数据时,一切正常。我收到此消息:“客户端接收:消息”但是当我发送“客户端消息”时,我的服务器没有收到它。

import java.io.IOException;

import java.net.*;
import java.io.*;

public class Server {    
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                clientSocket.getInputStream()));
        String inputLine, outputLine;

        outputLine = "message";
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
            System.out.println("server receive: " + inputLine);
            outputLine = "second message";
            out.println(outputLine);
        }

        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}


public void actionPerformed(ActionEvent e) {

    if (e.getSource() == startButton) {
        this.main.getContentPane().remove(homePanel);
        String name = this.name.getText();

        String result;            
        try {
            connectionToServer();
            if ((result = in.readLine()) != null) {
                System.out.println("client receive: " + result);
                out.println("client's message");
            }
        } catch(IOException err) {
            System.out.println("error");
        }

    }        
}

public void connectionToServer() throws IOException {
    try {
        this.socket = new Socket("localhost", 4444);
        this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        this.out = new PrintWriter(this.socket.getOutputStream(), true);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: taranis.");
        System.exit(1);
    }        
}

最佳答案

您的 actionPerformed 方法只是链接服务器,什么都不做。

发送消息后服务器关闭。

观察:while ((inputLine = in.readLine()) != null)

如果客户端不发送任何消息,代码将中断,然后您关闭服务器。

关于java - 客户端-服务器应用程序 JAVA,服务器不接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476755/

相关文章:

java - 如何使 itemStateChange 方法在按下 JButton 时运行?

java - 替换密码字段中的点字符,Jframe,Java

java - 在客户端-服务器传输中引入错误

java - 如何使 "Enter"键的行为类似于在 JFrame 上提交

tomcat - 在客户端连接终止时停止服务器线程

java - 阴影不适用于 TextView 。如何修复它?

java - 构造运行时要调用的方法名称

java - 尝试在泛型类上实现可比较的接口(interface)时出现绑定(bind)不匹配错误

java - 如何使用注册表操作

java - 连接具有 StringConverter 的 slider 和微调器