网络通信笔记12021-7-21

TCP

  • ServerSocket

  • Socket

  • ByteArrayOutputStream

  • InetAddress

  • java.net.InetAddress

    ​ 这个类表示Internet协议地址(IP地址)

客户端:

1、连接服务器socket

2、发送消息

package com.simon.socket1;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

public class Client {
    public static void main(String[] args) {
        //1、要知道服务器地址
        InetAddress serverIP = null;
        Socket socket = null;
        OutputStream os = null;
        {
            try {
                serverIP = InetAddress.getByName("127.0.0.1");
                int port = 9999;
                //2、创建一个socket链接
                socket = new Socket(serverIP, port);
                //3、发送消息IO流
                os = socket.getOutputStream();
                os.write("你好,欢迎学习".getBytes(StandardCharsets.UTF_8));
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

服务器端

1、建立服务端口ServerSocket

2、等待用户的链接accept()

3、接受用户消息

package com.simon.socket1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        //1.我得有一个地址
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        {
            try {
                serverSocket = new ServerSocket(9999);
                //2.等待客户端链接进来
                socket = serverSocket.accept();
                //3.读取客户端的消息
                is = socket.getInputStream();

                //管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while((len = is.read(buffer)) != -1){
                    baos.write(buffer, 0, len);
                }
                System.out.println(baos.toString());

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (baos != null){
                    try {
                        baos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (serverSocket != null){
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }






}

网络通信要素

  • ip地址和端口
  • 协议

UDP(没有明显的服务器端和客户端的概念)

  • DatagramPacket(java.net)
  • DatagramSocket
  • InetAddress

发送端

package com.simon.socket2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class SendClient {
    public static void main(String[] args) throws Exception {
        //1.建立一个Socket
        DatagramSocket socket = new DatagramSocket();
        //2.建个包
        String msg = "去问驱蚊器为";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port = 9090;
        //数据,数据的长度起始,要发给谁
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
        //3.发送包
        socket.send(packet);
        //
        socket.close();
    }
}

接收端

package com.simon.socket2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveClient {
    public static void main(String[] args) throws Exception {
        //开放端口
        DatagramSocket socket = new DatagramSocket(9090);
        //接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
        socket.receive(packet);

        //输出
        System.out.println(new String(packet.getData(),0,packet.getLength()));

        //关闭链接
        socket.close();


    }
}

https:443

http:80

tomcat:8080

  • URL
  • HttpUrlConnection
  • FileOutputStream
URL url = new URL("");

慢慢来,会很快,在那之前,要多想

人生没有白走的路,每一步,都算数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荆北彭于晏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值