0

我是开发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 ....”这个词。可以看出线程没有运行。

服务器正在运行... true Port ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=28000] 就绪!!!接受来自 Socket[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);
        }
                    }
                    }

但下一个问题是我无法在出现的字符串/文本中调出套接字的内容,如下所示:

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

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

4

1 回答 1

1

查看开发者文档

public final String readLine () 自:API 级别 1

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

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

如果你想使用 readLine() 你需要发送 "Hello....\n" 或者附加一个终止字符以便 readLine() 看到。

于 2012-05-04T15:02:25.457 回答