gpt4 book ai didi

java - 服务器未读取数据

转载 作者:行者123 更新时间:2023-12-03 11:55:39 25 4
gpt4 key购买 nike

我已经修改了Java套接字教程代码,以允许客户端发送字符串数组,而服务器将数组更改后的字符串传递回数组。它工作正常。

现在,我试图创建一个充当服务器组件的服务器类。该组件基本上仅从客户端接收数据并打印客户端数据。但是,服务器接受连接但不读取。因此,要查看发生了什么,我尝试仅从main运行服务器套接字。即使那样也不起作用。我不确定为什么教程代码可以工作,但是当我在代码中执行确切的操作时却不确定。我不明白这种奇怪行为的原因。

以下是服务器的Java教程代码

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

public class Server {
public static void main(String[] args) throws IOException {

int portNumber = Integer.parseInt(args[0]);

try
(
ServerSocket serverSocket =
new ServerSocket(Integer.parseInt(args[0]));
Socket clientSocket = serverSocket.accept();

ObjectInputStream objIn = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream objOut = new ObjectOutputStream(clientSocket.getOutputStream());
) {
String str[][] = (String [][])objIn.readObject();

if(str != null)
{ for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
System.out.println(str[i][j]);

for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
str[i][j] ="Success";
objOut.writeObject(str);
}

} catch (IOException | ClassNotFoundException ecp) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(ecp.getMessage());
}
}
}

以下是Java教程中的客户端
public class EchoClient {
public static void main(String[] args) throws IOException {

if (args.length != 2) {
System.err.println(
"Usage: java EchoClient <host name> <port number>");
System.exit(1);
}

String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);

try (
Socket echoSocket = new Socket(hostName,portNumber);
ObjectOutputStream objOut = new ObjectOutputStream(echoSocket.getOutputStream());
ObjectInputStream objIn = new ObjectInputStream(echoSocket.getInputStream());
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))

) {
String str[][] = new String[8][8];
for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
str[i][j] = "Test";
objOut.writeObject(str);
System.out.println("String array sent");

str = (String[][])objIn.readObject();
for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
System.out.println(str[i][j]);


} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

现在,以下是我的简单服务器尝试,当EchoClient发送数据时,该尝试不读取。它连接但不读取。
import java.net.*;
import java.io.*;

public class Server
{

public static void main (String[] args)
{

try {
ServerSocket serv = new ServerSocket(8000);
Socket client = serv.accept();

ObjectInputStream objin = new ObjectInputStream(client.getInputStream());
String[][] str = (String[][]) objin.readObject();

for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
str[i][j] ="Success";

System.out.println(str[0][0]);
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

请让我知道我要去哪里了。请注意,Java教程类EchoServer和EchoClient可以正常工作。但是我无法自己复制它。 Server类接受EchoClient,但不读取。

编辑:以下是我的固定尝试。这按预期工作。

服务器类:
import java.net.*;
import java.io.*;

public class Server
{
ServerSocket serverSocket;
Socket clientSocket;
String hostname;
int portNo = 8000;
String clientData;
int count = 0;
public Server()
{
try {
serverSocket = new ServerSocket(portNo);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void startReceiving()
{
try {
if(clientSocket == null)
{ clientSocket = serverSocket.accept();
System.out.println("Client Connected");
}

ObjectInputStream objIn = new ObjectInputStream(clientSocket.getInputStream());

//String strtemp[][];
String data;
while(true)
{
if((data = (String)objIn.readObject()) != null)
{
this.clientData = data;
System.out.println(this.clientData);

break;
}

/*if((strtemp = (String[][]) objIn.readObject())!= null)
{
str = strtemp;
System.out.println("received data");
for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
System.out.println(str[i][j]);
}
else
System.out.println("no data received");*/
}
sendData();

} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
try {
clientSocket.close();
clientSocket = null;
startReceiving();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}


}
public void sendData()
{
/*for(int i = 0; i<str.length;i++)
for(int j = 0; j<str[i].length;j++)
str[i][j] ="Success";*/
this.clientData = "client request response" + count;
count++;
ObjectOutputStream objOut;
try {
objOut = new ObjectOutputStream(clientSocket.getOutputStream());
objOut.writeObject(this.clientData);
objOut.flush();
startReceiving();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main (String[] args)
{
Server server = new Server();
server.startReceiving();

}
}

以下是客户:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestClient
{
Socket connection;
String hostname = "localhost";
int portNo = 8000;
String incomingData;
int count;
public TestClient()
{
try {
connection = new Socket(hostname,portNo);
count = 0;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receiveData()
{
ObjectInputStream in;
try {
in = new ObjectInputStream(this.connection.getInputStream());
String serverMessage;
while (true)
{
if((serverMessage = (String)in.readObject()) != null)
{
System.out.println(serverMessage);
break;
}
}
if(count < 5)
sendData();
else
{
in.close();
this.connection.close();
}

} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//

}
public void sendData()
{
try {
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject("Client request" + count);
out.flush();
count++;
receiveData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main(String args[])
{
TestClient testClient = new TestClient();
testClient.sendData();

}
}

最佳答案

您的客户端创建了一个ObjectInputStream,但是服务器没有创建相应的ObjectOutputStream,因此客户端阻止了从未发送的对象流 header 。

另外,您应该摆脱所有的ReadersWriters.。您不能在同一套接字上组合不同类型的读取器/写入器/流。

另外,您的服务器无法正确关闭套接字。

错误消息“无法为...获取I/O”也没有意义。我知道它来自Java教程,几十年前我就向他们提示过,但是您永远不应该编写自己的错误消息并丢弃此类异常所附带的错误消息。

关于java - 服务器未读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28491214/

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