获取百度主页
exec 8<> /dev/tcp/www.baidu.com/80
知识点:
- linux系统中一切皆文件
- 把这个8理解为Java中的引用对象
- exec 文件重定向
- 文件描述符
cd /proc/$$/fd
知识点:
ll
知识点:
- 此时已与百度建立socket连接(TCP连接已经有了,三次握手)
- 什么是三次握手四次分手?
- 怎么连接的?
- 什么是socket ?socket 通信✨
下一步我要和百度说什么话,百度能给我主页,此时与HTTP有关
发这样的话:
GET / HTTP/1.0\n
那怎样发过去呢?
echo -e 'GET / HTTP/1.0\n' 1>& 8
知识点:
此时回车,咔,内容就发给百度了,那百度就得给我们返回内容,那么返回的东西怎么看呢?
这样看
cat 0<& 8
知识点:
Java Socket编程
public class TestClient {
public static void main(String[] args) throws Exception {
Socket socket = null;
InputStream is = null;
OutputStream ops = null;
try {
socket = new Socket("www.baidu.com", 80);
is = socket.getInputStream();
ops = socket.getOutputStream();
ops.write("GET / HTTP/1.1\n".getBytes());
ops.write("HOST:www.baidu.com\n".getBytes());
ops.write("\n".getBytes());
int i = is.read();
while (i != -1) {
System.out.print((char) i);
i = is.read();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != is) {
is.close();
is = null;
}
if (null != ops) {
ops.close();
ops = null;
}
if (null != socket) {
socket.close();
socket = null;
}
}
}
}
public class TestServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=null;
Socket socket = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(8080);
while (true) {
socket = serverSocket.accept();
outputStream = socket.getOutputStream();
outputStream.write("HTTP/1.1 200 OK\n".getBytes());
outputStream.write("Content-Type:text/html".getBytes());
outputStream.write("Server:Apache-Coyote/1.1\n".getBytes());
outputStream.write("\n\n".getBytes());
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<html>");
stringBuffer.append("<head><title>title</title></head>");
stringBuffer.append("<body>");
stringBuffer.append("<h3> header</h3>");
stringBuffer.append("<a href='http://www.baidu.com>baidu</a>");
stringBuffer.append("</body>");
stringBuffer.append("</html>");
outputStream.write(stringBuffer.toString().getBytes());
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != outputStream) {
outputStream.close();
outputStream = null;
}
if (null != socket) {
socket.close();
socket = null;
}
}
}
}
tcpdump
yum install tcpdump
tcpdump -nn -i eth0 port 80
监听80端口
知识点:
curl www.baidu.com
作用:,默认80端口,对百度建立tcp三次握手,发送协议返回数据,关闭连接。
研究三次握手四次挥手过程
nc
yum install nc
nc -l 8080
nc localhost 8080
ps -fe | grep nc
- ps用法
- 未建立连接时,服务端为什么会出现一个socket?
- 建立连接后两个socket
strace
yum install strace
追踪程序和内核的系统调用
strace -ff -o out nc -l 8080
strace -ff -o out java Hello
产生文件out.nc程序的进程id号
如果是多线程的,则生成多个文件
举例
man 2 socket
通过观察accept,read,三次握手过程,理解BIO,
知识点:
- C语言
- 系统调用system call
- 操作系统
- BIO
- NIO
BIO
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)