0

Good day, I am developing an android app to send textView value to a server made in Java netbeans, I followed a tutorial on YouTube [https://www.youtube.com/watch?v=29y4X65ZUwE ] however when I run the server first it does not send the data through. I am connected to the same Wi-Fi network as well.

EDIT: When I use the code System.out.println(ss.getInetAddress()); in my server class in Java, I get 0.0.0.0 as the ip address but I am connected to a wifi network.

Here is my AsyncTask class (written in android studio):

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

Socket s;
DataOutputStream dos;
PrintWriter pw;

@Override
protected Void doInBackground(String... voids) {

    String number = voids[0];
    try{

        s = new Socket("196.248.139.178", 6000);

        pw = new PrintWriter(s.getOutputStream());
        pw.write(number);
        pw.flush();
        pw.close();
        s.close();

    }catch(IOException ioe){

        ioe.printStackTrace();
    }
    return null;
  }
}

Here is my MainActivity(called it the Orders class, written in android studio):

public class Orders extends AppCompatActivity {

Button  send;
EditText orderNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orders);

    orderNum = findViewById(R.id.orderNum);

    send = findViewById(R.id.send);

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SendData numSender = new SendData();
            numSender.execute(orderNum.getText().toString());

        }
    });
  }
}

The following is my Server Code written in NetBeans, it includes a JFrame with a JTextArea to display to data sent from android phone:

public class OrderList extends javax.swing.JFrame {

static Socket s;
static ServerSocket ss;
static InputStreamReader isr;
static BufferedReader br;
static String numbers;


/**
 * Creates new form OrderList
 */
public OrderList() {

    initComponents();   
}
public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new OrderList().setVisible(true);
        }
    });

     try{

         ss = new ServerSocket(6000);
         while(true){

             s = ss.accept();
             isr = new InputStreamReader(s.getInputStream());
             br = new BufferedReader(isr);
             numbers = br.readLine();

             System.out.println(numbers);
             // orderNumList is the text area where data is going to be set.
             if(orderNumList.getText().equals("")){

                 orderNumList.setText(numbers);
             }
             else{
                 orderNumList.setText(orderNumList.getText()+ "\n" + numbers);

             }

         }

     }catch(IOException e){

         e.printStackTrace();
     }
}

Any advice and help will be highly appreciated.

  • Do you receive any exceptions or any output at all? If so, please provide. – Nika Narushvili Jan 25 '19 at 10:40
  • @NikaNarushvili Unfortunately, I receive no exceptions or output. It does not crash either. Both the app on my phone and the server stay as is. –  Jan 25 '19 at 10:44
  • You're using a PrintWriter and a BufferedReader so are you sure your `orderNum.getText().toString()` method returns a string that is terminated by either `\n` or `\r`? – L.Spillner Jan 25 '19 at 10:47
  • @L.Spillner Sorry for the late reply to the comment, I did not have internet access. But yes, it does terminate by the '\n' –  Jan 28 '19 at 07:47
  • The following topic seems to be an issue that is next to yours : https://stackoverflow.com/questions/33841741/c-sharp-and-networkstream-sending-is-ok-but-receiving-is-not-working/33847599#33847599 – dvxwifiscan Jan 28 '19 at 09:49

2 Answers2

0

Also check if Windows Firewall does not block the communication.

  • 1
    I have set the firewall to allow the port (6000) to go through, yet the server is not picking the value up. –  Jan 28 '19 at 09:59
0

Try to read char by char because reading line by line can block. Check that : BufferedReader, detecting if there is text left to read

KotlinIsland
  • 799
  • 1
  • 6
  • 25
  • 1
    I added `while(br.ready())` under `br = new BufferedReader(isr);` and then I used readLine, yet no luck, still not sending data, I am running the app on my android device. –  Jan 28 '19 at 10:27
  • Also please can you tell what is the exception shown by ioe.printStackTrace(); ? – KotlinIsland Jan 28 '19 at 10:43
  • 1
    I managed to get it working by using a hotspot from my phone. I was connected to wifi hotspot which had blocked certain ports from their servers. It was a simple issue, but that fixed my problem. Thank you all for the advice and help. –  Jan 29 '19 at 09:05
  • Ok! You are welcome. So this was a kind of a firewall issue. Nice that you resolved it ! – KotlinIsland Jan 29 '19 at 11:18