gpt4 book ai didi

Java 服务器总是返回相同的消息

转载 作者:行者123 更新时间:2023-11-30 10:53:58 24 4
gpt4 key购买 nike

我是第一次做Socket编程,做了一个client程序和一个Server程序,server只是简单的给Client发送一个消息。问题是,当我更改消息时,它会再次显示旧消息。

这是客户端

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("连接客户端出错"); }

}

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

最佳答案

我测试了您的代码,它非常适合我。

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

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

这是我的做法:

  1. 编译服务器(你的代码原样)
  2. 编译客户端(你的代码原样)
  3. 在命令提示符下运行服务器(打开控制台并运行 java 服务器)
  4. 在不同的命令提示符下运行客户端(打开控制台并运行 java 客户端)
  5. 在客户端控制台中按要求插入 ip 地址 (127.0.0.1)
  6. 客户端控制台显示“这确实有效”
  7. 停止客户端(在客户端控制台按 Ctrl-C)
  8. 停止服务器(在服务器控制台按 Ctrl-C)
  9. 编辑 Server.java:修改服务器的消息(“那些实际工作的”->“不同的消息”)并保存
  10. 编译服务器
  11. 重复步骤 3 到 5
  12. 客户端控制台显示“不同的消息”

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

关于Java 服务器总是返回相同的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33824273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com