gpt4 book ai didi

Java 套接字 readInt EOFException

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

我在我的手机 (Android) 和我的服务器之间做了一个简单的套接字。

手机可以连接到服务器,但我可以为彼此发送或读取数据。

这是我为我的客户(电话端)使用的代码:

private static void connect(String url, int port)
{
do
{
try
{
socket = new Socket(url, port);
input = socket.getInputStream();
output = socket.getOutputStream();
inputstream = new DataInputStream(input);
outputstream = new DataOutputStream(output);
isConnected = true;
}
catch (Exception e)
{
try
{
Thread.sleep(100);
}
catch (Exception ex) {}
}
}
while (!isConnected);
}

public static String sendToServer(String json)
{
try
{
connect(Constants.SERVER_HOST, Constants.SERVER_PORT);

if (isConnected)
{
Log.e("SOCKET MNGR", "[SOCKET] Sending " + json);
outputstream.writeInt(json.getBytes().length);
outputstream.write(json.getBytes());

int arrLength = inputstream.readInt();
if (arrLength > 0)
{
byte[] fromServer = new byte[arrLength];
inputstream.read(fromServer, 0, fromServer.length);
String content = new String( Base64.decode(new String(fromServer)) );
Log.e("SOCKET MNGR", "[SOCKET] Reading " + content);
return content;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}

还有我的服务器:

public Client(Socket socket)
{
try
{
this.socket = socket;
this.input = this.socket.getInputStream();
this.output = this.socket.getOutputStream();
this.inputstream = new DataInputStream(this.input);
this.outputstream = new DataOutputStream(this.output);
}
catch (Exception e) {}
}

public void run()
{
while (true)
{
try
{
int arrLength = inputstream.readInt();
byte[] fromClient = new byte[arrLength];
inputstream.read(fromClient, 0, fromClient.length);
String request = new String(fromClient);
String read = new String( Base64.decode(request) );
String[] data = request.split("###");

if (data.length == 2 && data[0].equals("commit"))
{
CommitInterface.commit(data[1], true);
outputstream.writeInt(0);
outputstream.write(new byte[] {});
}
else if (data.length == 3 && data[0].equals("evaluate"))
{
EvaluateInterface.eval(data[1], data[2].equals("true") ? true : false);
outputstream.writeInt(0);
outputstream.write(new byte[] {});
}
else if (data.length == 2 && data[0].equals("publish"))
{
String content = PublishInterface.publish(new Vector<String>(Arrays.asList(data[1].split("|"))));
String encoded = Base64.encode(content.getBytes());
outputstream.writeInt(content.getBytes().length);
outputstream.write(content.getBytes());
}
}
catch (Exception e)
{
this.disconnect();
}
}
}

当我在手机上使用函数sendToServer时,我得到了以下日志:

06-29 13:26:05.823: E/Kramer(1781): [SOCKET] Sending commit###{"user":"me"}
06-29 13:26:05.823: W/System.err(1781): java.io.EOFException
06-29 13:26:05.823: W/System.err(1781): at java.io.DataInputStream.readInt(DataInputStream.java:287)
06-29 13:26:05.823: W/System.err(1781): at com.vodemki.network.KramerCommunicator.sendToServer(ServerCommunicator.java:78)
06-29 13:26:05.823: W/System.err(1781): at com.vodemki.activities.PhoneBookActivity$6.run(PhoneBookActivity.java:298)

第 78 行是 int arrLength = inputstream.readInt();

知道为什么会出现此错误吗?

谢谢。


我试过使用 UDP,但无法从智能手机读取内容。

我的代码如下:

服务器端:

public void run()
{
byte[] inData = new byte[48];
byte[] outData = new byte[48];
String message;
DatagramSocket socket;

try
{
socket = new DatagramSocket(this.port);
while (true)
{
DatagramPacket in = new DatagramPacket(inData, inData.length);
socket.receive(in);
InetAddress senderIP = in.getAddress();
int senderPort = in.getPort();
message = new String(in.getData(), 0, in.getLength());
System.out.println("Got "+message+" from "+senderIP+":"+senderPort);
outData = "Pong".getBytes();
DatagramPacket out = new DatagramPacket(outData, outData.length, senderIP, senderPort);
socket.send(out);
}
}
catch (Exception e) {}
}

客户端(智能手机):

public static String sendToServer(String json)
{
try
{
DatagramSocket socket = new DatagramSocket();
InetAddress serverIP = InetAddress.getByName(Constants.SERVER_HOST);
byte[] outData = ("Ping").getBytes();
DatagramPacket out = new DatagramPacket(outData,outData.length, serverIP, Constants.SERVER_PORT);
Log.e("kramer", "SENDING SOCKET");
socket.send(out);
Log.e("kramer", "SOCKET SENT");
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}

智能手机成功记录了这两条线,但我的服务器没有收到任何信息。有什么想法吗?

最佳答案

问题已解决。

防火墙阻止了传入连接。我只是将端口作为 UDP 打开。

关于Java 套接字 readInt EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11260980/

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