一、UDP:无连接通信。
特点:1.传输效率高
2.安全性低,不保证可到的数据传输。
开发流程:
发送端(客户端):
1.建立udp服务
DatagramSocket ds=new DatagramSocket()
2.封装数据包(DatagramPacket )
byte[] buff="udp test" .getBytes();
DatagramPacket dp=new DatagramPacket (buf,buf.length,InetAddress.getByName("192.168.1.22"),10000);
3.发送数据:
ds.send(dp);
4.关闭资源 close
ds.close()
接收端(服务端):
1.定义udp服务
DatagramSocket ds=new DatagramSocket(10000)
2.定义数据包接收数据
byte[] buff=new byte[1024] ;
DatagramPacket dp=new DatagramPacket (buf,buf.length);
3.通过udp服务接收数据包 receive
ds.receive(dp);
4.取出数据
string ip=dp.getAddress().getHostAddress();
int len=dp.getLength();
String data=new String(dp.getData(),len);
5.关闭资源close
ds.close();
服务端:
public class UdpServer extends Thread {
Handler handler;
public UdpServer(Handler handler){
this.handler=handler;
}
public void run(){
recive(handler);
}
String TAG="UdpTest_Server";
public void recive(Handler handler){
DatagramSocket ds=null;
try {
//1. 定义udp服务,接收来自端口号为10000的数据
ds=new DatagramSocket(1000_0);
//2. 定义数据包接收数据
byte[] buff=new byte[1024] ;
DatagramPacket dp=new DatagramPacket (buff,buff.length);
Message msg=handler.obtainMessage(1);
msg.obj="服务已经启动准备接收数据!";
handler.sendMessage(msg);
while (true) {
//3.通过udp服务接收数据,receive方法阻塞,直到接收到数据报
ds.receive(dp);
//4.取出数据
String ip = dp.getAddress().getHostAddress();
int len = dp.getLength();
String data = new String(dp.getData(), 0, len);
//---------和ui通讯-------------
msg = handler.obtainMessage(1);
msg.obj = "来自:" + ip + "的数据:" + data;
handler.sendMessage(msg);
}
//------------------------------
} catch (SocketException e) {
Message msg=handler.obtainMessage(1);
msg.obj="SocketException异常:"+e.toString();
handler.sendMessage(msg);
} catch (IOException e) {
Message msg=handler.obtainMessage(1);
msg.obj="IOException异常:"+e.toString();
handler.sendMessage(msg);
}finally {
//5.关闭资源close
if(ds!=null)
ds.close();
}
}
}
客户端代码:
public class UdpClient extends Thread{
String TAG="UdpTest_Client";
Handler handler;
public UdpClient(Handler handler){
this.handler=handler;
}
public void run(){
sendData(handler);
}
public void sendData(Handler handler){
//1.建立udp服务
DatagramSocket ds= null;
Message message=handler.obtainMessage();
try {
ds = new DatagramSocket();
//2.创建数据包
byte[] buff="udp client test" .getBytes();
InetAddress ip=InetAddress.getByName("192.168.43.1");//目的地IP
int port=10000;// 目的地端口
DatagramPacket dp = new DatagramPacket(buff,buff.length, ip,port);
//3.发送数据
ds.send(dp);
message.obj="udp 发送数据完成!";
handler.sendMessage(message);
//关闭资源
} catch (SocketException e) {
//Socket无法打开,或Socket无法绑定到指定的本地端口
message.obj="SocketException 异常="+e.toString();
handler.sendMessage(message);
}catch (UnknownHostException e) {
//IP地址是非法的长度
message.obj="UnknownHostException 异常="+e.toString();
handler.sendMessage(message);
} catch (IOException e) {
// 发生I / O错误。发送失败
message.obj="IOException 异常="+e.toString();
handler.sendMessage(message);
}finally {
if (ds != null) {
//message.obj="关闭资源";
// handler.sendMessage(message);
ds.close();
}
}
}
}
二、TCP:面向连接的通信。
特点:1.传输效率低
2.安全性高
开发流程:
客户端:
1.创建Socket服务,指定ip和端口
Socket socket=new Socket("192.168.1.12",10001);
2.发送数据,获取socket输出流
OutputStream out=socket.getOutputStream();
out.write("tcp测试".getBytes());
3.关闭socket
s.close();
服务端:
1.建立服务端的socket服务,监听端口
ServerSocket ss=new ServerSocket(10001);
2.通过accept方法获取连接过来的客户端对象
Socket s=ss.accept();//会阻塞
3.获取客户端发过来的数据
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);//read 会阻塞
String data=new String(buff,0,len);
4.关闭资源
服务端代码:
public class TcpServer extends Thread {
Handler handler=null;
public TcpServer(Handler handler){
this.handler=handler;
}
public void run(){
recive(handler);
}
public void recive(Handler handler){
ServerSocket ss=null;
try {
//1.建立服务端的socket服务,监听端口10000
ss=new ServerSocket(1000_1);
while (true){
showMsg("TCP服务已经开启!");
// 2.通过accept方法获取连接过来的客户端对象
Socket s=ss.accept();//会阻塞
showMsg("客户端连接:"+s.getInetAddress().getHostAddress());
//3.获取客户端发过来的数据
InputStream in=s.getInputStream();
byte[] buff=getData(in);
if(buff!=null) {
String data = new String(buff, 0, buff.length);
showMsg("接收来自:"+s.getInetAddress().getHostAddress()+",的数据:"+data);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(ss!=null){
try {
//4.关闭资源
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private byte[] getData(InputStream in){
byte[] temp=new byte[1024];
try {
int len= in.read(temp);
return Arrays.copyOf(temp,len);
} catch (IOException e) {
showMsg("读取数据发送异常:"+e.toString());
}
return null;
}
private void showMsg(String strMsg){
Message msg = handler.obtainMessage();
msg.obj = strMsg;
handler.sendMessage(msg);
}
}
客户端代码:
public class TcpClient extends Thread{
Handler handle=null;
public TcpClient(Handler handler){
this.handle=handler;
}
public void run(){
send();
}
public void send(){
//1.创建Socket服务,指定ip和端口
Socket socket= null;
try {
socket = new Socket("192.168.43.1",1000_1);
//2.发送数据,获取socket输出流
OutputStream out=socket.getOutputStream();
out.write("tcp客户端数据".getBytes());
showMsg("客户端发送数据成功!");
} catch (IOException e) {
showMsg("客户端发送数据异常:"+ e.toString());
e.printStackTrace();
}finally {
//3.关闭socket
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void showMsg(String data){
Message msg=handle.obtainMessage(1);
msg.obj=data;
handle.sendMessage(msg);
}
}