一、Java网络编程三要素:
1、IP地址:是要确定发送的地址,IP地址一般分为5类。
2、端口:要确定发送的程序是哪一个,端口的范围是0--65535,其中0-1024是系统使用或保留端口
3、协议:发送数据的方式,分为UDP和TCP
二、UDP协议发送接收数据:
1、数据需要打包
2、数据大小有限制,最大64K
3、不需要建立连接
4、速度快
5、是不可靠协议
网络编程为分发送端和接收端。
发送端:
class UdpSendDemo{ public static void main(String[] args) throws IOException { //创建Socket发送端对象 DatagramSocket ds = new DatagramSocket(); //创建数据 byte[] bys = "我来了".getBytes(); //长度 int length = bys.length; //IP地址 InetAddress address= InetAddress.getByName("192.168.10.89"); //端口号 int port = 10086; //数据打包 DatagramPacket dp = new DatagramPacket(bys,length,address,port); //用socket对象发送数据包 ds.send(dp); //关闭资源 ds.close(); } }
根据发送端的代码可总结UDP发送端步骤为:
1、首先建立Socket对象:DatagramSocket
2、将数据进行打包:DatagramPacket(bys,bys.length,InetAddress.getByName("192.168.10.10"),port)
3、Socket对象发送数据
4、关闭Socket对象。
接收端:
public UdpReceiveDemo{ public static void main(String[] args){ //建立接收端Socket对象 DatagramSocket ds = new DatagramSocket(10086); //接收数据首先创建个容器/数据包 byte[] bys = new byte[1024]; //长度 int length = bys.length; //创建数据包 DatagramPacket dp = new DatagramPacket(bys,length); //调用Socket对象接收数据包 ds.receive(dp);//阻塞式 //解析数据包 InetAddress address = dp.getAddress(); int ip = address.getHostAddress(); byte[] bys2= dp.getDate(); int len = dp.getLength(); String s = new String(bys2,0,len); //输出到控制台 System.out.println(ip+"传送的数据是"+s); //释放资源 ds.close(); } }
根据接收端代码可总结UDP接收端的步骤为:
1、建立接收端对象:DatagramSocket(port)
2、创建一个容器:DatagramPacket(bys,bys.lenth)
3、使用接收端对象接收这个容器
4、数据解析 int ip = dp.getAddress().getHostAddress(); String s = new String(dp.getDate(),0,dp.getLength());
5、释放资源
三、UDP协议多线程发送和接收数据
1、多线程实现发送端
public class sendThread implements Runnable{ public DatagramSocket ds; public sendThread (DatagramSocket ds){ this.ds=ds; } //重写run方法 @override public void run(){ try{ //接收键盘录入数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while(line=br.readLine!=null){ if line.equals("886"){ break; } //将数据打包并发送 byte[] bys = line.getBytes(); DatagramSocket ds = new DategramSocket(bys,bye.length,InetAddress.getByName("192.168.10.10",12306)); ds.send(dp); } ds.close(); }catch(IOExpection e){ e.printStackTrace(); } } }
2、多线程实现接收端
public class ReceiveThraed implements Runnable{ public DatagramSocket ds; public ReceiveThraed (DatagramSocker ds){ this.ds=ds; } @override public void run(){ try{ while(true){ //创建容器 byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket (bys,bys.length); //接收数据 ds.receive(dp); //解析数据 int ip = dp.getAddress().getHostAddress(); String s = new String(dp.getDate(),0,dp.getLength()); }catch(IOException e){ e.printStackTrace(); } } } }
3、多线程实现
DatagramSocket dsSend = new DatagramSocket(); DatagramSocket dsReceive = new DatagramSocket(12306); SendThread st = new SendThread(dsSend); ReceiveThread rt = new ReceiveThread(dsReceive); Thread t1 = new Thread(st); Thread t2 = new Thread(rt); t1.start(); t2.start();
四、TCP接收发送数据
1、建立连接
2、支持大数据传输
3、安全可靠
发送端:
public class SendDemo{ public static void main(String[] args){ //建立Socket对象 Socket s = new Socket("192.168.10.10",8888); //获取输出流写数据 OutputStream os =s.getOutputStream(); os.write('"我来了".getBytes()); //释放资源 s.close(); } }
接收端:
public class ReceiveDemo{ public static void main(String[] args){ //创建接收端的Socket对象 ServerSocket ss = new ServerSocket(8888); //监听客户端连接。返回一个对应的Socket对象 Socket s = ss.accept(); //获取输入流 InputStream is = s.getInputStream(); byte[] bys = new byte[1024]; int len = is.read(bys); String s = new String(bys,0,len); String ip = s.getInetAddress().getHostAddress(); s.close(); } }