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之间的通信就像两个人互相发信。我只需要知道对方的地址,然后就发信过去。对方是否收到我不知道,也不需要专门对口令似的来建立连接


 

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