您好,登錄后才能下訂單哦!
這篇文章給大家介紹TCP與UDP編程在java項(xiàng)目中有哪些區(qū)別,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
TCP
客戶端:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class MyClient { public static void main(String[] args) throws Exception{ Socket socket = null; BufferedReader in = null; PrintWriter out = null; BufferedReader input = null; // 請(qǐng)求指定ip和端口號(hào)的服務(wù)器 socket = new Socket("127.0.0.1",3333); while(true){ in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 接收控制臺(tái)的輸入 input = new BufferedReader(new InputStreamReader(System.in)); // out.println("this is client info!"); String info = input.readLine(); out.println(info); String str = in.readLine(); System.out.println("客戶端顯示--》服務(wù)器的信息:" + str); } //in.close(); //out.close(); } }
服務(wù)端:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class MyServices { public static void main(String[] args) throws Exception{ ServerSocket ss = null; Socket socket = null; BufferedReader in = null; PrintWriter out = null; BufferedReader input = null; // 監(jiān)聽(tīng)3333端口 ss = new ServerSocket(3333); // 等待接收客戶端的請(qǐng)求 socket = ss.accept(); while(true){ // 獲取連接對(duì)象的輸入流 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 獲取客戶端的輸入信息 String str = in.readLine(); input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("服務(wù)器顯示-->客戶端輸入數(shù)據(jù):" + str); out = new PrintWriter(socket.getOutputStream(),true); // 將數(shù)據(jù)輸出到客戶端 // out.println("hehe"); String info = input.readLine(); out.println(info); } // in.close(); // out.flush(); // out.close(); } }
UDP
客戶端:
import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPClient { public static void main(String[] args) throws Exception { DatagramSocket ds = null; DatagramPacket dp = null; byte[] buf = new byte[1024]; ds = new DatagramSocket(3333); dp = new DatagramPacket(buf, 1024); // 接收數(shù)據(jù),放入數(shù)據(jù)報(bào) ds.receive(dp); // 從數(shù)據(jù)報(bào)中取出數(shù)據(jù) String info = new String(dp.getData(),0, dp.getLength()); System.out.println("接收到的信息是:" + info); } }
服務(wù)端:
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPServer { public static void main(String[] args) throws Exception { DatagramSocket ds = null; DatagramPacket dp = null; // 構(gòu)建發(fā)送對(duì)象 ds = new DatagramSocket(); // 將數(shù)據(jù)打包-->打成數(shù)據(jù)報(bào) String info = "hello world!"; dp = new DatagramPacket(info.getBytes(), info.length(), InetAddress.getByName("localhost"),3333); // 發(fā)出數(shù)據(jù)報(bào) ds.send(dp); } }
關(guān)于TCP與UDP編程在java項(xiàng)目中有哪些區(qū)別就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。