【问题标题】:android - LAN socket programming -- getting nullpointer and address-in use exceptionandroid - LAN 套接字编程 - 获取空指针和地址使用异常
【发布时间】:2013-08-12 16:54:06
【问题描述】:

我已经完成了一个应用程序,其中 android 应用程序将数据发送到 java desktop swing 应用程序,以及使用 TCP 套接字编程通过 wifi 从桌面发送数据到 android。

应用程序是一个酒店厨房订单预订系统

问题描述了 Dine_Tables 类包含代表酒店中每张桌子的按钮,例如,在单击 table1 按钮时,它会启动 BackgroundServers Asyntask,它运行一个服务器来接收桌面应用程序数据将活动从Dinein_Tables.java 转移到Food_Customizer.java

Food_Customizer.java 中点击提交按钮,它会启动ServersendAsyncAction Asyntask,它将一些数据发送到桌面摇摆应用程序。

处理后的桌面应用程序将一些数据发送到android应用程序,在android应用程序中运行的服务器在接收到数据后再次从Food_Customizer.javaBackgroundServers中的Dinein_Tables.java活动@987654328 @onPostExecute 方法。

问题是,当我执行此过程时,由于地址使用和 BackgroundServers 中 socket = serverSocket.accept(); 处的 Null-Pointer 异常,应用程序停止了两到三倍。强>Asyntask.

谁能告诉我一些解决这个问题的方法

Dinein_Tables.java

public class Dinein_Tables extends Activity {
:
:


 table1.setOnClickListener(new OnClickListener() {

                    public void onClick(final View v) {
                        new Handler().postDelayed(new Runnable() {

                            public void run() {

                                        Food_Customizer.BackgroundServers ob = new Food_Customizer().new BackgroundServers(contexts);
                                        ob.execute("");
                                      Intent toAnotherActivity = new Intent(v.getContext(), Food_Customizer.class);
                                      startActivity(toAnotherActivity);
                                      finish();
                            }

                        }, 100L);    

                    }

                });
}

Food_Customizer.java

public class Food_Customizer extends Activity {
:
:
    submit= (Button)findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          pd = ProgressDialog.show(contexts, "Sending to Server...","Please Wait...", true, false);
          new ServersendAsyncAction().execute();
    }
    });
:
:



      /****************************** AsyncTask ********************************************************/

        private class ServersendAsyncAction extends AsyncTask<String, Void, String> {

            /****************************** AsyncTask doInBackground() ACTION ********************************/
            protected String doInBackground(String... args) {


                Socket socket = null;
                DataOutputStream dataOutputStream = null;
                DataInputStream dataInputStream = null;
                boolean flag = true;
                while (flag) /******** If data is send flag turn to be false *******/
                {
                    try {
                        socket = new Socket("192.168.1.74", 4444);
                        dataOutputStream = new DataOutputStream(socket.getOutputStream());
                        dataInputStream = new DataInputStream(socket.getInputStream());
                        dataOutputStream.writeUTF(datastosend);
                        flag = false;
                        } catch (UnknownHostException e) {
                        flag = true;
                        e.printStackTrace();
                        } catch (IOException e) {
                        flag = true;
                        e.printStackTrace();
                        }

                    /******** CLOSING SOCKET *****************/
                    finally {
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        /******** CLOSING DATAOUTPUTSTREAM *******/
                        if (dataOutputStream != null) {
                            try {
                                dataOutputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        /******** CLOSING DATAINPUTSTREAM ********/
                        if (dataInputStream != null) {
                            try {
                                dataInputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                return null;
                /******** returns what you want to pass to the onPostExecute() *******/
            }

            /****************************** AsyncTask onPostExecute() ACTION *********************************/
            protected void onPostExecute(String result) {

            }

        /********************* ENDING OF ASYN TASK CLASS ServersendAsyncAction ***************************/
        }



        public Context con;
            public static ServerSocket serverSocket = null;

        public class BackgroundServers extends AsyncTask<String, Void, String> {

            public BackgroundServers(Context context) {
                con=context;
            }

            /****************************** AsyncTask doInBackground() ACTION ********************************/
            protected  String doInBackground(String... args) {

                Socket socket = null;
                DataInputStream dataInputStream = null;
                DataOutputStream dataOutputStream = null;

                try {
                    serverSocket = new ServerSocket(9999);
                    System.out.println("Listening :9999");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                while (true) {
                    try {
                        socket = serverSocket.accept();
                        dataInputStream = new DataInputStream(
                        socket.getInputStream());
                        dataOutputStream = new DataOutputStream(socket.getOutputStream());
                        System.out.println("ip: " + socket.getInetAddress());
                        String incoming_message=(dataInputStream.readUTF());
                        incoming_message=incoming_message.replace("/", "");
                        String recdatas[]=incoming_message.split("#");
                        if(recdatas[0].equalsIgnoreCase("success"))
                        {
                          DatabaseConnection dbs=new DatabaseConnection(con);
                          int status=dbs.update("UPDATE hotel_pub_tables SET status='occupied' WHERE tableno='"+recdatas[1]+"'");
                          if(status>0)
                                  {
                              tabelstatus=1;
                              //msg.obj="Table status changed!!!";
                              System.out.println("Table status changed!!!");
                              if (true) {
                                    System.out.println("entered 222");

                                               System.out.println(tabelstatus);
                                               if(tabelstatus==1)
                                                {
                                                   System.out.println(tabelstatus); 
                                                   Food_Customizer.pd.dismiss();
                                                   System.out.println("success"); 

                                                }
                                                else if(tabelstatus==2)
                                                {
                                                    Food_Customizer.pd.dismiss();

                                                  Intent intent = new Intent(Food_Customizer.this, Dinein_Tables.class);
                                                  startActivity(intent);
                                                  finish(); 
                                                }

                                }
                          }
                          else
                              tabelstatus=2;
                          dbs.close();
                        }
                        dataOutputStream.writeUTF("Hello!");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        if (dataInputStream != null) {
                            try {
                                dataInputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        if (dataOutputStream != null) {
                            try {
                                dataOutputStream.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }
                             return null;
                }

                /******** returns what you want to pass to the onPostExecute() *******/
            }

            /****************************** AsyncTask onPostExecute() ACTION *********************************/
            @Override
            protected void onPostExecute(String result) {
                System.out.println("eneterd on posttttttttttttttt");
                con.startActivity(new Intent(con, Dinein_Tables.class));
                finish();
                }

            }
        }
        /********************* ENDING OF ASYN TASK CLASS BackgroundServers ***************************/
}

【问题讨论】:

    标签: android sockets android-asynctask nullpointerexception


    【解决方案1】:

    很明显,您在端口 9999 上设置了服务器:

    serverSocket = new ServerSocket(9999);
    

    但是你通过 4444 端口连接到服务器:

    socket = new Socket("192.168.1.74", 4444);
    

    确保您连接到正确的端口号,否则将无法正常工作。希望这会有所帮助。

    【讨论】:

    • 先生,用于将数据从 android 发送到桌面,我使用端口 4444,用于从桌面接收数据到 android,我使用 9999 .....
    • 是的,需要是同一个端口,否则无法正常通信。如果建立连接,则可以平等地发送和接收数据,不需要第二个端口只发送或接收数据。
    • 先生,如果有 10 位服务员在那里,那么将有 10 台平板电脑运行这个 android 应用程序,一次一台桌面......在这种情况下,如果我使用相同的端口它会导致任何问题
    • 只有一个套接字服务器可以为多个客户端服务,但您需要为每个客户端启动一个单独的线程。所以使用同一个端口应该不会造成任何麻烦。请参阅:stackoverflow.com/questions/10131377/…。希望这可以帮助您解决问题。欢呼
    • 我已将端口 4444 更改为 9999......现在我得到 java.net.BindException: bind failed: EADDRINUSE (Address已在使用) serverSocket 处的异常 = new ServerSocket(9999);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多