当前位置: 首页 > news >正文

河北网站建设推广海南七星彩网站建设

河北网站建设推广,海南七星彩网站建设,做盗号网站,wordpress仿qq空间主题什么是网络编程 计算机与计算机之间通过网络进行数据传输 两种软件架构 网络编程3要素 IP IPv4 IPv6 Testpublic void test01() throws UnknownHostException { // InetAddress.getByName 可以是名字或ipInetAddress address InetAddress.getByName(LAPTOP-7I…什么是网络编程 计算机与计算机之间通过网络进行数据传输 两种软件架构 网络编程3要素 IP IPv4 IPv6 Testpublic void test01() throws UnknownHostException { // InetAddress.getByName 可以是名字或ipInetAddress address InetAddress.getByName(LAPTOP-7IJTG2U2);System.out.println(address);String hostName address.getHostName();System.out.println(hostName);String ip address.getHostAddress();System.out.println(ip);} 端口号 协议  OSI参考模型 TCP/IP协议 UDP 发送数据接收数据 代码 /*54321端口发送数据到12345端口从12345端口取出数据*/Testpublic void UDP_Send() throws IOException {//1.创建DatagramSocket对象快递公司//空参绑定随机端口DatagramSocket ds new DatagramSocket(54321);//2.打包数据byte[] data测试UDP.getBytes();InetAddress address InetAddress.getByName(LAPTOP-7IJTG2U2);int port12345;DatagramPacket dp new DatagramPacket(data,data.length,address,12345);//发送数据释放资源ds.send(dp);//向名为“LAPTOP-7IJTG2U2”的计算机的12345端口发送数据ds.close();}Testpublic void UDP_Receive() throws IOException {//创建DatagramSocket对象绑定端口12345因为数据被发送到12345端口DatagramSocket ds new DatagramSocket(12345);InetAddress address InetAddress.getByName(LAPTOP-7IJTG2U2);byte[] bytes new byte[1024];DatagramPacket dp new DatagramPacket(bytes,bytes.length);System.out.println(12345端口等待接收数据中);ds.receive(dp);System.out.println(12345端口接收到数据);byte[] data dp.getData();String s new String(data,0,dp.getLength());System.out.println(从ip:dp.getAddress()的dp.getPort()端口获取到数据:s);} 先运行UDP_Receive会阻塞在ds.receive 再运行UDP_Send发送数据 运行结果 12345端口等待接收数据中 12345端口接收到数据 从ip:/192.168.123.1的54321端口获取到数据:测试UDPProcess finished with exit code 0 UDP案例聊天室  需求 发送聊天室里面的人 public class RoomSend {public static void main(String[] args) throws IOException {DatagramSocket ds new DatagramSocket();Scanner sc new Scanner(System.in);InetAddress address InetAddress.getByName(127.0.0.1);int port12345;while (true){System.out.println(请输入:);String str sc.next();if(886.equals(str)){break;}byte[] bytes str.getBytes();DatagramPacket dp new DatagramPacket(bytes, bytes.length, address, port);ds.send(dp);}ds.close();} } 接收 聊天室 public class RoomReceive {public static void main(String[] args) throws IOException {DatagramSocket ds new DatagramSocket(12345);byte[] bytes new byte[1024];DatagramPacket dp new DatagramPacket(bytes, 0,bytes.length);while (true){ds.receive(dp);System.out.println(dp.getAddress()--dp.getPort()--new String(dp.getData(),0,dp.getLength()));}} } 允许创建多个RoomSend 测试  TCP 发送数据,接收数据 服务端 public class Server {public static void main(String[] args) throws IOException {//1.创建ServerSocket对象ServerSocket ss new ServerSocket(10000);//2.监听客户端的链接没有客户端链接会一直卡在这里有客户端连接返回socket对象Socket socket ss.accept();//3.获取输入流读数据int b;BufferedReader br new BufferedReader(new InputStreamReader(socket.getInputStream()));while ((bbr.read())!-1){System.out.print((char)b);}//4.释放资源br.close();socket.close();//ss.close(); //客户端不关闭} } 客户端 public class Client {public static void main(String[] args) throws IOException {//1.创建socket对象连接不到服务器的端口10000会报错Socket socket new Socket(127.0.0.1, 10000);//2.获取socket对象的输出流输出数据OutputStream os socket.getOutputStream();os.write(我测.getBytes());//3.释放资源os.close();socket.close();} }TCP的三次握手和四次挥手 三次握手 四次挥手 练习 接收并反馈信息 上传文件 服务端  public class Server {public static void main(String[] args) throws IOException {ServerSocket ss new ServerSocket(10000);Socket socket ss.accept();InputStream is socket.getInputStream();BufferedInputStream bis new BufferedInputStream(is);BufferedOutputStream bos new BufferedOutputStream(new FileOutputStream(D:\\java_workspace\\demo3\\src\\main\\java\\com\\example\\upload\\copy.png));int len;byte[] bytes new byte[1024];while ((lenbis.read(bytes))!-1){bos.write(bytes,0,len);}bos.close();bis.close();socket.close();ss.close();System.out.println(传输完成);} } 客户端 public class Client {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream(new File(D:\\java_workspace\\demo3\\src\\main\\java\\com\\example\\upload\\img.png));BufferedInputStream bis new BufferedInputStream(fis);int len;byte[] bytes new byte[1024];Socket socket new Socket(127.0.0.1, 10000);OutputStream os socket.getOutputStream();while ((len fis.read(bytes))!-1){os.write(bytes,0,len);}fis.close();socket.shutdownOutput();os.close();socket.close();} } 上传文件线程版 服务端 public class TCPServer {public static void main(String[] args) throws IOException {ServerSocket ss new ServerSocket(10000);while (true){Socket socket ss.accept();new Thread(new MyRunnable(socket)).start();}} } public class MyRunnable implements Runnable{private Socket socket;public MyRunnable(Socket socket) {this.socket socket;}Overridepublic void run() {try {InputStream is socket.getInputStream();BufferedInputStream bis new BufferedInputStream(is);BufferedOutputStream bos new BufferedOutputStream(new FileOutputStream(D:\\java_workspace\\demo3\\src\\main\\java\\com\\example\\upload\\ UUID.randomUUID().toString().replace(-, ) .png));int len;byte[] bytes new byte[1024];while ((len bis.read(bytes)) ! -1) {bos.write(bytes, 0, len);}bos.close();bis.close();socket.close();System.out.println(传输完成);} catch (IOException e) {e.printStackTrace();}} } 客户端 public class TCPClient {public static void main(String[] args) throws IOException, InterruptedException {FileInputStream fis new FileInputStream(new File(D:\\java_workspace\\demo3\\src\\main\\java\\com\\example\\upload\\img.png));BufferedInputStream bis new BufferedInputStream(fis);int len;byte[] bytes new byte[1024];Socket socket new Socket(127.0.0.1, 10000);OutputStream os socket.getOutputStream();while ((len fis.read(bytes)) ! -1) {os.write(bytes, 0, len);}Thread.sleep(5000);fis.close();socket.shutdownOutput();os.close();socket.close();} } 上传文件线程池版 服务端 public class TCPServer {private static ThreadPoolExecutor myThreadPool new ThreadPoolExecutor(4,//核心线程数17,//最大线程数1,//空闲线程最大存活时间TimeUnit.MINUTES,//时间单位new ArrayBlockingQueue(2),//长度为3的等待队列Executors.defaultThreadFactory(),//创建线程工厂new ThreadPoolExecutor.AbortPolicy()//设置拒绝策略丢弃任务并抛出异常);public static void main(String[] args) throws IOException {ServerSocket ss new ServerSocket(10000);while (true){Socket socket ss.accept();TCPServer.myThreadPool.submit(new MyRunnable(socket)); // new Thread(new MyRunnable(socket)).start();}} } 客户端和MyRunnable同上
http://www.w-s-a.com/news/891779/

相关文章:

  • 青岛网站建设服务器wordpress迁移跳转原网站
  • 泰安网站建设哪里有公司如何注册网站
  • 做网站开专票税钱是多少个点上海市有哪些公司
  • 寿县有做网站开发的吗宁波网站建设方式
  • 网站建设和网站推广服务器怎么发布网站
  • 比较好的摄影网站雅安市政建设公司网站
  • 网站与微信区别wordpress 站内信
  • 宁夏网站开发设计说明书源码下载脚本之家
  • 邱县做网站百度搜索排名机制
  • 运城个人网站建设智慧团建系统官方网站登录
  • 公司营业执照可以做几个网站一家专门做母婴的网站
  • 网站建设商标属于哪个类别搜狗seo快速排名公司
  • 织梦做商城网站企业网络建站
  • 网站后期维护都有什么wordpress首页加图片
  • 展会网站怎么做网页设计与制作教程版徐洪亮课后答案
  • 石景山网站建设设计公司建设网站怎么建立服务器
  • 本地生活服务平台app网站关键词优化原理
  • 建网站的公司叫什么重庆论坛建站模板
  • 湖北网站制作公司银川网站建设哪家不错
  • 网站后台演示地址服装网站建设公司推荐
  • 湖北钟祥建设局网站旅游哪个网站最好
  • 浙江建设工程信息网站辽宁建设工程信息网场内业绩什么意思
  • 郑州做网站公司 汉狮网络专业图片搜集网站怎么做
  • 网站托管是什么品牌推广营销平台
  • 制作网站的难度贵州省兴义市建设局网站
  • 永春建设局网站室内设计师培训班学费多少
  • 做仿站如何获取网站源码windows2012做网站
  • 网站建设最好的公司东莞外贸网站
  • 普兰店网站建设一般做网站什么价格
  • 网站建设的发展目标甘肃网站设计公司