1.TCP和UDP
在java中網路程式有兩種協議:TCP和UDP,TCP通過握手協議進行可靠的串連,UDP則是不可靠串連。
UDP:將資料,資料來源,資料目的封裝組成資料包中,不需要建立串連,不可靠協議,但是速度快,每個資料包大小限制在64k內
TCP:建立串連,形成傳輸資料的通道,在串連中進行大資料量的傳輸,通過三向交握完成串連,可靠,效率稍低。
2.UDP/TCP通訊簡單Demo
UPD通訊叫發送端和接收端
public static void main(String[] args) throws Exception{ //1,建立udp服務。通過DatagramSocket對象。DatagramSocket ds = new DatagramSocket();BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));String line = null;while((line=bufr.readLine())!=null){byte[] buf = line.getBytes();//2,確定資料,並封裝成資料包DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10001);//3,通過socket服務,將已有的資料包發送出去。通過send方法。ds.send(dp);//4,關閉資源。ds.close();}public static void main(String[] args) throws Exception{//1,建立udp socket,建立端點。DatagramSocket ds = new DatagramSocket(10001);//2,定義資料包。用於儲存資料。byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf,buf.length);//3,通過服務的receive方法將收到資料存入資料包中。ds.receive(dp);//阻塞式方法。//4,通過資料包的方法擷取其中的資料。String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength()); //5,關閉資源<span style="white-space:pre"></span>ds.close();}}</span> TCP通訊叫服務端和用戶端。tcp的傳輸的用戶端和服務端的互訪
1, tcp分用戶端和服務端。
2,用戶端對應的對象是Socket。服務端對應的對象是ServerSocket。3, 通過查閱socket對象,發現在該對象建立時,就可以去串連指定主機。4, 因為tcp是連線導向的。所以在建立socket服務時,就要有服務端存在,並串連成功。形成通路後,在該通道進行資料的傳輸。用戶端</span><span style="font-family: KaiTi_GB2312;">步驟:</span><span style="font-family:KaiTi_GB2312;">1,建立socket服務。指定要串連主機和連接埠。2,擷取socket流中的輸出資料流。將資料寫到該流中。通過網路發送給服務端。3,擷取socket流中的輸入資料流,將服務端反饋的資料擷取到,並列印。4,關閉用戶端資源。class TCPClient{public static void main(String[] args) throws Exception {//建立用戶端的socket服務。指定目的主機和連接埠Socket s = new Socket("192.168.1.254",10003); <span style="white-space:pre"></span>//為了發送資料,應該擷取socket流中的輸出資料流。OutputStream out = s.getOutputStream();<span style="white-space:pre"></span>out.write("服務端,你好".getBytes());<span style="white-space:pre"></span><span style="white-space:pre"></span>InputStream in = s.getInputStream();<span style="white-space:pre"></span>byte[] buf = new byte[1024];<span style="white-space:pre"></span>int len = in.read(buf);<span style="white-space:pre"></span>System.out.println(new String(buf,0,len));<span style="white-space:pre"></span>s.close();}}服務端:1,建立服務端的socket服務。ServerSocket();並監聽一個連接埠。2,擷取串連過來的用戶端對象。通過ServerSokcet的 accept方法。沒有串連就會等,所以這個方法阻塞式的。3,用戶端如果發過來資料,那麼服務端要使用對應的用戶端對象,並擷取到該用戶端對象的讀取流來讀取發過來的資料。4,關閉服務端。(可選)class TCPServer{public static void main(String[] args) throws Exception{//建立服務端socket服務。並監聽一個連接埠。ServerSocket ss = new ServerSocket(10003);//通過accept方法擷取串連過來的用戶端對象。Socket s = ss.accept();<span style="white-space:pre"></span>String ip = s.getInetAddress().getHostAddress();<span style="white-space:pre"></span>System.out.println(ip+"....connected");<span style="white-space:pre"></span>InputStream in = s.getInputStream();<span style="white-space:pre"></span>byte[] buf = new byte[1024];<span style="white-space:pre"></span>int len = in.read(buf);<span style="white-space:pre"></span>System.out.println(new String(buf,0,len));<span style="white-space:pre"></span>OutputStream out = s.getOutputStream();<span style="white-space:pre"></span>Thread.sleep(10000);<span style="white-space:pre"></span>out.write("哥們收到,你也好".getBytes());<span style="white-space:pre"></span>s.close();<span style="white-space:pre"></span>ss.close();}}</span>
3.其他Demo
<span style="font-family:KaiTi_GB2312;">import java.io.*;import java.net.*;class TextClient{public static void main(String[] args) throws Exception{Socket s = new Socket("192.168.1.254",10006);BufferedReader bufr = new BufferedReader(new FileReader("IPDemo.java"));PrintWriter out = new PrintWriter(s.getOutputStream(),true);String line = null;while((line=bufr.readLine())!=null){out.println(line);}s.shutdownOutput();//關閉用戶端的輸出資料流。相當於給流中加入一個結束標記-1.BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));String str = bufIn.readLine();System.out.println(str);bufr.close();s.close();}}class TextServer{public static void main(String[] args) throws Exception{ServerSocket ss = new ServerSocket(10006);Socket s = ss.accept();String ip = s.getInetAddress().getHostAddress();System.out.println(ip+"....connected");BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));PrintWriter out = new PrintWriter(new FileWriter("server.txt"),true);String line = null;while((line=bufIn.readLine())!=null){out.println(line);}PrintWriter pw = new PrintWriter(s.getOutputStream(),true);pw.println("上傳成功");out.close();s.close();ss.close();}}</span>