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); } }

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. c++单链表基本功能
  2. ios 中使用https的知识
  3. AX2012 常用表关系(客户地址,联系信息)
  4. WCF - 绑定后续之自定义绑定
  5. 使用GDI绘制文本
  6. 安装setuptools 报错缺少zlib
  7. k64 datasheet学习笔记3---Chip Configuration之Human machine interfaces
  8. Internet spirit
  9. python学习之路05
  10. node.js中process进程的概念和child_process子进程模块的使用
  11. python学习-判断是否是IP地址
  12. 在Ubuntu上更新Chrome到最新的版本
  13. VS Code使用Git管理代码
  14. CSS3实战-文字篇
  15. December 26th 2016 Week 53rd Monday
  16. C# 银行系统
  17. mongodb数据导入导出mongoexport/mongoimport
  18. [Fri, 3 Jul 2015 ~ Tue, 7 Jul 2015] Deep Learning in arxiv
  19. python中文件操作的其他方法
  20. 线程安全-一个VC下多个网络请求

热门文章

  1. 数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络
  2. ASP.NET RemoteAttribute远程验证更新问题
  3. 【转】安卓逆向(一)--Smali基础
  4. 提高Interface Builder高效工作的8个技巧
  5. js为Object对象动态添加属性和值 eval c.k c[k]
  6. HTML 学习笔记 JQuery(盒子操作)
  7. 【题解】[CJOI2019]Cipher
  8. 近年来世界各地ICO的花式骗局盘点
  9. Swift 烧脑体操(三) - 高阶函数
  10. oops信息的分析【转】