Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务器未读取数据_Java_Sockets_Server - Fatal编程技术网

Java 服务器未读取数据

Java 服务器未读取数据,java,sockets,server,Java,Sockets,Server,我修改了JavaSocket教程代码,允许客户端发送一个字符串数组,服务器在数组中的字符串更改后将数组传回。它很好用 现在,我尝试创建一个服务器类,其行为类似于服务器组件。组件基本上只从客户端接收数据并打印客户端数据。但是,服务器接受连接,但不读取。因此,为了查看发生了什么,我尝试从main运行一个服务器套接字。即使这样也不行。我不知道为什么教程代码可以工作,但当我在代码中做正确的事情时就不行了。我不明白这种奇怪行为的原因 以下是服务器的Java教程代码 import java.net.*; i

我修改了JavaSocket教程代码,允许客户端发送一个字符串数组,服务器在数组中的字符串更改后将数组传回。它很好用

现在,我尝试创建一个服务器类,其行为类似于服务器组件。组件基本上只从客户端接收数据并打印客户端数据。但是,服务器接受连接,但不读取。因此,为了查看发生了什么,我尝试从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());
    }
}
}
import java.net.*;
导入java.io.*;
公共类服务器{
公共静态void main(字符串[]args)引发IOException{
int portNumber=Integer.parseInt(args[0]);
尝试
(
服务器套接字服务器套接字=
新的ServerSocket(Integer.parseInt(args[0]);
Socket clientSocket=serverSocket.accept();
ObjectInputStream objIn=新的ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream objOut=新的ObjectOutputStream(clientSocket.getOutputStream());
) {
String str[][]=(String[][])objIn.readObject();
如果(str!=null)

{for(int i=0;i您的客户端创建了一个
ObjectInputStream
,但服务器没有创建相应的
ObjectOutputStream
,因此客户端阻止等待从未发送的对象流头

此外,您还应该去掉所有的
读卡器
写卡器。
您不能在同一个套接字上组合不同类型的读卡器/写卡器/流

此外,服务器未正确关闭套接字


此外,错误消息“无法为…获取I/O”也毫无意义。我知道它来自Java教程,几十年前我曾向他们抱怨过,但你永远不应该编造自己的错误消息,然后扔掉这样的异常消息。

你没有正确理解此应用程序是如何设计的

您删除了发送str[i][j]的部分,但仍然在循环中将其设置为success,这是不合逻辑的。在第一次运行时,服务器将不读取任何内容,因此它永远不会向数组设置任何值,这就是为什么
System.out.println(str[0][0])
print nothing

您必须了解,在本教程的示例中,第一个循环读取接收到的内容

for(int i = 0; i<str.length;i++)
    for(int j = 0; j<str[i].length;j++)
        System.out.println(str[i][j]);
使用此
main

public static void main (String[] args)
{
    int portNumber = 8000;
    try {
        ServerSocket serv = new ServerSocket(portNumber);
        Socket client = serv.accept();

        ObjectInputStream objin = new  ObjectInputStream(client.getInputStream());
        ObjectOutputStream objOut = new ObjectOutputStream(client.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);
        }

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

确保在try范围之外声明套接字,以便在finally中访问它。

我注意到的第一件事是“客户端”希望从服务器读取输入,但您修改的服务器没有发送任何输入。但即使我对其进行注释,服务器也不会打印收到的数据。是的,您是对的,我不应该将字符串更改为success,因为我没有从服务器发送回数据。但是,我仍然不明白服务器为什么不会重新启动ad字符串数组已发送。服务器是否始终需要发送回数据才能成功读取?@user3213348,因为客户端从未发送数据。它在
new ObjectInputStream(…).
查看我的答案。我已经准备好了工作并修改了它。但我仍然不明白它为什么没有读取。EchoClient确实创建了一个ObjectOutputStream并写入了该对象。服务器也创建了一个ObjectInputStream来读取该对象。因此,即使工作正常,我仍然不清楚该错误。@user3213348因为,您在服务器中创建了ObjectOutputStream,所以客户端一直在等待,从未收到任何数据。因为流头。如果您想在服务器中延迟对象输出流的构造,您还必须在客户端中延迟对象输入流的构造。通常,最好在首先在两端对对象输出流进行ct。
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();

}
}
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);
public static void main (String[] args)
{
    int portNumber = 8000;
    try {
        ServerSocket serv = new ServerSocket(portNumber);
        Socket client = serv.accept();

        ObjectInputStream objin = new  ObjectInputStream(client.getInputStream());
        ObjectOutputStream objOut = new ObjectOutputStream(client.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);
        }

        System.out.println(str[0][0]);
    } catch (IOException | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
try
{
...
}
catch(IOEXception e)
{
...
}
finally
{
    if(socket != null)
        socket.close();
}