我想在客户端 - 服务器体系结构中使用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,不客气 –