我是开发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 伙计们...