2012-05-04 22 views
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 ....”一词。可以看出线程没有运行。

服务器正在运行... 真正 端口的ServerSocket [地址= 0.0.0.0/0.0.0.0,端口= 0,将localPort = 28000]就绪! 接受连接请求fromSocket [ADDR =/172.17.231.254,端口= 3567,将localPort = 28000] 从客户/172.17.231.254和PORT 3567 线程运行...

当我替换readline方法上的使用getInputStream线程可以从客户端运行线程并显示消息。这是我进入线程来替换我以前使用的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 [地址= 0.0.0.0/0.0.0.0,端口= 0,localport = 28000] Siap !!! 接受连接请求fromSocket [ADDR =/172.17.231.254,端口= 3577,将localPort = 28000] 从客户/172.17.231.254和PORT 3577 线程运行... [B @ 7c6768 java.net.SocketInputStream @ 1690726 线程成功执行!!! 字符的长度接收并返回512

请帮助我,谢谢:) GBU家伙...

回答

1

查看开发docmentation

公开的最终字符串的readLine() 因为: API级别1

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

的readLine()将阻塞和不会返回,直到其看到结束线的条件,例如一个新行字符,或者该流的结束时达到,这可能是当连接丢失会发生什么。

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

+0

感谢您的回复,非常有帮助:) –