Java套接字自动通信
服务器和客户端之间的通信有问题。我试图找出一种自动通信的方法,因为它们必须交换一些参数。然而,使用我编写的代码,服务器要么在客户机确认消息后继续向客户机发送相同的消息,要么客户机根本没有收到任何消息。插座和所有东西以前都已经安装好了。函数sendString()和receiveString()在两个代码示例中是相同的。有没有合适的方法?我不明白为什么这不起作用 服务器:Java套接字自动通信,java,sockets,datainputstream,Java,Sockets,Datainputstream,服务器和客户端之间的通信有问题。我试图找出一种自动通信的方法,因为它们必须交换一些参数。然而,使用我编写的代码,服务器要么在客户机确认消息后继续向客户机发送相同的消息,要么客户机根本没有收到任何消息。插座和所有东西以前都已经安装好了。函数sendString()和receiveString()在两个代码示例中是相同的。有没有合适的方法?我不明白为什么这不起作用 服务器: String buffer; while(true){ buffer = client.receive
String buffer;
while(true){
buffer = client.receiveString();
if(buffer != null && buffer.equals("ready")){
System.out.println("Client is ready");
client.sendString("ready");
while(true){
buffer = client.receiveString();
if(buffer != null && buffer.equals("k")){
System.out.println("stopped");
break;
}
}
break;
}
}
public String receiveString() throws IOException{ //From the client class
if(dataIn.available() > 0){
int length = dataIn.readInt();
byte[] b = new byte[length];
dataIn.readFully(b, 0, b.length);
return new String(b, Charset.forName("UTF-8"));
}
return null;
}
public void sendString(String msg) throws IOException{
byte[] b = msg.getBytes(Charset.forName("UTF-8"));
dataOut.writeInt(b.length);
dataOut.write(b);
}
客户:
String buffer;
while(true){
sendString("ready");
buffer = receiveString();
if(buffer!=null)
System.out.println(buffer);
if(buffer != null && buffer.equals("ready")){
System.out.println("Server is ready");
sendString("k");
break;
}
}
此代码可能适用于您的情况: Client.java
public class Client {
public static void main(String[] args) throws Exception {
try (Socket socket = new Socket("localhost", 8080)) {
try (BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
Scanner in = new Scanner(socket.getInputStream())) {
System.out.println("Client: sending ready.");
writeLine("ready", out);
System.out.println("Client: sent ready.");
String line = in.nextLine();
if ("ready".equals(line)) {
System.out.println("Client: server is ready");
writeLine("k", out);
}
}
}
}
private static void writeLine(final String line, final BufferedOutputStream out) throws IOException {
out.write((line +"\n").getBytes());
out.flush();
}
}
Server.java:
public class Server {
public static void main(String[] args) throws Exception {
boolean running = true;
try (ServerSocket socket = new ServerSocket(8080, 0)) {
while (running) {
System.out.println("Waiting for client accept.");
try (final Socket client = socket.accept();
final Scanner in = new Scanner(client.getInputStream());
final BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream())) {
System.out.println("Waiting for client ready.");
String line = readLine(in);
if ("ready".equals(line)) {
writeLine("ready", out);
while (running) {
line = readLine(in);
if (line != null && line.equals("k")) {
System.out.println("Server: received stop signal");
running = false;
} else {
Thread.sleep(100);
System.out.println("Server: waiting for command.");
}
}
}
}
}
}
}
private static String readLine(final Scanner in) {
String line = in.nextLine();
System.out.println("Server: client sent " + line);
return line;
}
private static void writeLine(final String line, final BufferedOutputStream out) throws IOException {
out.write((line + "\n").getBytes());
out.flush();
}
}
那么这里发生了什么?
服务器套接字等待客户端。若客户端连接,它会等待它发送一些东西(以阻塞方式!)。如果其“就绪”,则检查其他命令
注意:一次仅适用于单个服务器客户端连接。不知道这是否适合你的申请。如果客户端发送“k”,服务器就会关闭,就像您的情况一样。A guess:dataIn.available()是一个非阻塞调用。因此,您的客户机基本上发送“就绪”,检查是否有答案(可能不是这样),然后立即再次发送就绪。尝试删除if(dataIn.available()>0)条件,然后等待服务器响应。我还不完全理解为什么会这样做,但确实如此。还有一个问题。实际上,我必须发送一些实际的字节,这些字节不是字符串,也不是通过套接字发送的任何东西,它们不适用于BufferedOutputStream,但感谢您的帮助:DMy很糟糕,我刚刚意识到在发送之前,您正在将所有内容都灵化为字节,所以我将为我的另一个问题找出解决方法