网络编程入门

网络编程入门

网络通信的两个要素

  • IP
  • 端口号

协议

  1. TCP:用户传输协议,类似于打电话,需要双方同时存在,才能连接。
  2. UDP:用户数据报协议,类似于发短信,只需发送短信,无法监控另一方是否收到短信。

TCP协议的简单实现

package com.edagr.lesson02;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//TCP实现聊天 客户端
public class TestClientSocket {

    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress serverIP = InetAddress.getByName("localhost");
            //创建连接
            socket = new Socket(serverIP, 9999);
            //输出信息
            os = socket.getOutputStream();
            os.write("edgar学网络编程".getBytes());
            os.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if(socket!=null){
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
package com.edagr.lesson02;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//TCP实现聊天 服务端
public class TestServerSocket {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //创建服务端口号
            serverSocket = new ServerSocket(9999);
            //等待连接
            socket = serverSocket.accept();
            is = socket.getInputStream();

            //输出信息
            //管道流
            baos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (is != null) {
                    is.close();
                }
                if (socket != null) {
                    socket.close();
                }
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

UDP协议的简单实现

package com.edagr.lesson03;

import java.io.IOException;
import java.net.*;
//客户端
public class UdpClientDemo01 {

    public static void main(String[] args) {
        try {
            //1.建立一个Socket
            DatagramSocket socket = new DatagramSocket();

            //2.建个包
            String msg = "你好啊,服务器!!";
            InetAddress localHost = InetAddress.getLocalHost();
            int port = 9090;

            DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localHost, port);

            //3.发送包
            socket.send(packet);

            //4.关闭流
            socket.close();

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
package com.edagr.lesson03;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//服务端
public class UdpServerDemo01 {

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

            //阻塞接受
            socket.receive(packet);
            System.out.println(packet.getAddress().getHostAddress());
            System.out.println(new String(packet.getData()));

            //关闭流
            socket.close();


        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
posted @ 2021-04-12 22:48  EdgarStudy  阅读(112)  评论(0编辑  收藏  举报