gpt4 book ai didi

java - 基本 Java 套接字,服务器不能发送,客户端也不能接收任何东西

转载 作者:行者123 更新时间:2023-11-29 03:56:11 26 4
gpt4 key购买 nike

我根据“java-all-in-one desk reference for Dummies”一书中的教程用 Java 编写了一个基本的客户端-服务器套接字程序,它包含 3 个类,BartServer、BartClient、BartQuote.. 基本上是什么BartServer.class 的作用是监听 BartClient.class,BartClient 在执行时会向 BartServer 发送命令,BartServer 根据 BartClient 发送的内容进行回复。

如果是“get”命令,BartServer 将回复从 BartQuote.class 生成的随机报价else of "bye"将退出客户端...我在本地主机上尝试过,客户端成功连接了服务器,但是 BartServer 没有做任何事情,客户端也不会收到...命令无法响应或任何...我哪里出错了?抱歉我的英语不好...

BartServer:

package socketBart;
import java.net.*;import java.io.*;import java.util.*;

public class BartServer {

public static void main(String[] args)
{
int port = 1892;

BartQuote bart = new BartQuote();

try
{
System.out.println("BartServer 1.0");
System.out.println("Listening on port "+port);
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept(); // WAITS for a client to connect, once connected, returns a socket object
// once clients connects and socket s is created, it will proceed to the next line

String client;
client = s.getInetAddress().toString();
System.out.println("Connected to "+ client);

Scanner in = new Scanner(s.getInputStream()); //READS data sent to this server from CLIENTS
PrintWriter out = new PrintWriter(s.getOutputStream()); //SENDS data FROM this server to CLIENTS

out.println("Welcome to BartServer 1.0");// these lines are sent to the client
out.println("Enter GET to get a quote or BYE to exit.");

while (true)
{
String input = in.nextLine();
if (input.equalsIgnoreCase("bye"))
break;
else if (input.equalsIgnoreCase("get"))
{
out.println(bart.getQuote());
System.out.println("Serving "+ client);
}
else
out.println("Huh?");
}
out.println("So long suckers!");
s.close();

System.out.println("Closed connection to " + client);
}
catch(Exception e){
e.printStackTrace();
}

}

}

巴特客户端:

package socketBart;
import java.net.*;import java.util.*;import java.io.*;

public class BartClient {

public static void main(String[] args)
{
int port = 1892;
System.out.println("Welcome to the Bart Client\n");
Socket s = getSocket(port);

try
{
System.out.println("Connected on port " + port);

Scanner in = new Scanner(s.getInputStream());
PrintWriter out = new PrintWriter(s.getOutputStream(),true);

//discard welcome message from server
in.nextLine();

//discard the exit instructions
in.nextLine();

//get a quote
out.println("get");
String quote = in.nextLine();

//disconnect from server
out.println("bye");
s.close();

//write the quote of the 'chalkboard'
for (int i = 0; i < 20; i++)
System.out.println(quote);
}
catch (Exception e)
{
e.printStackTrace();
}

}

private static Socket getSocket(int port)
{
Socket s;
String host;
InetAddress ip;

Scanner sc = new Scanner(System.in);
while (true)
{
System.out.print("Connect to server: ");
host = sc.nextLine();

try
{
ip = InetAddress.getByName(host);
s = new Socket(ip,port);
return s;
}
catch(UnknownHostException e)
{
System.out.println("The host is unknown.");
}
catch (IOException e)
{
System.out.println("Network error, check your port.");
}//end catch
}
}

}

巴特引用:

package socketBart;
import java.util.Random;
import java.util.ArrayList;

public class BartQuote {

ArrayList<String> q = new ArrayList<String>();

public BartQuote()
{
q.add("I will not waste chalk");
q.add("I will not skateboard in the halls");
q.add("I will not burp in the class");
q.add("I will not instigate a revolution");
q.add("It's potato, not potatoe.");
q.add("I will not encourage others to fly.");
q.add("Tar is not a plaything.");
q.add("I will not sell school property.");
q.add("I will not get very far with this attitude");
q.add("I will not sell land in Florida.");
q.add("I will not grease the monkey bars.");
q.add("I am not a dentist");
q.add("I will finish what I started.");
q.add("Hamsters cannot fly");
}

public String getQuote()
{
int i = new Random().nextInt(q.size());
return q.get(i);
}



}

最佳答案

更改服务器上的这一行:

PrintWriter out = new PrintWriter(s.getOutputStream());

到:

PrintWriter out = new PrintWriter(s.getOutputStream(), true);

这将改变,以便服务器始终在写入后刷新流。否则,数据可能会在输出流的服务器缓冲区中徘徊。

请注意,您的客户端代码已设置为自动刷新。

关于java - 基本 Java 套接字,服务器不能发送,客户端也不能接收任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6296792/

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