android socket编程之UDP

socket是基于tcp和upd的,而tcp和udp是基于ip协议,

客户端:

package com.sunfb.upd;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class ServerClient {
    DatagramSocket cDatagramSocket=null;
    DatagramPacket cDatagramPacket=null;
    DatagramPacket receiverResponseDatagramPacket=null;
    byte[] b=new byte[1024];
    public ServerClient(){
        try {
            cDatagramSocket=new DatagramSocket();
        } catch (SocketException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    
    public DatagramPacket send(String host,int port,byte[] bytes){
        try {
            cDatagramPacket=new DatagramPacket(bytes, 0, bytes.length, InetAddress.getByName(host), port);
            cDatagramSocket.send(cDatagramPacket);
        
        } catch (UnknownHostException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return cDatagramPacket;
    }
    public String receiverResponse(){
        
        receiverResponseDatagramPacket=new DatagramPacket(b, b.length);
        try {
            cDatagramSocket.receive(receiverResponseDatagramPacket);
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        System.out.println("我是客户端 这是我从服务器的到的返回信息:"+new String(receiverResponseDatagramPacket.getData()));
        return new String(receiverResponseDatagramPacket.getData());
        
    }
    
    public static void main(String[] args) {
        ServerClient serverClient=new ServerClient();
        String host="192.168.0.196";
        int port=8826;
        String str="sunfb孙凤彪来也 接招吧";
        while(true){
        serverClient.send(host, port, str.getBytes());
        serverClient.receiverResponse();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        }
    }

}

服务端:

package com.sunfb.upd;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class ServerSocket {
    DatagramSocket datagramSocket=null;
    DatagramPacket datagramPacket=null;
    DatagramPacket responseDatagramPacket=null;
    InetSocketAddress inetSocketAddress=null;
    
    byte[] b=new byte[1024];
    
    public ServerSocket(String host,int port){
        //初始化 、
        inetSocketAddress=new InetSocketAddress(host, port);
        try {
            datagramSocket=new DatagramSocket(inetSocketAddress);
        } catch (SocketException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }
    public String receive(){
        String receiveStr=null;
        datagramPacket=new DatagramPacket(b, b.length);
        try {
            System.out.println("服务端已经启动。。。");
            datagramSocket.receive(datagramPacket);
            System.out.println("接收到客户端发送的信息是:"+new String(datagramPacket.getData()));
            
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return receiveStr;
    }
    public void response(){
        responseDatagramPacket=new DatagramPacket(b, b.length, datagramPacket.getAddress(), datagramPacket.getPort());
        try {
            System.out.println("我要给客户端返回数据了");
            datagramSocket.send(responseDatagramPacket);
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }
    public void close(){
        datagramSocket.close();
            }

    public static void main(String[] args) {
        String host="192.168.0.196";
        int port=8826;
        ServerSocket serverSocket=new ServerSocket(host, port);
        while(true){
        serverSocket.receive();
        serverSocket.response();
        }

    }

}

这里我也不做太多解释了,但注意的是在socket在基于udp编程时,客户端和服务端并没有什么区别,只是一个访问方向的问题

转载于:https://www.cnblogs.com/sunfb/archive/2012/11/28/2792082.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值