67,513
社区成员
发帖
与我相关
我的任务
分享
byte[] buf = new byte[1024];
InputStream in = socket.getInputStream();//socket 是已经建立好了的一个Socket连接
int c = in.read(buf);
public class TcpClient {
public static void main(String[] a) {
try {
Socket skt = new Socket(InetAddress.getLocalHost(), 10001);
InputStream in = skt.getInputStream();
OutputStream out = skt.getOutputStream();
byte[] b = new byte[100];
int c = in.read(b);
for (int i = 0; i < c; i++) {
System.out.println(b[i]);
}
if (c > 0) {
byte[] buf = {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 1, 0, 16, 0, 2, 0, 'a', 'b'};
out.write(buf);
out.flush();
}
skt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TcpServer {
public static void main(String[] a) {
try {
ServerSocket sskt = new ServerSocket(10001);
while (true) {
Socket skt = sskt.accept();
OutputStream out = skt.getOutputStream();
byte[] buf = new byte[2];
buf[0] = 0x0D;
buf[1] = 0x0A;
out.write(buf);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TcpServer {
public static void main(String[] a) {
try {
ServerSocket sskt = new ServerSocket(10001);
Socket skt = sskt.accept();
while (true) {
OutputStream out = skt.getOutputStream();
byte[] buf = new byte[2];
buf[0] = 0x0D;
buf[1] = 0x0A;
out.write(buf);
out.flush();
out.close(); // 如果这里关闭了。 就会返回-1;
Thread.sleep(2000);
byte[] buf1 = new byte[2];
buf1[0] = 0x0D;
buf1[1] = 0x0A;
out = skt.getOutputStream();
out.write(buf1);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class TcpClient {
public static void main(String[] a) {
try {
Socket skt = new Socket(InetAddress.getLocalHost(), 10001);
InputStream in = skt.getInputStream();
OutputStream out = skt.getOutputStream();
while (true) {
byte[] b = new byte[100];
int c = in.read(b);
System.out.println(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}