Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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

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
服务器套接字总是向客户端返回null,Java_Java_Sockets_Networking_Tcp - Fatal编程技术网

服务器套接字总是向客户端返回null,Java

服务器套接字总是向客户端返回null,Java,java,sockets,networking,tcp,Java,Sockets,Networking,Tcp,一、 做一些计算机网络作业,我必须开发某种分布式数据库管理系统,它们通过对等网络相互连接,因此我在一个.java文件中有一个TCP客户端和一个TCP服务器,它们通过线程彼此相邻运行。该类的TCP服务器总是从其他TCP客户端侦听其他TCP客户端并为其提供服务,问题是当我System.out我必须在服务器端发送回客户端的字符串时,它是按照应该的方式发送的,但发送后,客户端什么也得不到,并打印空值。我根据在网上找到的教程编写代码,当我测试它们时,它们工作得很好,但在我自己的代码中不起作用。你能看看我的

一、 做一些计算机网络作业,我必须开发某种分布式数据库管理系统,它们通过对等网络相互连接,因此我在一个.java文件中有一个TCP客户端和一个TCP服务器,它们通过线程彼此相邻运行。该类的TCP服务器总是从其他TCP客户端侦听其他TCP客户端并为其提供服务,问题是当我
System.out
我必须在服务器端发送回客户端的字符串时,它是按照应该的方式发送的,但发送后,客户端什么也得不到,并打印空值。我根据在网上找到的教程编写代码,当我测试它们时,它们工作得很好,但在我自己的代码中不起作用。你能看看我的问题在哪里吗?谢谢

class CommandComp
{
    int PORT = 1210;
    int PORT2 = 1211;
    String IPLocal = "";
    String IPdest = "";
    InetAddress IPAD;
    InetAddress IPAD2;
    int numOfNodes;
    int numOfNodesnonchanged;
    String[] Nodes;
    Random rand = new Random();
    int max = 2000;
    int min = 1000;
    String command;

    Socket clientSocket;

    CommandComp(String[] IPdest, String IPLocal, int numOfNodes, String command)
    {
        try
        {
            this.numOfNodes = numOfNodes;
            numOfNodesnonchanged = numOfNodes;
            this.IPLocal = IPLocal;
            this.Nodes = IPdest;
            this.command = command;
            // this.IPAD = InetAddress.getByName(this.IPdest);
            this.IPAD2 = InetAddress.getByName(this.IPLocal);
            // clientSocket = new Socket(this.IPAD , PORT ,this.IPAD2 , PORT2 );
        }
        catch (Exception e)
        {
            // //e.printStackTrace();
        }
    }

    public String call()
    {

        int i = 0;
        while (numOfNodes > 0)
        {
            String response = "";
            try
            {
                Thread.sleep(rand.nextInt(max - min + 1) + min);
                i = numOfNodes - 1;
                int max2 = 50;
                int min2 = 10;
                this.IPAD = InetAddress.getByName(Nodes[i]);
                clientSocket = new Socket(this.IPAD, PORT, this.IPAD2, PORT2 + rand.nextInt(max2 - min2 + 1) + min2);
                PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                outToServer.println(command);
                System.out.println(inFromServer.readLine());
                response = inFromServer.readLine();
                outToServer.close();
                inFromServer.close();
                clientSocket.close();
                numOfNodes--;
                System.out.println(Nodes[i] + " Remote DBMS");
                System.out.println(response);

            }
            catch (Exception e)
            {
                e.printStackTrace();
                try
                {
                    clientSocket.close();
                }
                catch (Exception e2)
                {
                    // TODO: handle exception
                }
            }
        }
        return command;
    }
}

class TCPListnerService
    implements Callable<Object>
{    
    String from;

    String to;
    ServerSocket Server;
    String IP = "";
    int numOfNodes;
    int numofNodesUnchanged;
    static clientThread t[];

    TCPListnerService(String IP, int numOfNodes)
    {
        try
        {
            this.IP = IP;
            this.numOfNodes = numOfNodes;
            numofNodesUnchanged = numOfNodes * 2;
            this.t = new clientThread[numofNodesUnchanged];
            InetAddress IPAD = InetAddress.getByName(IP);
            Server = new ServerSocket(1210, 20, IPAD);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String call()
        throws Exception
    {

        String gotten = "";
        while (numOfNodes > 0)
        {
            Socket connected = Server.accept();

            for (int i = 0; i < numofNodesUnchanged; i++)
            {
                if (t[i] == null)
                {
                    (t[i] = new clientThread(connected)).start();
                    break;
                }
            }
        }
        return gotten;
    }

}

class clientThread
    extends Thread
{

    Socket clientSocket = null;
    sqlite DB = new sqlite();
    String response = "";
    String fromclient;
    String delims = "[ =)(',\n\t\r]+";
    String[] tokens;

    public clientThread(Socket clientSocket)
    {
        this.clientSocket = clientSocket;
    }

    public void run()
    {

        try
        {
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
            fromclient = inFromClient.readLine();

            tokens = fromclient.split(delims);
            if (tokens[0].equalsIgnoreCase("create")
                || tokens[0].equalsIgnoreCase("drop")
                || tokens[0].equalsIgnoreCase("delete")
                || tokens[0].equalsIgnoreCase("insert")
                || tokens[0].equalsIgnoreCase("update")
                || tokens[0].equalsIgnoreCase("select"))
            {
                response = DB.RunQuery(fromclient);
                System.out.println(response);
                outToClient.print(response);
                clientS.close();

            }
            else if (tokens[0].equalsIgnoreCase("shut"))
            {
                System.exit(0);
            }

            inFromClient.close();
            outToClient.close();
            clientSocket.close();
        }
        catch (Exception e)
        {
        }
        ;
    }
}
class命令组件
{
int端口=1210;
int端口2=1211;
字符串IPLocal=“”;
字符串IPdest=“”;
iNet地址IPAD;
iNet地址IPAD2;
int numOfNodes;
int numOfNodesnonchanged;
字符串[]节点;
Random rand=新的Random();
int max=2000;
int最小值=1000;
字符串命令;
插座客户端插座;
CommandComp(String[]IPdest,String IPLocal,int numOfNodes,String命令)
{
尝试
{
this.numOfNodes=numOfNodes;
numOfNodesnonchanged=numOfNodes;
this.IPLocal=IPLocal;
this.Nodes=IPdest;
this.command=命令;
//this.IPAD=InetAddress.getByName(this.IPdest);
this.IPAD2=InetAddress.getByName(this.IPLocal);
//clientSocket=新套接字(this.IPAD,端口,this.IPAD2,端口2);
}
捕获(例外e)
{
////e.printStackTrace();
}
}
公共字符串调用()
{
int i=0;
而(numOfNodes>0)
{
字符串响应=”;
尝试
{
线程睡眠(rand.nextInt(max-min+1)+min);
i=numOfNodes-1;
int max2=50;
int min2=10;
this.IPAD=InetAddress.getByName(节点[i]);
clientSocket=新套接字(this.IPAD,PORT,this.IPAD2,PORT2+rand.nextInt(max2-min2+1)+min2);
PrintWriter outToServer=新的PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader INFOROMSERVER=新的BufferedReader(新的InputStreamReader(clientSocket.getInputStream());
println(命令);
System.out.println(informserver.readLine());
response=inforomserver.readLine();
outToServer.close();
nfromserver.close();
clientSocket.close();
numOfNodes--;
System.out.println(节点[i]+“远程DBMS”);
System.out.println(响应);
}
捕获(例外e)
{
e、 printStackTrace();
尝试
{
clientSocket.close();
}
捕获(异常e2)
{
//TODO:处理异常
}
}
}
返回命令;
}
}
类TCPListnerService
实现可调用的
{    
串从;
串到;
服务器套接字服务器;
字符串IP=“”;
int numOfNodes;
int numofnodes不变;
静态客户端线程t[];
TCPListnerService(字符串IP,整数节点)
{
尝试
{
this.IP=IP;
this.numOfNodes=numOfNodes;
numofNodesUnchanged=numOfNodes*2;
this.t=newclientthread[numofodessunchanged];
InetAddress IPAD=InetAddress.getByName(IP);
服务器=新的服务器插座(1210,20,IPAD);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
公共字符串调用()
抛出异常
{
字符串=”;
而(numOfNodes>0)
{
Socket connected=Server.accept();
对于(int i=0;i
问题在于:

inFromClient.close();
outToClient.close();
clientSocket.close();
您正在关闭(1)输入流,这将关闭套接字