gpt4 book ai didi

java - 如何使用 I/O 将 double 从客户端发送到服务器并返回客户端

转载 作者:行者123 更新时间:2023-12-02 03:40:24 24 4
gpt4 key购买 nike

我无法使用扫描仪或打印机

我有一个客户端和一个服务器。

我运行服务器,输出是,

Creating Server Socket 5095 . . . 
SUCCESS!!!
in while loop
Waiting for connection.

然后我运行客户端,输出是,

Creating Client Socket 
SUCCESS!!!
initializing variables
variables flushed
initializing input
java.io.EOFException
at java.io.DataInputStream.readFully(Unknown Source)
at java.io.DataInputStream.readLong(Unknown Source)
at java.io.DataInputStream.readDouble(Unknown Source)
at Client.runClient(Client.java:38)
at Client.main(Client.java:51)

我想在客户端初始化一些 double ,并将它们发送到服务器,在服务器上对它们进行解析和操作,然后将结果发送回客户端。

客户端因 EOFException 失败,服务器因 ClassCastException 失败

我做错了什么?

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

public class Client {

static void pStr(String p) {
System.out.println(p);
}

static void runClient() {

Socket client;
DataOutputStream output;
DataInputStream input;

try {
pStr("Creating Client Socket ");
client = new Socket("127.0.0.1", 5095);
pStr("SUCCESS!!!");

input = new DataInputStream(client.getInputStream());
output = new DataOutputStream(client.getOutputStream());

System.out.println("initializing variables");
double arg1 = 5;
output.writeDouble(arg1);
output.flush();

double arg2 = 3;
output.writeDouble(arg2);
output.flush();

System.out.println("variables flushed");

System.out.println("initializing input");
// error occurs at next line
double ans = input.readDouble();
System.out.println("input has been read");
System.out.println(ans);

input.close();
output.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// this is second class file
public static void main(String args[]) {
Client.runClient();
}
}
public class SimpleServer {

static void pStr(String p) {
System.out.println(p);
}

static void runServer() {
ServerSocket server;

try {
// Create a ServerSocket.
pStr("Creating Server Socket " + 5095 + " . . . ");
server = new ServerSocket(5095);
pStr("SUCCESS!!!");

while (true) {
System.out.println("in while loop");
Socket connection;
DataOutputStream output = null;
DataInputStream input = null;

pStr("Waiting for connection.");
connection = server.accept();
pStr("Done");

System.out.println("entering try");

try {
// Get input and output streams.
input = (DataInputStream) connection.getInputStream();
output = (DataOutputStream) connection.getOutputStream();

String ansStr;

double arg1, arg2;
try {
System.out.println("int try block");
arg1 = input.readDouble();
arg2 = input.readDouble();

ansStr = "" + arg1 + "," + arg2;
System.out.println(ansStr);

output.writeDouble(arg1);
output.writeDouble(arg2);

} catch (Exception e) {
ansStr = e.getMessage();
}
output.flush();
} catch (Exception e) {
pStr("Error in Protocol: " + e.getMessage());
} finally {
try {
input.close();
} catch (Exception e) {
}
try {
output.close();
} catch (Exception e) {
}
try {
connection.close();
} catch (Exception e) {
}
}
}
} catch (Exception e) {
pStr("Error making ServerSocket");
}
}

public static void main(String args[]) {
runServer();
}
}

最佳答案

您的服务器代码因 ClassCastException 崩溃,而您却莫名其妙地没有提及。因此,连接被关闭而没有发送任何数据,因此出现客户端异常。

仅通过强制转换无法从套接字获取 DataInputStreamDataOutputStream。您必须使用在客户端中使用的相同流程。

关于java - 如何使用 I/O 将 double 从客户端发送到服务器并返回客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36904310/

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