JAVA study notes 11-network programming

network programming

1. Software structure
C/S structure: client and server structure
B/S structure: browser and server structure
2. Network communication protocol
Application layer: HTTP, FTP, TFTP, SMTP, SNMP, DNS
Transport layer: TCP, UDP
network Layer: ICMP, IGMP, IP, ARP, RARP
Data link layer: Protocol defined by the underlying network
3. Three elements of network programming
Protocol
IP address
Check the local IP address:
ipconfig
Check whether the network is connected:
ping IP address
ping 220.181.57.216
Port number
Integer represented by two bytes, the value range is 0~65535
4.
Overview of TCP communication program Steps for communication
at both ends:
1. The server program needs to be started in advance and waits for the client to connect
2. The client actively connects On the server side, the connection is successful to communicate.
Client: The
java.nei.Socket class indicates that a Socket object is created, a connection request is sent to the server, and the server responds to the request. The two establish a connection to start communication
. Server:
java.net.ServerSocket Create a ServerSocket object, which is equivalent to starting a service and waiting for the client to connect.
Socket class
This class implements client sockets, and sockets refer to the endpoints of communication between two devices
Socket = IP address + port number
Note:
1. If multiple clients interact with the server at the same time, the server must specify which client to interact
with. There is a method on the server to accept the client to obtain the requested client object
2. Multiple client colleagues interact with the server and need to use multiple IO stream objects. The
server does not have an IO stream. The server can obtain the requested client object. Socket
uses the IO stream provided in each client Socket to interact with the client.
The server uses the client's byte input stream to read the data sent by the
client. The server uses the client's byte output stream to write data back to the client.
Summary: The server uses the client's stream to interact with the
client. The client's
construction method
Socket(String host,int) port): Create a stream socket and connect it to the specified port number on the specified
host String host: the name of the server host/server IP address
int port: the port number
of the secondary weapon Member method
OutputStream getOutputStream(): return this set The output stream of
the socket InputStream getInputStream(): returns the input stream of this socket

public class TCPClient {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建一个客户端对象Socket,构造方法中绑定服务器的IP地址与端口号
        Socket socket=new Socket("127.0.0.1",8888);
        //使用Socket对象中的方法getOutputStream()获取网络字节输出流对象
        OutputStream os=socket.getOutputStream();
        //使用OutputStream对象中的方法write给服务器发送数据
        os.write("你好服务器".getBytes());
        //使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
        InputStream is=socket.getInputStream();
        //使用网络字节输入流InputStream对象中的方法read,读取服务器回写的数据
        byte[] bytes=new byte[1024];
        int len=is.read(bytes);
        System.out.println(new String(bytes,0,len));
        //释放资源
        socket.close();
    }
}

Server-side
construction method
ServerSocket(int port): Create a server socket bound to a specific port
Note: The
server must know which client requested the server.
You can use the accept method to obtain the requested client object Socket
member method
Socket accept( ): listen and receive the connection of this socket

public class TCPServer {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建服务器ServerSocket对象和系统要指定的端口号
        ServerSocket server=new ServerSocket(8888);
        //使用ServerSocket对象中的方法accept,获取到请求的客户端对象Socket
        Socket socket=server.accept();
        //使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
        InputStream is=socket.getInputStream();
        //使用网络字节输入流InputStream对象中的方法read,读取客户端发送的数据
        byte[] bytes=new byte[1024];
        int len=is.read(bytes);
        System.out.println(new String(bytes,0,len));
        //使用Socket对象中的方法getOutputStream(),获取网络字节输出流OutputStream对象
        OutputStream os=socket.getOutputStream();
        //使用网络字节输出流OutputStream对象中的方法write,给客户端回写数据
        os.write("收到谢谢".getBytes());
        //释放资源
        socket.close();
        server.close();
    }
}

Upload file case
client

public class TCPClient {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建一个本地字节输入流FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis=new FileInputStream("F:\\JAVA\\File\\picture.jpg");
        //创建一个客户端Socket对象,构造方法中绑定服务器的IP地址和端口号
        Socket socket=new Socket("127.0.0.1",8888);
        //使用Socket中的方法getOutputStream,获取网络字节输出流OutputStream对象
        OutputStream os=socket.getOutputStream();
        //使用本地字节输入流FileInputStream对象中的方法read读取本地文件
        int len=0;
        byte[] bytes=new byte[1024];
        while((len=fis.read(bytes))!=-1){
    
    
            //使用网络字节输出流OutputStream对象中的方法write,把读取到的文件上传到服务器
            os.write(bytes,0,len);
        }
        socket.shutdownOutput();
        //使用Socket中的方法getInputStream,获取网络字节输出流OutputStream对象
        InputStream is=socket.getInputStream();
        while((len=is.read(bytes))!=-1){
    
    
            //使用网络字节输入流InputStream对象中的方法read读取服务器回写的数据
            System.out.println(new String(bytes,0,len));
        }
        //释放资源
        fis.close();
        socket.close();
    }
}

Service-Terminal

public class TCPServer {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建一个服务器ServerSocket对象,和系统要指定的端口号
        ServerSocket server=new ServerSocket(8888);
        //使用ServerSocket对象中的方法accept,获取到请求的客户端Socket对象
        Socket socket=server.accept();
        //使用Socket对象中的getInputStream,获取到网络字节输入流InputStream对象
        InputStream is = socket.getInputStream();
        //判断文件夹是否存在,不存在则创建
        File file=new File("F:\\JAVA\\File\\FileCopy");
        if(!file.exists()){
    
    
            file.mkdirs();
        }
        //创建一个本地字节输出流对象FileOutputStream对象。构造方法中绑定要输出的目的地
        FileOutputStream fos=new FileOutputStream("F:\\JAVA\\File\\FileCopy\\copy.jpg");
        //使用网络字节输入流InputStream对象中的方法read,读取客户端上传的文件
        int len=0;
        byte[] bytes=new byte[1024];
        //使用本地字节输出流FileOutputStream对象中的方法write,把读取到的文件保存到服务器的硬盘上
        while((len=is.read(bytes))!=-1){
    
    
            fos.write(bytes,0,len);
        }
        //使用Socket对象中的方法getOutputStream,获取到网络字节输出流OutputStream对象
        //使用网络字节输出流OutputStream对象中的方法write回写数据
        socket.getOutputStream().write("上传成功".getBytes());
        //释放资源
        fos.close();
        socket.close();
        server.close();
    }
}

Simulate BS server

public class TCPServer {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建一个服务器ServerSocket和系统要绑定的端口号
        ServerSocket server=new ServerSocket(8080);
        //使用accept方法获取到请求的客户端对象
        Socket socket = server.accept();
        //使用Socket对象中的方法getInputStream。获取到网络字节输入流InputStream对象
        InputStream is = socket.getInputStream();
        //使用网络字节输入流InputStream对象中的方法read读取客户端的请求信息
        //把网络字节输入流对象转换为字符输入流对象
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //把客户端请求信息第一行读取出来
        String line=br.readLine();
        //把读取信息进行切割
        String[] arr=line.split(" ");
        //进行截取
        String htmlpath = arr[1].substring(1);
        //创建一个本地字节输入流,构造方法中读取htnl路径
        FileInputStream fis=new FileInputStream(htmlpath);
        //使用Socket中的方法getOutputStream获取网络字节输出流OutputStream对象
        OutputStream os=socket.getOutputStream();
        os.write("HTTP/1.1 200 OK\r\n".getBytes());
        os.write("Content-Type:text/html\r\n".getBytes());
        os.write("\r\n".getBytes());
        //一读一写,把服务器读取的html文件回写到客户端
        int len=0;
        byte[] bytes=new byte[1024];
        while((len=fis.read())!=-1){
    
    
            os.write(bytes,0,len);
        }
        fis.close();
        socket.close();
        server.close();
    }
}

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/107126543