我无法使用扫描仪或打印机
我有一个客户端和一个服务器。
我运行服务器,输出是,
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)
我想初始化客户端中的一些双打,并将它们发送到服务器,在那里它们将被解析和操作,然后将结果发送回客户端。
客户端失败并显示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();
}
}
答案 0 :(得分:2)
您的服务器代码崩溃了ClassCastException
,您无法提及。因此,在没有任何数据发送的情况下关闭连接,因此客户端异常。
您只能通过投射从套接字中获取DataInputStream
或DataOutputStream
。您必须使用您在客户端中使用的相同流程。