android socket编程之UDP

android socket编程之UDP_第1张图片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

你可能感兴趣的:(android socket编程之UDP)