您的位置:首页 > 其它

获取百度主页和系统调用

2020-06-06 06:31 25 查看

获取百度主页

exec 8<> /dev/tcp/www.baidu.com/80

知识点:

  • linux系统中一切皆文件
  • 把这个8理解为Java中的引用对象
  • exec 文件重定向
  • 文件描述符
cd /proc/$$/fd

知识点:

  • /proc文件夹
  • $$当前进程ID
  • fd
ll

知识点:

  • 此时已与百度建立socket连接(TCP连接已经有了,三次握手)
  • 什么是三次握手四次分手
  • 怎么连接的?
  • 什么是socket ?socket 通信✨

下一步我要和百度说什么话,百度能给我主页,此时与HTTP有关

发这样的话:

GET / HTTP/1.0\n

那怎样发过去呢?

echo -e 'GET / HTTP/1.0\n' 1>& 8

知识点:

  • -e
  • 标准输入标准输出
  • &

此时回车,咔,内容就发给百度了,那百度就得给我们返回内容,那么返回的东西怎么看呢?

这样看

cat 0<& 8

知识点:

  • cat
  • 标准输入标准输出

Java Socket编程

/**
* @author tengwz
*/
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;
}

}

}

}
/**
* @author tengwz
*/
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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐