java基础_网络编程(udp 聊天)

package com.java.demo;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Chat1 {
    public static void main(String[] args) throws Exception {
        //创建套接字
        DatagramSocket socket = new DatagramSocket(1555);
        while (true) {
            //键盘录入
            Scanner sc = new Scanner(System.in);
            System.out.println("回复:");
            //获取发生的消息
            String message = sc.nextLine();
            //将消息转换为byte数组
            byte[] bytes = message.getBytes();

            //创建发生的报包
           DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 1234);
         //  DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("10.10.92.127"),1234);
            //发送
            socket.send(packet);

            //接收数据
            byte[] bytes1 = new byte[1024];
            DatagramPacket packet1 = new DatagramPacket(bytes1, bytes1.length);
            socket.receive(packet1);
            int port = packet1.getPort();
            InetAddress ip = packet1.getAddress();
            System.out.println(ip+","+port);
            //解析数据
            byte[] data = packet1.getData();
            //获取接收的有效数据
            int len = packet.getLength();
            //输出内容
            String s = new String(data, 0, len);
            System.out.println(s);

            if (s.equals("再见")){
                socket.close();
                break;
            }
        }

    }
}

 

package com.java.demo;

import javax.sound.sampled.Port;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.util.Scanner;

public class Chat2 {
    public static void main(String[] args)throws Exception {
        DatagramSocket socket = new DatagramSocket(1234);
        while (true) {
            //接收数据
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            socket.receive(packet);
            int len = packet.getLength();
            byte[] data = packet.getData();
            //输出
            String s = new String(data, 0, len);
            System.out.println(s);

            //发送
            Scanner sc = new Scanner(System.in);
            System.out.println("回复:");
            String message = sc.nextLine();
            byte[] bytes1 = message.getBytes();
            int port = packet.getPort();
            InetAddress ip = packet.getAddress();
            System.out.println(ip+","+port);
            DatagramPacket packet1 = new DatagramPacket(bytes1, bytes1.length,ip,port);
            socket.send(packet1);
            if (s.equals("再见")){
                socket.close();
                break;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值