首页 文章

Java Server总是返回相同的消息

提问于
浏览
2

这是我第一次进行Socket编程,我制作了一个客户端程序和一个Server程序,服务器只是向客户端发送一条简单的消息 . 问题是,当我更改消息时,它将再次显示旧消息 .

这是客户

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.util.*;
public class Client extends JFrame {

public static final int  PORT = 49998;

    Client(){
        JFrame window = new JFrame();
        JPanel thePanel = new JPanel();
        JTextField txt = new JTextField(20);
        JButton btn = new JButton("Send");

        window.add(thePanel);
        thePanel.add(txt);
        thePanel.add(btn);

        window.setSize(400,400);
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.setTitle("Client");
        window.setVisible(true);
    }


    public static void main(String[] args){     
        new Client();

        Socket conect;
        String ip;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter you're IP adress: ");
        ip = sc.nextLine();

        try{
            conect = new Socket(ip,PORT);
            BufferedReader read = new BufferedReader(
                new InputStreamReader    (conect.getInputStream()));
            String line = read.readLine();
            if(line == null){
                System.out.println("Error reading from server");
            }
            System.out.println();
            System.out.println(line);
            read.close();
        }catch(IOException e){
            System.out.println("Can't connect to server");
        }
    }
}

这是服务器

import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Server {

    static PrintWriter pw;

    Server(){

        JFrame window = new JFrame();
        JPanel thePanel = new JPanel();
        JTextField txt = new JTextField(20);
        JButton btn = new JButton("Send");

        window.add(thePanel);
        thePanel.add(txt);
        thePanel.add(btn);

        window.setSize(400,400);
        window.setTitle("Server");
        window.setVisible(true);

    }

    public static void main(String[] args){

        new Server();

        ServerSocket server;
        Socket connection;
        int port = 49998;

        try{
            server = new ServerSocket(port);
            while(true){
                connection = server.accept();
                sendMsg(connection);
            }           
        }catch(IOException e){
            System.out.println("Problem connection to client");
        }

    }

    public static void sendMsg(Socket con){

        try{
            pw = new PrintWriter(
                con.getOutputStream());
            pw.println(" this actually work");

//这是消息,如果我改变它的内容它不再工作,除非我改变端口pw.flush(); pw.close(); } catch(IOException e){System.out.print(“connceting to client”); }}

}

我还没有对JFrame做任何事情,所以不要注意它 .

2 回答

  • 1

    我测试了你的代码,它适合我 .

    我的意思是......根据您发布的代码,我假设您正在更改源代码中的消息并重新启动服务器,对吧?

    我怀疑你只是没有停止你的第一个服务器进程,以便它保持端口忙,它一直响应 .

    这是我做的:

    • 编译服务器(您的代码不变)

    • 编译客户端(您的代码不变)

    • 在命令提示符下运行server(打开控制台并运行java Server)

    • 在不同的命令提示符下运行客户端(打开控制台并运行java客户端)

    • 在客户端控制台中根据请求插入IP地址(127.0.0.1)

    • 客户端控制台显示"Those this actually work"

    • 停止客户端(客户端控制台上的Ctrl-C)

    • 停止服务器(服务器控制台上的Ctrl-C)

    • 编辑Server.java:修改服务器的消息("Those this actually work" - > "Different message")并保存

    • 编译服务器

    • 重复步骤3到5

    • 客户端控制台显示"Different message"

    我怀疑你错过了第8步......你能仔细检查吗? (在这种情况下,您应该在服务器控制台中看到消息“与客户端连接有问题”)

  • 0

    关闭一切 . 应用程序告诉操作系统它将侦听特定端口......直到它关闭 .

    try (ServerSocket server = new ServerSocket(port)) {
            while (true) {
                try (Socket connection = server.accept()) {
                    sendMsg(connection);
                } catch (IOException e) {
                    System.out.println("Problem connection to client");
                }
            }           
        } catch (IOException e) {
            System.out.println("Problem connection for server");
        }
    

    try-with-resources将负责关闭 .

相关问题