java網絡編程之UDP和TCP的


TCPUDP的區別:
二者都是有用的和常用的,如果純粹從概念上區分二者就比較費解了,我們直接從功能上進行區分,簡單明瞭:
這兩種傳輸協議也就是合於適配不同的業務和不同的硬件終端。
在使用中,類似於圖像、聲音等對可靠性要求沒有那麼高的業務可以用UDP,他們不需要準確存儲對準確性無要求但要求速度快。
類似於文本、程序、文件等要求可靠的數據最好就用TCP,但會犧牲一些速度。
對系統資源的要求:CP較多,UDP少。
程序結構:UDP程序結構較簡單,TCP複雜。
流模式與數據報模式:TCP保證數據正確性,UDP可能丟包; TCP保證數據順序,UDP不保證

UDP:用戶數據報協議:無連接的通訊協議,它不保證可靠的數據傳輸,但是能實現向若干目標發送數據的功能.所有的信息都是以數據報的形式發送,不保證客戶端是否能夠成功接收到信息.

 具體的操作步驟:

(1),在發送消息的程序中,創建一個DatagramSocket對象,

(2),把要發送的信息包裝爲DatagramPacket對象,這個對象裏面包含了消息發送的不表地址和端口號,

(3),調用DatagramSocket對象的send方法,發送數據報,

(4),在接收程序端,創建對應的帶有指定的端口號的DatagramSocket對象,端口號需要跟發送時指定的一致,

(5),調用創建的DatagramPacket對象的receive方法 接收發送的消息

(6),使用DatagramSocket對象的getData方法得到發送數據

 

/**

*

*UDP 發送消息的 程序

*/

public class UdpSend {

public static void main(String[] args) {

DatagramSocket ds=null;

DatagramPacket dp=null;

byte[] b=new byte[1024];

try {

//DatagramSocket類完成消息的發送

ds=new DatagramSocket();

/*把要發送的消息,使用DatagramPachet類包裝*/

String str="Hello World!";

dp=new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),3000);

ds.send(dp);//發送數據包

} catch (Exception e) {

e.printStackTrace();

}finally{

ds.close();//消息發送完畢,關閉對象

}

}

 

}

/**

*

*UDP 接收消息的 程序

*/

public class UdpRecv {

/**

 * @param args

 */

public static void main(String[] args) {

DatagramPacket dp=null;

DatagramSocket ds=null;

byte[] b=new byte[1024];

try {

/*發送消息時,指定消息發送的端口號是3000

 * 因此在接收程序中,構建DatagramSocket對象時,端口號要對應一致*/

ds=new DatagramSocket(3000);

dp=new DatagramPacket(b,b.length);

//接收發送來的消息

ds.receive(dp);

String str=new String(dp.getData(),0,dp.getLength());

str+=" 消息來自:"+dp.getAddress().getHostAddress();

System.out.println(str);

} catch (Exception e) {

e.printStackTrace();

}finally{

ds.close();

}

}

 

}

 

 

TCP: 傳輸控制協議,面向連接的通訊協議, 它提供了兩臺計算機之間可靠無差錯的數據傳輸.傳輸數據的方式是輸入輸出流.

 具體的操作步驟:

(1),服務器程序創建ServerSocket對象,調用accept()方法等待客戶端的連接這個方法調用之後服務器端將進入到阻塞狀態,直到客戶端的再次連接它的返回類型是Socket.

(2),客戶端程序創建Socket對象並請求與服務器建立連接.

(3),服務器接收到客戶的連接請求並創建新的Socket對象與客戶端建立專線連接.

(4),建立連接的Socket在同一個線程上對話.

(5),服務器重新等待新的連接請求.

 

/**

*TCP的 服務器端 

*/

public class TcpServer {

public static void main(String[] args) {

ServerSocket serSoc = null;

Socket soc = null;

BufferedReader in = null;// 讀取數據

PrintWriter out = null;// 輸出數據

try {

serSoc = new ServerSocket(9000);// 創建端口爲9000 的服務器

// 接收客戶端的鏈接

soc = serSoc.accept();

// 接收客戶端輸入的信息

in = new BufferedReader(new InputStreamReader(soc.getInputStream()));

// 向客戶端發送消息,true爲自動刷新緩衝區

out = new PrintWriter(soc.getOutputStream(), true);

while (true) {

out.println("我是服務器,鏈接成功。");

System.out.println("客戶端輸出的是:" + in.readLine());

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

in.close();

out.close();

soc.close();

serSoc.close();

} catch (IOException e) {

// TODO 自動生成 catch 

e.printStackTrace();

}

}

}

}

 

/**

*TCP的 客戶端

*/

public class TcpClient {

public static void main(String[] args) {

Socket soc = null;

BufferedReader in = null;// 讀取數據

BufferedReader input = null;

PrintWriter out = null;// 輸出數據

try {

soc=new Socket("127.0.0.1",9000);

while(true){

input=new BufferedReader(new InputStreamReader(System.in));

in=new BufferedReader(new InputStreamReader(soc.getInputStream()));

out=new PrintWriter(soc.getOutputStream(),true);

out.print("客戶端說:"+input.readLine());

System.out.println("服務器說:"+in.readLine());

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

 

我的總結:TCP的server和client之間通信就好比兩個人打電話,需要互相知道對方的電話號碼,然後開始對話。所以在兩者的連接過程中間需要指定端口和地址。

UDP的server和client之間的通信就像兩個人互相發信。我只需要知道對方的地址,然後就發信過去。對方是否收到我不知道,也不需要專門對口令似的來建立連接


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章