這裏是我的服務器類,我想發送簡單的類'heee',但每次連接到客戶端時,我都會從try catch獲取'用戶未發送'。我究竟做錯了什麼?如何通過java中的套接字發送和接收對象
public class heee{
String me = "hehehe";
}
public static void main(String args[]) throws Exception {
ServerSocket ssock = new ServerSocket(2222);
System.out.println("Listening");
while (true)
{
Socket sock = ssock.accept();
System.out.println("Connected");
new Thread(new MultiThreadedServer(sock)).start();
}
}
@Override
public void run() {
try
{
out = new ObjectOutputStream(csocket.getOutputStream());
heee hope = new heee();
out.writeObject(hope);
System.out.println("debug line");
}catch(Exception ex)
{
System.out.println("users was not sent");
}
這裏我想讀取線程中的對象。
public class ChatServer implements Runnable {
Socket clientSocket = null;
ObjectInputStream in = null;
@Override
public void run()
{
try
{
clientSocket = new Socket ("localhost", 2222);
in = new ObjectInputStream(clientSocket.getInputStream());
heee nope = (heee) in.readObject();
System.out.print(nope.me);
} catch(Exception Ex)
{
}
}
public static void main(String args[]) throws Exception {
new Thread(new ChatServer()).start();
}
public class heee{
String me;
}
}
「我做錯了什麼?」您不打印異常。 – tkausl