網路程式設計—UDP與URL
網路程式設計—UDP
發訊息:不用連線;需要知道對方的地址!
傳送訊息
客戶端
package com.deng.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//不需要連線伺服器
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception{
//1.建立一個Socket
DatagramSocket socket = new DatagramSocket();
String msg = "你好,歡迎光臨";
//傳送給誰
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//資料 資料的長度起始 要傳送給誰
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,localhost,port);
//3.傳送包
socket.send(packet);
socket.close();
}
}
服務端
package com.deng.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception{
//開放埠
DatagramSocket socket= new DatagramSocket(9090);
//接受資料包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);//阻塞接受
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//關閉連線
socket.close();
}
}
諮詢
迴圈傳送訊息
package com.deng.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
//傳送方
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(8888);
//準備資料 控制檯System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
socket.close();
}
}
迴圈接收訊息
package com.deng.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收方
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//準備接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接受包裹
//斷開連線 bye
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData);
if (receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
線上諮詢: 兩個人都可以是傳送方 都可以是接收方
建一個TalkReceive
package com.deng.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;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom =msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
//準備接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接受包裹
//斷開連線 bye
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom +":"+receiveData);
if (receiveData.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
建一個TalkSend
package com.deng.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 TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPor,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() {
try {
while (true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
建一個TalkSend
package com.deng.chat;
public class TalkSend {
public static void main(String[] args) {
//開啟兩個執行緒
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老師")).start();
}
}
建一個TalkTeacher
package com.deng.chat;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"學生")).start();
}
}
URL
統一資源定位符:定位資源的,定位網際網路上的某一個資源
DNS 域名解析 xxx.x..x..x
1.協議: //ip地址:埠/專案名/資源
例題:下載網路資源
package com.deng.lesson04;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//下載網路資源
public class urlDown {
public static void main(String[] args) throws Exception{
//1.下載地址
URL url = new URL("https://s4.music.126.net/style/web2/img/default/default_avatar_40.jpg");
//2.連線到這個資源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("40.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//寫出資料
}
fos.close();
inputStream.close();
urlConnection.disconnect();//斷開連線
}
}