Socket network programming for java learning

Socket network programming for java learning

0x00 Preface

In the development of some tools, the most inseparable may be network programming, such as directory scanner, port scanning, including exp, which actually depend on socket. Take a simple exp for example. It is actually a payload that has been constructed, and then use socket to send network requests, and then use the vulnerability to obtain a shell.

0x01 socket class overview

Socket class: this class implements client socket. Socket refers to the endpoint of communication between two devices.

matters needing attention:

@H_419_13@1. 服务端程序,需要事先启动,等待客户端的连接。
2. 客户端主动连接服务器端,连接成功才能通信。服务端不可以主动连接客户端。

In Java, two classes are provided to implement TCP communication programs:

0x02 socket usage

View construction method:

@H_419_13@Socket(String host,int port) 
    创建一个流套接字并将其连接到指定主机上的指定口号。

Construction method example:

@H_419_13@Socket client = new Socket("127.0.0.1",6666);

Member method:

@H_419_13@getInputStream() 
          返回此套接字的输入流
getOutputStream() 
          返回此套接字的输出流
 void bind(SocketAddress bindpoint) 
          将套接字绑定到本地地址。 
 void close() 
          关闭此套接字。 
 void connect(SocketAddress endpoint) 
          将此套接字连接到服务器。 

0x03 ServerSocket usage

Construction method:

@H_419_13@ServerSocket(int port) 
          创建绑定到特定端口的服务器套接字。

Common methods:

@H_419_13@accept() 
    侦听并接受到此套接字的连接。
bind(SocketAddress endpoint,int backlog) 
    将 ServerSocket 绑定到特定地址(IP 地址和端口号)。 
 void close() 
          关闭此套接字 


0x04 socket code instance

Client code:

@H_419_13@public static void main(String[] args) throws IOException {
        Socket client = new Socket("127.0.0.1",8080);
        OutputStream os = client.getOutputStream();
        os.write("nihao".getBytes());
        os.close();
        client.close();
        

    }

Create a socket object, pass in parameters to the constructor, use getoutputstream to obtain a socket output stream, and then write characters to it. When transmitting characters here, they need to be converted to byte type.

Server:

@H_419_13@ public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        System.out.println("监听中");
        Socket server = ss.accept();
        InputStream is = server.getInputStream();
        byte[] b = new byte[1024];

        int len = is.read(b);
        String msg = new String(b,len);
        System.out.println(msg);
        is.close();
        server.close();
    }
    

Use the ServerSocket class to instantiate an object and accept the socket connection. When connecting, a new socket will be generated. Use the new socket to obtain the socket input stream and read the data passed in by the client for printing.

0x05 end

This article is relatively short. I don't know what case to write for demonstration. Javese's part has also come to an end for the time being and began to step into the java web part.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>