Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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_Android_Sockets_Client Server - Fatal编程技术网

Java 每次服务器启动时,客户端都会创建新的套接字对象

Java 每次服务器启动时,客户端都会创建新的套接字对象,java,android,sockets,client-server,Java,Android,Sockets,Client Server,我做了一个套接字编程,客户端驻留在android上,服务器驻留在桌面上。。。。。。。我们知道,每当服务器关闭时,客户端就会失去连接……因此它会经历一个循环,直到连接到服务器 这里的问题在下面的代码中 tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT); 在while循环中,在连接断开的情况下…………但是当连接再次打开时,它会创建一个新对象 谁能告诉我如何解决这个问题 在客户端 while(true){ try {

我做了一个套接字编程,客户端驻留在android上,服务器驻留在桌面上。。。。。。。我们知道,每当服务器关闭时,客户端就会失去连接……因此它会经历一个循环,直到连接到服务器

这里的问题在下面的代码中

tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
在while循环中,在连接断开的情况下…………但是当连接再次打开时,它会创建一个新对象

谁能告诉我如何解决这个问题

在客户端

while(true){
        try {

            tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);

            tabletout = new PrintWriter(tabletclient.getOutputStream());
            in = new Scanner(tabletclient.getInputStream());
            try
            {
                if((line = in.nextLine())!=null)
                {
                    // my task to be done 
                }
            }catch(Exception d){
                System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"+tabletclient.isConnected());

            }
            } catch (UnknownHostException e) {  System.out.println("Entered 2.........");
            } catch (IOException e) {  System.out.println("Entered 3.........");e.printStackTrace();
            }
         }
:
:
       private Set <Socket> TABhs=new HashSet<Socket>();
:
:
        new Thread(new TABServerThread()).start(); // runs in background
:
:
:
 class ServerThread implements Runnable {
        private ServerSocket server;

        @Override
        public void run() {
            try {
                server = new ServerSocket(SERVER_PORT);
                System.out.println("Server Start the server at port " + SERVER_PORT
                        + " and waiting for clients...");
                while (true) {
                    Socket socket = server.accept();
                    System.out.println("Server Accept socket connection: "
                                    + socket.getLocalAddress());
                      new Thread(new ClientHandler(socket)).start();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

private static PrintWriter out;

    class ClientHandler implements Runnable {

        private Socket clientSocket;

        private Scanner in;

        public ClientHandler(Socket clietSocket) {
            this.clientSocket = clietSocket;
        }

        @Override
        public void run() {
            try {
                out = new PrintWriter(clientSocket.getOutputStream());
                in = new Scanner(clientSocket.getInputStream());
                String line;
                System.out.println("ClientHandlerThread Start communication with : "+ clientSocket.getLocalAddress());
                try{
                while((line = in.nextLine()) != null) {
                    System.out.println("ClientHandlerThread Client says: " + line);
                    String dat[]=line.split("#");
                        String query="insert into table_orders (tableno,kotno, orders,status) values('"+dat[1]+"','"+dat[0]+"','"+dat[2]+"','pending')";
                        try {
                            int i= dbGetDet.insertDetails(query);
                            if(i>0)
                            {
                             fillTable();
                             filtercomboBox();
                             out.print("success");
                             out.flush();
                             for(Socket so:TABhs)
                             {
                                PrintWriter ot = new PrintWriter(so.getOutputStream());
                                ot.println("tableallocation#"+dat[1]);
                                ot.flush();
                             }
                                System.out.println("SENDED 'SUCCESS' TO CLIENT");
                            }
                        } catch (Exception ex) {
                            Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
                        } 
                   // }
                }
                }catch(Exception r){}
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
在服务器端

while(true){
        try {

            tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);

            tabletout = new PrintWriter(tabletclient.getOutputStream());
            in = new Scanner(tabletclient.getInputStream());
            try
            {
                if((line = in.nextLine())!=null)
                {
                    // my task to be done 
                }
            }catch(Exception d){
                System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"+tabletclient.isConnected());

            }
            } catch (UnknownHostException e) {  System.out.println("Entered 2.........");
            } catch (IOException e) {  System.out.println("Entered 3.........");e.printStackTrace();
            }
         }
:
:
       private Set <Socket> TABhs=new HashSet<Socket>();
:
:
        new Thread(new TABServerThread()).start(); // runs in background
:
:
:
 class ServerThread implements Runnable {
        private ServerSocket server;

        @Override
        public void run() {
            try {
                server = new ServerSocket(SERVER_PORT);
                System.out.println("Server Start the server at port " + SERVER_PORT
                        + " and waiting for clients...");
                while (true) {
                    Socket socket = server.accept();
                    System.out.println("Server Accept socket connection: "
                                    + socket.getLocalAddress());
                      new Thread(new ClientHandler(socket)).start();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

private static PrintWriter out;

    class ClientHandler implements Runnable {

        private Socket clientSocket;

        private Scanner in;

        public ClientHandler(Socket clietSocket) {
            this.clientSocket = clietSocket;
        }

        @Override
        public void run() {
            try {
                out = new PrintWriter(clientSocket.getOutputStream());
                in = new Scanner(clientSocket.getInputStream());
                String line;
                System.out.println("ClientHandlerThread Start communication with : "+ clientSocket.getLocalAddress());
                try{
                while((line = in.nextLine()) != null) {
                    System.out.println("ClientHandlerThread Client says: " + line);
                    String dat[]=line.split("#");
                        String query="insert into table_orders (tableno,kotno, orders,status) values('"+dat[1]+"','"+dat[0]+"','"+dat[2]+"','pending')";
                        try {
                            int i= dbGetDet.insertDetails(query);
                            if(i>0)
                            {
                             fillTable();
                             filtercomboBox();
                             out.print("success");
                             out.flush();
                             for(Socket so:TABhs)
                             {
                                PrintWriter ot = new PrintWriter(so.getOutputStream());
                                ot.println("tableallocation#"+dat[1]);
                                ot.flush();
                             }
                                System.out.println("SENDED 'SUCCESS' TO CLIENT");
                            }
                        } catch (Exception ex) {
                            Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
                        } 
                   // }
                }
                }catch(Exception r){}
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
第二次单击

SEEE SOCKET Ready 1----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 2----->false
TABhs--------------------->2
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 3----->false
TABhs--------------------->4
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
SEEE SOCKET Ready 3----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 4----->false
TABhs--------------------->5
第四次单击

SEEE SOCKET Ready 1----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 2----->false
TABhs--------------------->2
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 3----->false
TABhs--------------------->4
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
SEEE SOCKET Ready 3----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 4----->false
TABhs--------------------->5

我认为问题是在客户端,你读了一行,然后创建了一个新的连接。 我认为您必须一直读取套接字,直到它关闭或出现错误

例如:

while (true)
{
    tabletclient = null;
    int loop = 0;
    // loop until a connection is established
    while (tabletclient == null)
    {
        try
        {
            tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            // set the value to quit when no connection could be established
            if (loop++ > 100)
                return;
        }
    }

    try
    {
        tabletout = new PrintWriter(tabletclient.getOutputStream());
        in = new Scanner(tabletclient.getInputStream());
        // read the socket until it's closed or an error occurs
        try
        {
            while ((line = in.nextLine()) != null)
            {
                // my task to be done
            }

        }
        catch (Exception d)
        {
            System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"
                            + tabletclient.isConnected());
        }
        tabletsocket.close();
    }
    catch (UnknownHostException e)
    {
        System.out.println("Entered 2.........");
    }
    catch (IOException e)
    {
        System.out.println("Entered 3.........");
        e.printStackTrace();
    }
}

此外,从服务器到客户端的传输完成后,必须关闭服务器端。

解决什么问题?你没有关闭套接字,但你还没有说你的问题是什么。先生,你能看到输出(更新)吗?这就是我面临的问题…………每次哈希集增加时,不要对大写的人大喊大叫。这被认为是非常无礼的。我真的很抱歉,先生……你还没有把你的问题解释清楚。我们无法读懂你的心思。