第四周第一天(【URL(openStream()】【基于UDP的网络通信】)
来源:互联网 发布:淘宝的瑕疵大牌化妆品 编辑:程序博客网 时间:2024/05/22 16:04
基于UDP的网络通信
package com.day1_2015_8_3;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Scanner;public class MySendTest { public static void main(String[] args) { try { DatagramSocket send = new DatagramSocket(); // DatagramSocket此类表示用来发送和接收数据报包的套接字。 DatagramSocket receive = new DatagramSocket(8088); Scanner scanner = new Scanner(System.in); byte[] ReceiveArray = new byte[1024]; while (true) { Calendar rightNow =Calendar.getInstance(); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String time = format.format(rightNow.getTime()); System.out.print(time+"\n小明说:"); String s = scanner.next(); byte[] array = s.getBytes(); DatagramPacket dp = new DatagramPacket(array, array.length, InetAddress.getLocalHost(), 8080); // DatagramPacket此类表示数据报包。 // public DatagramPacket(byte[] buf,int length,InetAddress // address,int port)构造数据报包, // 用来将长度为 length 的包发送到指定主机上的指定端口号。length 参数必须小于等于 buf.length。 send.send(dp); DatagramPacket packetRecive = new DatagramPacket(ReceiveArray, 1024, InetAddress.getLocalHost(), 8080); receive.receive(packetRecive); byte[] data = packetRecive.getData(); System.out.println(time +"\n小红说:" + new String(data, 0, packetRecive.getLength())); } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}//******************************/package com.day1_2015_8_3;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Scanner;public class MyReceiveTest { public static void main(String[] args) { try { DatagramSocket receive = new DatagramSocket(8080); DatagramSocket send = new DatagramSocket(); Scanner scanner = new Scanner(System.in); byte[] array = new byte[1024]; while (true) { Calendar rightNow =Calendar.getInstance(); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String time = format.format(rightNow.getTime()); DatagramPacket dp = new DatagramPacket(array, 1024,InetAddress.getLocalHost(), 8088); receive.receive(dp); byte[] data = dp.getData(); System.out.println(time+"\n小明说:"+ new String(data, 0, dp.getLength())); System.out.print(time+"\n小红说:"); String s = scanner.next(); byte[] sendByte = s.getBytes(); DatagramPacket ps = new DatagramPacket (sendByte, sendByte.length, InetAddress.getLocalHost(), 8088); send.send(ps); } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
URL(openStream()网址)
//*****************URL(openStream())*********************/package com.day1_2015_8_3;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;public class TestURL { public static void main(String[] args) { try { URL url = new URL("http://www.baidu.com"); InputStream is = url.openStream(); BufferedReader br= new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while(line!=null){ System.out.println(line); line = br.readLine(); } br.close(); is.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}//***************URL(openStream()图片)*********************/package com.day1_2015_8_3;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;public class TestURL2 {public static void main(String[] args) { try { URL url = new URL("http://pic17.nipic.com/20111026/2487606_163354654187_2.jpg"); File file = new File("d:\\11.jpg"); if(!file.exists()){//file.exists()判断文件是否存在 file.createNewFile();//不存在创建一个新的文件 } OutputStream os = new FileOutputStream(file); InputStream is = url.openStream(); byte[] array = new byte[1024]; int i = is.read(array); while(i!=-1){ os.write(array,0,i); i=is.read(array); } os.flush(); os.close(); is.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}
0 0
- 第四周第一天(【URL(openStream()】【基于UDP的网络通信】)
- 第四周第一天java网络编程
- 第四周第一天
- 第四周第一天
- Java学习之路0803(URL、基于UDP的数据包网络通信)
- 第十四周第一天
- 第十四周 第一天(做项目)
- 线程与网络编程(第五节:UDP的扩展与双工通信)
- 基于UDP的多线程网络通信程序
- java 基于UDP的Socket网络通信
- 网络第一天
- ccna-security学习第一天(基于privilege的实验)
- 基于J2EE的Ajax学习第一天
- RDP(Reliable--UDP)Library 一个基于UDP/IP协议的可靠网络通信库
- 物联网通信第一天
- 自己动手写网络爬虫(第一天)
- 基于TCP/UDP的Socket网络通信系列之网络基础篇(一)
- 第四周的作业
- 支付宝
- ORACLE 实用语句大集合
- ViewPager,java.lang.UnsupportedOperationException: Required method destroyItem was not overridden
- AndroidTV/机顶盒 ListView获取焦点与点击事件问题处理方案
- JavaScript 与 Objective-C 通信
- 第四周第一天(【URL(openStream()】【基于UDP的网络通信】)
- 【Android应用开发技术:媒体开发】拍照
- Redis Java连接操作(转)
- LeetCode_3Longest Substring Without Repeating Characters
- CentOS6.5中桌面上的面板被误删后,如何正常恢复?
- JavaScript 的keydown,keypress,keyup事件
- ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting B
- 数据库之Oracle的使用【入门版,包括常用的Sql语句】
- Basic