无法从套接字中检索数据

时间:2012-05-04 14:53:51

标签: android

我是开发java应用程序的初学者。我在android上做了一个聊天应用程序。我使用一个线程为进入的客户端提供服务,但是当客户端连接到服务器时,我无法检索套接字中包含的数据,但是当客户端连接丢失时,可以显示数据。我使用ReadLine方法从套接字读取数据。 这是服务器端的程序代码:

package server;
import java.net.*;
import java.io.*;
import java.util.Vector;  

import com.sun.org.apache.bcel.internal.generic.NEW;  

public class Server {       

public static void main(String[] args)throws IOException, InstantiationException,
IllegalAccessException {            

    ServerSocket servsocket = null;  
    Socket sock = null;  
    byte[] bytebuffer = new byte[512];  

    try {
        System.out.println("SERVER IS RUNNING...");
        servsocket = new ServerSocket(28000);
        while(true){
        sock = servsocket.accept();
        System.out.println(servsocket.isBound());
        System.out.println("Port "+servsocket+" Ready!!!");
        System.out.println("Accept connection requests from " + sock);
        System.out.println("From CLIENT "+sock.getInetAddress()+ " and PORT " +  
            sock.getPort());
        ChatThread thread = new ChatThread(sock);  
        System.out.println("Thread is running");
        thread.run();       


        }
    } catch (IOException ioe) {
        System.out.println(ioe);
    } 
    finally{
            try {
                servsocket.close();
                } catch (IOException ioe) {
                    System.out.println(ioe);
                }
            }       
    }
    } 

class ChatThread extends Thread{
    static Vector<ChatThread> chatthread = new Vector<ChatThread>(10);
    private Socket sock;
    private BufferedReader in ;
    private PrintWriter out;

    public ChatThread (Socket socket) throws IOException {
        this.sock = socket;
        in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(
                new OutputStreamWriter(socket.getOutputStream()));

        byte[] bytebuffer = new byte[512];
        int receivemssg;

    }

    public void run(){
        int recvMsgSize;
        byte[] bytebuffer = new byte[512];
        String readsocket;
        try {
            readsocket = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
            }
                }   

程序启动时服务器端的显示屏下方。我试图从客户端发送“Hello ....”这个词。可以看出线程没有运行。

服务器正在运行... 真正 端口ServerSocket [addr = 0.0.0.0 / 0.0.0.0,port = 0,localport = 28000]准备好!!!  接受来自套接字的连接请求[addr = / 172.17.231.254,port = 3567,localport = 28000] 来自CLIENT /172.17.231.254和PORT 3567 线程正在运行......

当我用getInputStream替换线程上的readline方法时,可以从客户端运行该线程并显示消息。这是我输入线程以替换之前使用的readline方法的代码。

public ChatThread (Socket socket) throws IOException {
        this.sock = socket;
        in  = sock.getInputStream();
        out = sock.getOutputStream();
        byte[] bytebuffer = new byte[512];
        int receivemssg;

    }

    public void run(){
        int recvMsgSize;
        byte[] bytebuffer = new byte[512];

        System.out.println("Thread is Running...");
        String masuk = new String(bytebuffer);

        System.out.println(bytebuffer);
        System.out.println(in.toString());
        System.out.println("thread successfully executed !!!");

        synchronized (chatthread) {
            chatthread.addElement(this);
        }

         try {
            while ((recvMsgSize = in.read(bytebuffer)) != -1) {
                    out.write(bytebuffer, 0, recvMsgSize);
                    System.out.println("The length of a character is received and returned "+bytebuffer.length);
                 }
        } catch (IOException e) {

            System.out.println(e);
        }
                    }
                    }

但是接下来的问题是我无法在出现的字符串/文本中显示套接字的内容如下:

Port ServerSocket [addr = 0.0.0.0 / 0.0.0.0,port = 0,localport = 28000] Siap !!!  接受来自套接字的连接请求[addr = / 172.17.231.254,port = 3577,localport = 28000] 来自CLIENT /172.17.231.254和PORT 3577 线程正在运行...... [B @ 7c6768 java.net.SocketInputStream@1690726 线程成功执行!!! 收到一个字符的长度并返回512

请帮助我,谢谢:) GBU伙计们......

1 个答案:

答案 0 :(得分:1)

参见开发人员文档

  

public final String readLine()      自:API Level 1

     

返回一个字符串,其中包含此流中可用的下一行文本。      一行由零个或多个字符组成,后跟'\ n','\ r',“\ r \ n”      或者流的结尾。该字符串不包含换行符序列。

readLine()将阻塞并且不会返回,直到它看到诸如换行符之类的行结束条件,或者到达流的末尾,这可能是连接丢失时发生的情况。

如果你想使用readLine(),你需要发送“Hello .... \ n”或以其他方式为readLine()添加一个终止字符来查看。