我想在客戶端 - 服務器體系結構中使用Socket發送一個簡單的字節數組。有問題也與Netbeans的調試,因爲它提供了:通過套接字傳輸字節數組
SocketException:連接重置
所以我下面張貼我的代碼,我真的很喜歡,如果有人可以幫助我。
客戶:
public class TestClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 3242);
byte[] b;
b = "Hello".getBytes();
DataOutputStream os = new DataOutputStream(s.getOutputStream());
os.write(b);
} catch (Exception e) {
e.printStackTrace();
}
}
}
服務器:
public class TestServer {
public static void main(String[] args) {
try {
byte[] b = new byte[5];
Socket s = new ServerSocket(3242).accept();
DataInputStream is = new DataInputStream(s.getInputStream());
is.read(b);
System.out.println(String.valueOf(b));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我試圖用InputStream和OutputStream簡單,但行爲是一樣的。
運行上面這些代碼的結果是:
[[email protected]
謝謝您的關注。
問題解決。謝謝。 :) –
npbr,不客氣 –