服务器程序不发送数据

时间:2014-05-30 12:27:15

标签: java android sockets

我正在继续为我的同事开发Android应用程序。我已经克服了代码中的错误,但出现了一个新问题。服务器程序旨在从文本文件中读取并将数据发送到Android应用程序,该应用程序解释数据并将其组织到列表视图中。 这是代码:

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);

            }   



 }

程序有一些Sytsem.out.println命令用于调试。问题是程序在while循环中停留在&#34;&#34;并停在那里。没有错误,没有logcat,没有。我不确定问题是什么。你能帮助我吗?

谢谢

3 个答案:

答案 0 :(得分:1)

您应该在Out.flush();

之后添加Out.write(input)
When to use flush method and why do we use it?

当需要同步发送

时使用

例如你有一个双工(双向)连接,你刚刚发送了一条消息,现在需要等待它的回复,没有刷新缓冲的输出流可能会保持它,直到缓冲区填满(死锁)

答案 1 :(得分:0)

临时解决方案:我想您已在项目清单中定义了您需要互联网权限。见link

答案 2 :(得分:0)

在您的客户端中使用readLine()。因此服务器应该写行。行以\ n结束。特别发送“结束\ n”。