user36728
user36728

Reputation: 11

Server program not sending data

I'm continuing work on an android app for my coworkers. I've gotten past the errors in my code, but a new problem has arisen. The server program is meant to read from a text file and send the data to an android app, which interprets the data and organizes it into a listview. This is the code:

Bserver:

package beaconserver;

import java.io.*;
import java.net.*;

public class Bserver {

 static ServerSocket Sock = null;
 static Socket server = null;

    public static void main(String[] args) {

        BufferedReader in = null;
        PrintWriter Out = null;
        String ok;
        try{
            System.out.println("Waiting for connection");
            while(true){
            Sock = new ServerSocket(8006);
            server = Sock.accept();
                in = new BufferedReader(new InputStreamReader(server.getInputStream()));
                Out = new PrintWriter(server.getOutputStream());
                String input = null;
                Bfile file = new Bfile();
                String[] companies = file.get();

                    System.out.println("starting send");
                    while(true){
                        System.out.println("in the while loop");
                        ok=in.readLine();
                        Bprotocol protocol = new Bprotocol(); 
                        input = protocol.sendMessage(ok, companies );

                        Out.write(input);
                        if(input.equals("end")){
                            break;
                        }
                    }       
                System.out.println("out of the while loop");
                    Out.close();
                    in.close();
                    server.close();
                    Sock.close();
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Bprotocol:

public class Bprotocol {
    static String output = null;
    static int count = 0;

    public String sendMessage(String ok, String[] file){

        if(ok!=null && count<file.length){
        System.out.println(count+1 + "of" + file.length);
        output = file[count];
        count++;
        }else if(ok==null && count<file.length){
            output=null;
        }else if (count==file.length){
            output = "end";
        }
        return output;

    }
}

Bfile:

package beaconserver;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Bfile {
    static String[] filenames;
    static BufferedReader Br = null;
    static String names = null;
    static ArrayList<String> Names = new ArrayList<String>();
    public String[] get(){
        try {
            Br = new BufferedReader(new FileReader("/home/kyle/Documents/names.txt"));
        } catch (FileNotFoundException e) {
            System.out.println("file not found");
            e.printStackTrace();
        }

        try {
            while(Br.ready()){
                names = Br.readLine();
                Names.add(names);
            }
            Br.close();
        } catch (IOException e) {
            System.out.println("IOexception");
            e.printStackTrace();
        }
        filenames = new String[Names.size()];
        filenames = Names.toArray(filenames);

        return filenames;
    }


}

MainActivity: android

package com.AWS.beacon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

    TextView response;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        response = (TextView) this.findViewById(R.id.textView2);

        new GetNames(this).execute();

            }
        }

 class GetNames extends AsyncTask<Void,String,Void>{


            ArrayList<String> names = new ArrayList<String>();
            PrintWriter out = null;
            BufferedReader in = null;
                Socket sock = null;
                int L = 0;
            String  T = null;
            static String[] companies = null;
            String fromserver;

            public MainActivity GetNamescontext =null;

            public GetNames (MainActivity context){
                GetNamescontext = context;
            }

            @Override
            protected void onPreExecute(){
                super.onPreExecute();
        TextView prog = (TextView) GetNamescontext.findViewById(R.id.textView2);
        prog.setText("Connecting");
            }

    @Override
    protected Void doInBackground(Void... params) {

                try {
                    sock = new Socket("elvishknight1.noip.me", 8006);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                 out = new PrintWriter(sock.getOutputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            try {


                    out.write("ok");

                while((fromserver=in.readLine())!=null){

                if(fromserver.equals("end")){
                    break;
                }
                T = in.readLine();
                names.add(T);

                }



                out.close();
                in.close();
                sock.close();   

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }


        @Override 
        protected void onPostExecute(Void result){
            super.onPostExecute(result); 
            companies = names.toArray(new String[names.size()]) ;
            Intent i = new Intent(GetNamescontext, MainScreen.class);
            GetNamescontext.startActivity(i);

            }   



 }

the program has some Sytsem.out.println commands for debugging. the problem is the program gets stuck on "in the while loop" and stops there. No errors, no logcat, nothing. I'm not sure what the problem could be. Could you guys help me out on this.

Thanks

Upvotes: 0

Views: 78

Answers (3)

Melih Altıntaş
Melih Altıntaş

Reputation: 2535

You should add Out.flush() after Out.write(input);

When to use flush method and why do we use it?

this is used when there needs to be synchronous sending

for example you have a duplex (2-way) connection and you just sent a message and now need to wait for the reply on it, without flush a buffered outputstream might hold it until the buffer fills up (deadlock)

Upvotes: 1

greenapps
greenapps

Reputation: 11214

In your client you use readLine(). Therefore the server should write lines. Lines end with a \n. Especially send "end\n".

Upvotes: 0

AntJavaDev
AntJavaDev

Reputation: 1262

Temp Solution : i suppose you have defined in project's manifest that you need the internet permission. See this link

Upvotes: 0

Related Questions