1

i've done an application in which the android application send datas to java desktop swing application as well as send datas from desktop to android using TCP socket programming through wifi.

Th application is a Hotel Kitchen order booking system

The problem describes that Dine_Tables class contains buttons which represents each tables in a hotel, on clicking table1 button for example it starts the BackgroundServers Asyntask which runs a server for receiving desktop application datas also it takes the activity from Dinein_Tables.java to Food_Customizer.java.

In Food_Customizer.java on clicking submit button it starts ServersendAsyncAction Asyntask which sends some datas to desktop swing application.

The desktop application after processing sends some datas to android application, The server that runs in the android application on receiving the datas goes again from Food_Customizer.java to Dinein_Tables.java activity in the BackgroundServers Asyntask onPostExecute method.

The problem is that when i do this process a two or three times the application stop due to address-in use and Null-Pointer exception at socket = serverSocket.accept(); in the BackgroundServers Asyntask.

Can anyone please tell me some solution for this problem

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 ***************************/
}

1 Answers1

1

Well it's obvious that you setup your server on port 9999:

serverSocket = new ServerSocket(9999);

But you connect with the server on port 4444:

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

Make sure you connect to the correct port-number otherwise it wont work. Hope this helps.

user2652394
  • 1,686
  • 1
  • 13
  • 15
  • sir for send datas from android to desktop i'm using port 4444 and for receiving datas from desktop to android i'm using 9999..... –  Aug 11 '13 at 06:02
  • yes it is, it needs to be the same port, otherwise it can't communicate properly. if the connection is setup, both can send and receive data equally, no need the second port to just send or receive data. – user2652394 Aug 11 '13 at 06:55
  • sir for hotel if 10 waiters are there, then there will be 10 tablets with this android application running on it and one desktop at a time.....in that case if i use same port does it causes any problem –  Aug 11 '13 at 06:59
  • It's OK for only one socket-server serves many clients, but you need to start a separate thread for each and every client. So using the same port should NOT cause any troubles. Refer to this: http://stackoverflow.com/questions/10131377/socket-programming-multiple-client-to-one-server. Hope this will help you out of your problem. cheers – user2652394 Aug 11 '13 at 07:06
  • i've changed the port 4444 to 9999................now i'm getting java.net.BindException: bind failed: EADDRINUSE (Address already in use) exception at serverSocket = new ServerSocket(9999); –  Aug 11 '13 at 07:09
  • Seems like you create more than ONE server at the same port at a time, why you need each android application as a server in this case? any restriction? Can you use the reversed process? ONE desktop socket-server serves to android client apps? Make that logic much simpler and applicable. – user2652394 Aug 11 '13 at 07:19
  • even though for receiving datas from desktop i need a server in android right............ –  Aug 11 '13 at 09:00
  • No you dont, server in desktop is to broadcast the service and once the connection has been setup, client and server can communicate with each other.(server sends client receive and the other way around only on ONE connection). Sorry English is not my main, if this cause misunderstanding, let me know. Cheers! – user2652394 Aug 11 '13 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35219/discussion-between-user2040500-and-user2652394) –  Aug 11 '13 at 09:17