1. UDP concept:

      UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768是UDP的正式规范。UDP在IP报文的协议号是17。(from baike)

  2. UDP program:

      因爲UDP協議是一種無連接的協議,所以

        (1)、每次發送數據並不需要綁定,只需要使用DatagramPacket()構造好對應的數據包就可以了;

        (2)、沒有像TCP一樣的ServerSocket 與Socket之分,雙方都用DatagramPacket初始化;

         (3)、發送接收用send(數據包),receive(數據包)

  下面上代碼:

  *UDP並無Server與Client之分,但是代碼一個發送一個接受就起了這名字(這並不重要~逃)

  *發送成功之後雙方就建立了一個鏈接(虛),可以互發數據

 import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException; public class UdpServer {
public static void main(String[] args) {
DatagramSocket dgs = null;
try {
dgs=new DatagramSocket(8888);
//receive
byte[] buf=new byte[256];
int length=256;
DatagramPacket dgp=new DatagramPacket(buf, length);
dgs.receive(dgp);
System.out.println("receive from client:"+new String(buf, 0, length)); //send
String str="yeah i have receivered~";
System.out.println(dgp.getAddress());//use last time's packet to get the address
System.out.println(dgp.getPort());
DatagramPacket dgp_send=new DatagramPacket(str.getBytes(),str.length(), dgp.getAddress(), dgp.getPort());
dgs.send(dgp_send);
dgs.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
dgs.close();
}
}
}

UdpServer

 import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress; public class UdpClient {
public static void main(String[] args) {
DatagramSocket dgs = null;
try {
dgs=new DatagramSocket();
//send->server
String str="so dirty haha~";
DatagramPacket dgp_send=new DatagramPacket(str.getBytes(),0,str.length(),InetAddress.getByName("localhost"),8888);
dgs.send(dgp_send);
//receive from server
byte[] buf=new byte[256];
DatagramPacket dgp2_receive=new DatagramPacket(buf, 256);
dgs.receive(dgp2_receive);
System.out.println("receive from server:"+new String(buf,0,256));
dgs.close(); } catch (Exception e) {
System.out.println("error:"+e);
}
}
}

UdpClient

p.s.I:Server 的 send 部分使用之前用來接收數據的包dgp來取得發送者的address以及port,然後再用新的DatagramPacket來發送數據

p.s.II:DatagramPacket的搆造函數中,byte[]用來放數據,length用來放長度,程序簡單所以數字亂寫的(ha

  

Java study 1:The note of studying Socket which based UDP的更多相关文章

  1. Java study 2:The note of studying Socket which based TCP

    TCP concept: 传输控制协议(Transmission Control Protocol, TCP)是一种面向连接(连接导向)的.可靠的.基于字节流的运输层(Transport layer) ...

  2. java.net.SocketException: Software caused connection abort: socket write error

    用Java客户端程序访问Java Web服务器时出错: java.net.SocketException: Software caused connection abort: socket write ...

  3. [JAVA] java class 基本定义 Note

    java class 基本定义 Note 1 package abeen.note; 2 import java.util.*; 3 4 5 /* 6 java calss 基本 7 */ 8 pub ...

  4. Java从零开始学四十五(Socket编程基础)

    一.网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可 ...

  5. testNG java.net.SocketException: Software caused connection abort: socket write error

    执行用例报错,提示 java.net.SocketException: Software caused connection abort: socket write error java.net.So ...

  6. Caused by: java.net.SocketException: Software caused connection abort: socket write error

    1.错误描述 [ERROR:]2015-05-06 10:54:18,967 [异常拦截] ClientAbortException: java.net.SocketException: Softwa ...

  7. ClientAbortException: java.net.SocketException: Software caused connection abort: socket write erro

    1.错误描述 ClientAbortException: java.net.SocketException: Software caused connection abort: socket writ ...

  8. 【Java】NO.80.Note.1.Java.1.001-【Java 各种特性概念】

    1.0.0 Summary Tittle:[Java]NO.80.Note.1.Java.1.001-[Java 各种特性概念] Style:Java Series:Java Since:2018-0 ...

  9. 运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接

    运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接 最近在项目中可能要用到socket相关的东西来发送消息,所以初步研究了下socket的TC ...

随机推荐

  1. Qt中暂停线程的执行(利用QMutex,超级简单明了)

    在线程中定义一个信号量: QMutex pause;把run()函数中循环执行的部分用信号量pause锁住: void run() { while(1) { pause.lock(); //循环执行的 ...

  2. FindControl什么时候才会使用ObjectFromHWnd函数呢?——VCL很难调试,加一个日志函数,记录时间

    IsDelphiHandleFindVCLWindowfunction IsVCLControl(Handle: HWND): Boolean;function FindControl(Handle: ...

  3. 【POJ】1035 Spell checker

    字典树. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib ...

  4. libc.so.6 误删后修复

    libc.so.6 误删后修复  libc.so.6 被删除了(libc.so.6只是个链接,真实的lib 文件是 libc-2.15.so) su, sudo,ls, cp, mv 等等一系列命令都 ...

  5. Going Home(最小费用最大流)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16200   Accepted: 8283 Description On a ...

  6. Ubuntu 14.04 dnw配置

    之前写的Ubuntu嵌入式环境搭建没有讲怎么配置dnw下载工具,使用dnw还得用红帽,今天配置好了ubuntu下的dnw,记录一下 首先先下载dnw的源码,这是我上传的提供给大家下载:http://p ...

  7. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  8. zoj 1372

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1372 #include<iostream> #include& ...

  9. wordpress 404 error on all pages!

    You have to enable mod_rewrite in apache itself or you won't be able to have permalinks the way you ...

  10. selenium python presence_of_element_located vs visibility_of_element_located

    背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待元素出现.结果屡屡出问题.元素默认是隐藏的,导致等待过早的就结 ...