【发布时间】:2016-05-30 16:40:11
【问题描述】:
我正在尝试在客户端-服务器架构中使用 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,但行为是相同的。
运行上面这些代码的结果是:
[B@25154f
感谢您的关注。
【问题讨论】: