JAVA 网络编程

TCP

三次握手四次挥手(三次干架,四次分手)
三次握手(三次干架)
A:你瞅啥
B:瞅你咋地
A:干他
四次挥手(四次分手)
A:我要走了
B:你真的要走了吗?
B:你真的真的要走了吗?
A:我真的要走了

package TCP;

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

//服务器
public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(6000);
        Socket s = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = null;
        while ((str = br.readLine()) != null){
            System.out.println("服务器接收数据为:" + str);
        }
        //接收状态转为发送状态
        s.shutdownInput();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write("我是小兰");
        //发送之后必须清空缓存区
        bw.flush();
        //关闭资源
        br.close();
        bw.close();
        s.close();
    }
}

package TCP;

import java.io.*;
import java.net.Socket;

//客户端
public class TCPClient{
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("127.0.0.1" ,6000);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write("我是小明");
        //发送之后必须清空缓存区
        bw.flush();
        //发送状态转为接收状态
        s.shutdownOutput();
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = null;
        while ((str = br.readLine()) != null){
            System.out.println("客户端接收数据为:" + str);
        }
        //关闭资源
        bw.close();
        br.close();
        s.close();
    }
}

UDP

package UDP;

import java.io.IOException;
import java.net.*;
//服务端
public class UdpClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9000);
        byte[] buffer = new byte[1024];
        DatagramPacket packetReceive = new DatagramPacket(buffer , 0 , buffer.length);
        socket.receive(packetReceive);
        System.out.println("服务端接收到的数据:" + new String(buffer , 0 , packetReceive.getLength()));
        //接收数据
        String str = "你好 生活";
        InetAddress address = InetAddress.getByName("localhost");
        int port = packetReceive.getPort();;
        DatagramPacket packetSend = new DatagramPacket(str.getBytes() , 0 ,str.getBytes().length , address , port);
        socket.send(packetSend);
        //关闭UDP
        socket.close();
    }
}
package UDP;

import java.io.IOException;
import java.net.*;
//客户端
public class UdpClient2 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9001);
        String str = "你好 世界";
        InetAddress address = InetAddress.getByName("localhost");
        int port = 9000;
        DatagramPacket packetSend = new DatagramPacket(str.getBytes() , 0 ,str.getBytes().length , address , port);
        socket.send(packetSend);
        //发送数据
        byte[] buffer = new byte[1024];
        DatagramPacket packetReceive = new DatagramPacket(buffer , 0 , buffer.length);
        socket.receive(packetReceive);
        System.out.println("客户端接收到的数据:" + new String(buffer , 0 , packetReceive.getLength()));
        //关闭UDP
        socket.close();
    }
}

UDP聊天

package UDP聊天;

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

public class ThreadReceive extends Thread{
    DatagramSocket socket;
    private int port;
    private String str;

    public ThreadReceive(int port, String str) {
        this.port = port;
        this.str = str;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true){
            try {
                byte[] buffer = new byte[1024];
                DatagramPacket packetReceive = new DatagramPacket(buffer , 0 , buffer.length);
                socket.receive(packetReceive);
                String receiveData = new String(buffer , 0 , packetReceive.getLength());
                System.out.println(str + ":" + receiveData);
                if (receiveData.equals("再见"))
                {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}
package UDP聊天;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

public class ThreadSend extends Thread{
    DatagramSocket socket = null;
    BufferedReader reader = null;
    private final int fromPort;
    private final String toIP;
    private final int toPort;

    public ThreadSend(int fromPort, String toIP, int toPort) {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;
        try {
            socket = new DatagramSocket(fromPort);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true){
            try {
                String data = reader.readLine();
                DatagramPacket packetSend = new DatagramPacket(data.getBytes() , 0 ,data.getBytes().length , InetAddress.getByName(toIP) , this.toPort);
                socket.send(packetSend);
                if (data.equals("再见"))
                {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}
package UDP聊天;

public class Udp_XiaoMing {
    public static void main(String[] args) {
        new Thread(new ThreadSend(7777 , "localhost" , 9999)).start();
        new Thread(new ThreadReceive(8888 , "小明")).start();
    }
}
package UDP聊天;

public class Udp_XiaoHong {
    public static void main(String[] args) {
        new Thread(new ThreadSend(5555 , "localhost" , 8888)).start();
        new Thread(new ThreadReceive(9999 , "小红")).start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值