UDP 编程 客服咨询回复

package 网络编程_客户咨询;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class sender {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        do{
        InetAddress ia = null;
        ia = InetAddress.getByName("localhost");
        System.out.println("input a message:");

        String mes = input.next();
        DatagramPacket dp = new DatagramPacket(mes.getBytes(),mes.length(),ia,8806);
        DatagramSocket ds = new DatagramSocket();
        ds.send(dp);
        System.out.println("ok!sending..");
    //-------------接受------
        byte[] buf= new byte[1024];
        DatagramPacket dp1 = new DatagramPacket(buf, 1024);
        DatagramSocket ds1 = new DatagramSocket(8806);
        ds1.receive(dp1);
        String mee = new String(dp1.getData(),0,dp1.getLength());
        System.out.println(mee);
        System.out.println("receive session over!");
        ds.close();
        }while(true);
    }

}
package 网络编程_客户咨询;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class receiver {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        do{
        System.out.println("Session begin:");
        byte[] buf= new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf, 1024);
        DatagramSocket ds = new DatagramSocket(8806);
        ds.receive(dp);
        String mee = new String(dp.getData(),0,dp.getLength());
        System.out.println(mee);
        System.out.println("receive session over!");
//-------------------------发送回复---------------------
        System.out.println("please reply:");
        String reply = input.nextLine();
        InetAddress ia = null;
        ia = InetAddress.getByName("localhost");
        DatagramPacket dpr = new DatagramPacket(reply.getBytes(),reply.length(),ia,8806);
        ds.send(dpr);
        ds.close();
        }while(true);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MrCharles

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

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

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

打赏作者

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

抵扣说明:

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

余额充值