您好,登錄后才能下訂單哦!
TCP實現(xiàn)
TCP協(xié)議需要在雙方之間建立連接,通過輸入輸出流來進(jìn)行數(shù)據(jù)的交換,建立需要通過三次握手,斷開需要四次揮手,保證了數(shù)據(jù)的完整性,但傳輸效率也會相應(yīng)的降低。
簡單的TCP實現(xiàn)
//服務(wù)端 public class TcpServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8886); // 建立服務(wù)端,ip為本機ip,端口為8886 Socket accept = serverSocket.accept(); // 監(jiān)聽客戶端的連接,一旦有客戶端連接,則會返回客戶端對應(yīng)的accept InputStream in = accept.getInputStream(); //獲取到客戶端的輸出流 byte b[] = new byte[1024]; int len = in.read(b); System.out.println("接受到客戶端數(shù)據(jù),返回數(shù)據(jù)"+new String(b,0,len)); OutputStream out = accept.getOutputStream(); // 給客戶端發(fā)送消息 out.write("服務(wù)端已經(jīng)接受".getBytes()); serverSocket.close(); } } // 客戶端 public class TcpClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 8886); // 通過Socket來建立和服務(wù)端的連接 OutputStream out = socket.getOutputStream(); // 獲取輸出流(客戶端輸出流即向服務(wù)端輸出信息) out.write("hello tcp Server".getBytes()); // 輸出信息 InputStream in = socket.getInputStream(); // 接受服務(wù)端的消息 byte b[] = new byte[1024]; int len = in.read(b); System.out.println("接受到服務(wù)器消息 : "+new String(b,0,len)); // 輸出 out.write("返回的的數(shù)據(jù)已經(jīng)收到 ".getBytes()); // 向服務(wù)器返回消息 socket.close(); } }
改進(jìn)服務(wù)端,啟用多線程來接受客戶端的數(shù)據(jù)
// server public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8886); // 建立服務(wù)端,ip為本機ip,端口為8886 int i=4; while(i>2){ Socket accept = serverSocket.accept(); // 監(jiān)聽客戶端的連接,一旦有客戶端連接,則會返回客戶端對應(yīng)的accept ServerThread st = new ServerThread(accept); // 啟動線程 Thread th = new Thread(st); th.start(); } serverSocket.close(); } // thread public class ServerThread implements Runnable { private Socket accept; public ServerThread(Socket s) { this.accept = s; } public void run(){ InputStream in; try { in = accept.getInputStream(); byte b[] = new byte[1024]; int len = in.read(b); System.out.println("接受到客戶端數(shù)據(jù),返回數(shù)據(jù)" + new String(b, 0, len)); OutputStream out = accept.getOutputStream(); // 給客戶端發(fā)送消息 out.write("服務(wù)端已經(jīng)接受".getBytes()); } catch (IOException e) { e.printStackTrace(); } } }
傳遞圖片
// 服務(wù)端 public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(5555); Socket ss = serverSocket.accept(); BufferedInputStream br = new BufferedInputStream(ss.getInputStream()); BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream("copy.jpg")); // 寫出文件流 byte b[] = new byte[1024]; int len = 0; while ((len = br.read(b)) != -1) { // 寫出文件 bw.write(b, 0, len); bw.flush(); // 別忘了刷新,要不然最后一塊緩沖區(qū)字符串會缺失 } BufferedWriter bw2 = new BufferedWriter(new OutputStreamWriter(ss.getOutputStream())); // 輸出 bw2.write("圖片傳輸成功"); bw2.flush(); bw.close(); ss.close(); } } // 客戶端 public class Client { public static void main(String[] args) throws UnknownHostException, IOException { Socket socket = new Socket("127.0.0.1", 5555); BufferedInputStream in = new BufferedInputStream(new FileInputStream("c.jpg")); BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream()); byte b[] = new byte[1024]; int len = 0; while ((len = in.read(b)) != -1) { out.write(b, 0, len); out.flush(); // 刷新緩沖區(qū) 要不然最后一塊緩沖區(qū)字符串會缺失 } socket.shutdownOutput(); // 關(guān)閉流以后Server段才會接收道結(jié)束字符結(jié)束接受 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line = br.readLine()) != null) { System.out.println(line); } in.close(); socket.close(); } }
UDP實現(xiàn)
UDP是將數(shù)據(jù)打成數(shù)據(jù)包向?qū)Ψ桨l(fā)送,只關(guān)系是否發(fā)送成功,而不關(guān)心是否接收成功,傳輸速度快,但是可靠性低。
udp代碼實現(xiàn)
// 發(fā)送端 public class SendDemo { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(); // 此類表示用來發(fā)送和接收數(shù)據(jù)報包的套接字。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 鍵盤輸入 String line = null; while ((line = br.readLine()) != null) { byte[] bytes = line.getBytes(); DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"), 10005); // 數(shù)據(jù)包對象 ds.send(dp); if ("886".equals(line)) { // 當(dāng)輸入886時結(jié)束發(fā)送 break; } } ds.close(); } } // 接收端 public class ReciveDemo { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(10005); // 建立服務(wù)端 byte bytes[] = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes, bytes.length); // 建立數(shù)據(jù)包對象 while (true) { ds.receive(dp); // 接受數(shù)據(jù)包 byte[] data = dp.getData(); // 獲取數(shù)據(jù) String str = new String(data, 0, dp.getLength()); if ("886".equals(str)) { break; } System.out.println(str); } ds.close(); } }
以上這篇Java簡單實現(xiàn)UDP和TCP的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。