你能來看看在這個?:傳輸Java對象,字節[]
這是我的客戶:
try {
Socket socket = new Socket("127.0.0.1", 3000);
OutputStream out = socket.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(mp3data);
oos.close();
byte[] bytes = baos.toByteArray();
out.write(bytes);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
這是我的服務器:
int port = 3000;
try {
ServerSocket clientConnect = new ServerSocket(port);
System.out.println("SimpleServer running on port" + port);
Socket clientSock = clientConnect.accept();
InputStream is = clientSock.getInputStream();
byte[] buffer = new byte[1024];
int read = is.read(buffer);
ObjectInputStream ois = new ObjectInputStream(is);
MP3[] songs = (MP3[])ois.readObject();
clientSock.close();
// HTML erzeugen
Website site = new Website("index2.html",songs);
} catch (Exception e) {
System.out.println (e);
}
它不工作。我沒有得到任何例外,但網站構造函數沒有被調用。
您已經調試過嗎? – burna
你應該在客戶端的'out.write()'之後''out.flush()'。另外,在服務器端,您定義了一個尺寸僅爲1024字節的緩衝區,我懷疑它能夠接收到您希望接收的內容,除非我誤認爲... – fge
http://codereview.stackexchange.com/是更好的地方問這樣的問題 –