前言:
本文内容:UDP聊天实现、UDP多线程在线咨询、UDP下载网络资源
推荐免费Java网络编程实战讲解视频:【狂神说Java】网络编程实战讲解_哔哩哔哩_bilibili
Java网络编程(NET)笔记代码下载地址:
蓝奏云:下载地址 密码:joker
百度云:下载地址 提取码:jpvx
UDP聊天实现
循环发送消息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.jokerdig.chat;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException;
public class UdpSenderDemo01 { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){ String data=reader.readLine();
byte[] bytes = data.getBytes(); DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",6666));
socket.send(packet); if(data.equals("bye")){ break; } } } }
|
循环接收消息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.jokerdig.chat;
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException;
public class UdpServerDemo01 { public static void main(String[] args) throws IOException{ DatagramSocket socket = new DatagramSocket(6666);
while (true){ byte[] bytes = new byte[1024]; DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length); socket.receive(packet); byte[] data = packet.getData(); String s = new String(data,0, packet.getLength()); System.out.println(s); if(s.equals("bye")){ break; } } socket.close(); } }
|
UDP多线程在线咨询
在线咨询:
两个人都可以是发送方,也都可以是接收方。
发送端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.jokerdig.chat;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException;
public class TalkSeed implements Runnable{
DatagramSocket socket=null; BufferedReader reader=null; private int fromPort; private String toIp; private int toPort; public TalkSeed(int fromPort, String toIp, int toPort) { this.fromPort = fromPort; this.toIp = toIp; this.toPort = toPort; try { socket = new DatagramSocket(fromPort); reader = new BufferedReader(new InputStreamReader(System.in)); } catch (SocketException e) { e.printStackTrace(); } }
@Override public void run() { while(true){ String data= null; try { data = reader.readLine(); byte[] bytes = data.getBytes(); DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toIp,this.toPort)); socket.send(packet);
if(data.equals("bye")){ break; } } catch (IOException e) { e.printStackTrace(); } } } }
|
接收端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.jokerdig.chat;
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException;
public class TalkReceive implements Runnable{ DatagramSocket socket=null; DatagramPacket packet=null; private int port; private String fromMsg;
public TalkReceive(int port,String fromMsg) { this.port = port; this.fromMsg = fromMsg; try { socket=new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } }
@Override public void run() {
while (true){ try { byte[] bytes = new byte[1024]; packet = new DatagramPacket(bytes,0,bytes.length); socket.receive(packet); byte[] data = packet.getData(); String s = new String(data,0, packet.getLength()); System.out.println(fromMsg+"发送:"+s); if(s.equals("bye")){ break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); } }
|
用户:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.jokerdig.chat;
public class TalkUser { public static void main(String[] args) { new Thread(new TalkSeed(7777,"localhost",9999)).start(); new Thread(new TalkReceive(8888,"客户")).start(); } }
|
客户:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.jokerdig.chat;
public class TalkClient { public static void main(String[] args) { new Thread(new TalkSeed(6666,"localhost",8888)).start(); new Thread(new TalkReceive(9999,"用户")).start(); } }
|
UDP下载网络资源
URL:统一资源定位符,定位互联网某一资源。
DNS域名解析:www.jokerdig.com
协议://ip地址:端口/项目名/资源
URL方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.jokerdig.url;
import java.net.MalformedURLException; import java.net.URL;
public class UrlDemo { public static void main(String[] args) throws MalformedURLException { URL url= new URL("https://localhost:8080/helloword/index.jsp?username=name&password=123"); System.out.println(url.getProtocol()); System.out.println(url.getHost()); System.out.println(url.getPort()); System.out.println(url.getPath()); System.out.println(url.getFile()); System.out.println(url.getQuery()); } }
|
URL文件下载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.jokerdig.url;
import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
public class UrlFileDownload { public static void main(String[] args) throws Exception{ URL url = new URL("https://jokerdig.com/images/avatar.png"); HttpURLConnection http=(HttpURLConnection) url.openConnection(); InputStream inputStream = http.getInputStream(); FileOutputStream file = new FileOutputStream("avatar.png");
byte[] buffer = new byte[1024]; int len; while((len=inputStream.read(buffer))!=-1){ file.write(buffer,0,len); } file.close(); inputStream.close(); http.disconnect(); } }
|