您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java網絡編程怎么發送接收數據”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java網絡編程怎么發送接收數據”吧!
A:創建發送端socket對象;
B:創建數據,并把數據打包;
C:調用socket對象的發送方法發送數據包;
D:釋放資源
package net; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class SendDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(); //B byte[] by = "Hello,UDP".getBytes(); int length = by.length; InetAddress addr = InetAddress.getByName("192.168.1.22"); int port = 10010; DatagramPacket dp = new DatagramPacket(by, length, addr, port); //C ds.send(dp); //D ds.close(); } }
A:創建接收端socket對象;
B:創建一個數據包(接收容器);
C:調用socket對象的接收方法接收數據;
D:解析數據,顯示到控制臺;
E:釋放資源
package net; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class ReceiveDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(10010); //B byte[] by = new byte[1024]; int length = by.length; DatagramPacket dp = new DatagramPacket(by, length); //C ds.receive(dp); //D //獲取對方ip InetAddress addr = dp.getAddress(); String ip = addr.getHostAddress(); byte[] by2 = dp.getData(); int len = by2.length; String s = new String(by2, 0, len); System.out.println(ip+"發送的數據是:"+s); //E ds.close(); } }
先運行接收端代碼,再運行發送端代碼。
package net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class SendDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(); //數據來自鍵盤錄入 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = br.readLine()) != null){ //當輸入jieshu時,結束 if("jieshu".equals(line)){ break; } //B byte[] by = line.getBytes(); int length = by.length; InetAddress addr = InetAddress.getByName("192.168.1.22"); int port = 10010; DatagramPacket dp = new DatagramPacket(by, length, addr, port); //C ds.send(dp); } //D ds.close(); } }
package net; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class ReceiveDemo { public static void main(String[] args) throws IOException { //A DatagramSocket ds = new DatagramSocket(10010); //多次接受版本 while(true){ //B byte[] by = new byte[1024]; int length = by.length; DatagramPacket dp = new DatagramPacket(by, length); //C ds.receive(dp); //D //獲取對方ip InetAddress addr = dp.getAddress(); String ip = addr.getHostAddress(); byte[] by2 = dp.getData(); int len = by2.length; String s = new String(by2, 0, len); System.out.println(ip+"發送的數據是:"+s); } //E //ds.close(); } }
感謝各位的閱讀,以上就是“Java網絡編程怎么發送接收數據”的內容了,經過本文的學習后,相信大家對Java網絡編程怎么發送接收數據這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。