日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA--网络编程

發布時間:2025/3/19 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA--网络编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JAVA— 網絡編程

  • U D P 多線程在線咨詢
  • URL下載網上資源

通過URL下載網上資源下載

端口

TCP實現聊天

U D P 多線程在線咨詢

端口

端口表示計算機上一個程序的進程

  • 不同的進程有不同的端口號 用來區分軟件

  • 被規定 0~65535

  • 端口分類
    (1)公認端口(Well Known Ports)

(2)注冊端口(Registered Ports)

(3)動態和/或私有端口(Dyanmic and /or Private Ports)

  • 查看所有的端口

  • netstat -ano #查看所有的端口 netstat -ano findstr "5900" # 查看指定的端口
import java.net.InetAddress; import java.net.UnknownHostException;public class TestIntAddress {public static void main(String[] args) {try {// 查詢本機地址InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");System.out.println(inetAddress1);// 查詢百度的 主機地址InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");System.out.println(inetAddress2);System.out.println(inetAddress2.getAddress());// 規范的名字System.out.println(inetAddress2.getCanonicalHostName());//查詢 ipSystem.out.println(inetAddress2.getHostAddress());//查詢域名System.out.println(inetAddress2.getHostName());} catch (UnknownHostException e) {e.printStackTrace();}} }

TCP實現聊天


客戶端:

  • 連接服務器 Socket
  • 發送消息
  • import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException;// 客戶端 public class TcpClientDemo01 {public static void main(String[] args) {OutputStream os =null;Socket socket = null;try {// 知道 服務器地址 和端口InetAddress serverIp= InetAddress.getByName("127.0.0.1");int port = 9999;// 創建一個socket 連接socket = new Socket(serverIp,port);// 3 發送消息 IO 流os = socket.getOutputStream();os.write("你好啊,大哥哥,每一天都要開開心心哦 六一兒童節禮物還沒有給你!".getBytes());} catch (Exception e) {e.printStackTrace();}finally {if(os != null){try {os.close();} catch (IOException e) {e.printStackTrace();}}if(socket != null){try {socket.close();} catch (IOException e) {e.printStackTrace();}}}} }

    服務器

  • 建立服務的端口 ServerSocket
  • 等待用戶的鏈接 accept
  • 接收用戶的消息
  • import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;// 服務器端 public class TcpServerDemo01 {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;ByteArrayOutputStream baos = null;try {// 我得有一個地址serverSocket = new ServerSocket(9999);// 等待客戶端連接過來socket = serverSocket.accept();// 讀取客戶端的消息is = socket.getInputStream();//管道流baos = new ByteArrayOutputStream();byte[] buffer =new byte[1024];int len;while((len = is.read(buffer)) != -1){baos.write(buffer,0,len);}System.out.println(baos.toString());} catch (IOException e) {e.printStackTrace();}} }

    UDP多線程在線咨詢

    // 第一個類 Send 發送 import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress;public class TalkSend implements Runnable{DatagramSocket socket = null;BufferedReader reader = null;private int fromIP;private String toIP;private int toPort;public TalkSend(int fromIP, String toIP, int toPort) {this.fromIP = fromIP;this.toIP = toIP;this.toPort = toPort;try {socket = new DatagramSocket(fromIP);reader = new BufferedReader(new InputStreamReader(System.in));}catch (Exception e){e.printStackTrace();}}@Overridepublic void run() {while(true){try {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 (Exception e){e.printStackTrace();}}socket.close();}}
    // 第二個類 Receive 接收 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();}}@Overridepublic void run() {while(true){try {// 準備接收 包裹byte[] container = new byte[1024];DatagramPacket packet = new DatagramPacket(container,0,container.length); // socket.receive(packet);socket.receive(packet);// 阻塞時接收包裹// 斷開連接 byebyte[] data = packet.getData();String receiveDate = new String (data, 0 ,data.length);System.out.println(msgFrom+":"+receiveDate);if(receiveDate.equals("bye")){break;}}catch (IOException e){e.printStackTrace();}}socket.close();} }

    測試類 學生 和 老師

  • public class TalkStudent {public static void main(String[] args) {// 開啟兩個線程new Thread(new TalkSend(7777,"localhost",9999)).start();new Thread(new TalkReceive(8888,"老師")).start();} }
  • public class TalkTeacher {public static void main(String[] args) {new Thread(new TalkSend(5555,"localhost",8888)).start();new Thread(new TalkReceive(9999,"學生")).start();} }
  • 效果圖

  • 不知道 IDEA 怎么控制臺怎分屏

    URL網上資源下載

    • 本次是以從網上下載網易云音樂為例:
    • 下載的必要要素 必須知道 網上資源的URL 才可以進行下載

    • 首先去瀏覽器上搜索 然后進入瀏覽器控制臺 找到音頻的 URL 然后這個URL 就是我們進行下載的路徑。
    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 {// 下載地址URL url = new URL("http://m10.music.126.net/20200602120835/02638bb77e5403d65fdb86d86f2863d6/yyaac/obj/wonDkMOGw6XDiTHCmMOi/2703362268/aa27/f3dd/abf2/062dde2b67cf0cf1ad5d242fae90f156.m4a");// 這個里面放的地址就是 之前在網上找到的音頻的url // 連接到這個資源 HTTPHttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();InputStream inputStream = urlConnection.getInputStream();FileOutputStream fos = new FileOutputStream("f156.m4a");// 這個f156.m4a “.m4a” 就是文件保存格式 名字隨意是隨意取的 byte[] buffer = new byte[1024];int len;while((len=inputStream.read(buffer)) != -1){fos.write(buffer,0,len);}// 關閉連接 fos.close();inputStream.close();urlConnection.disconnect();} }

    成功就會在idea中出現一個 .m4a 格式的文件 到本地文件夾中 通過音樂軟件打開即可。能播放就是成功的。


    初次寫這樣的博客 不足之處 請見諒。

    總結

    以上是生活随笔為你收集整理的JAVA--网络编程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。