Java网络编程

网络编程

一、无连接传输协议——UDP
所谓无连接,就像古代邮差送信,发送方必须有,但是接收方有没有不知道,邮差只知道把信送到指定位置,不管有没有人接收

发送方格式:

public static void main(String[] args) throws Exception{
        DatagramSocket ds = new DatagramSocket();
        //将发送的信息转化成字节存放到byte数组中
        byte[] bytes = "UDP".getBytes();
        //获取接收方ip地址
        InetAddress ip = InetAddress.getByName("192.168.0.139");
        //设置端口号,以此为收发凭证
        int port=1210;
        //打包数据
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, ip, port);
        //调用send()方法,发送打包的数据
        ds.send(dp);
        //关闭
        ds.close();
    }

接收方格式:

public static void main(String[] args) throws Exception{
        DatagramSocket ds = new DatagramSocket(1210);//凭端口号监听是否有数据发送来
        //创建数组接收信息
        byte[] bytes = new byte[1024];
        //将接收的信息进行打包
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        //接收打包的信息
        ds.receive(dp);
        //将接收到的数据存放到数组中
        byte[] data = dp.getData();
        //控制台输出信息
        System.out.println(new String(data,0,dp.getLength()));
        //关闭
        ds.close();
    }

二、面向连接传输协议——TCP
开始传输前有三次握手:
1、客户端向服务端发送连接请求
2、服务端确认客户端的请求
3、客户端确认服务端的信息

结束传输有四次挥手:
1、客户端向服务端发送断开连接请求(客户端出于半连接状态)
2、服务端收到客户端的请求后,立即进行确认
3、服务端释放与客户端的连接,并发送剩余数据
4、客户端收到服务端的释放连接后必须确认

客户端格式:

public static void main(String[] args) throws Exception {
        Socket socket = new Socket("192.168.0.139",1210);
        OutputStream os = socket.getOutputStream();
        os.write("TCP".getBytes());
        os.close();
    }

服务端格式:

public static void main(String[] args) throws Exception{
        ServerSocket ss = new ServerSocket(1210);
        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = 0;
        while ((read=is.read(bytes))!=-1){
            System.out.println(new String(bytes,0,read));
        }
        is.close();
        socket.close();
        ss.close();
    }

三、基于TCP协议的简单聊天
发送端:

package Day622.Demo4;


import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception{
        Socket socket = new Socket("192.168.0.139", 1210);
        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();
        Scanner sc = new Scanner(System.in);
        while (true){
            String s1=sc.next();
            os.write(s1.getBytes());
            System.out.println("发送端发送的数据:"+s1);
            if (s1.equals("bye")){
                break;
            }
            byte[] bytes = new byte[1024];
            int read = is.read(bytes);
            String s2 = new String(bytes, 0, read);
            System.out.println("发送端接受的数据:"+s2);
            if (s2.equals("bye")){
                break;
            }
        }
        is.close();
        os.close();
        socket.close();
    }
}

接收端:

package Day622.Demo4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        ServerSocket ss = new ServerSocket(1210);
        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        while (true){
            byte[] bytes = new byte[1024];
            int read = is.read(bytes);
            String s1 = new String(bytes, 0, read);
            System.out.println("接收端接受的数据:"+s1);
            if (s1.equals("bye")){
                break;
            }
            String s2=sc.next();
            os.write(s2.getBytes());
            System.out.println("接收端发送的数据:"+s2);
            if (s2.equals("bye")){
                break;
            }
        }
        is.close();
        os.close();
        socket.close();
        ss.close();
    }
}

你可能感兴趣的:(java)