码迷,mamicode.com
首页 > 编程语言 > 详细

java网络之udp

时间:2014-12-04 00:57:16      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   on   

 

import java.net.*;

/**
 * InetAddress用法
 * 获取本地主机名,ip
 * 通过主机名,ip获取InetAddress对象
 */
public class Demo{
    public static void main(String[] args) throws Exception {
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost.toString());
        System.out.println(localHost.getHostAddress());
        System.out.println(localHost.getHostName());
        
        InetAddress thinkpad = InetAddress.getByName("reborn-PC");
        System.out.println(thinkpad.toString());
    }
}

 

import java.net.*;

/**
 * 简单udp发送接收
 */
class UdpSend {
    public static void main(String[] args) throws Exception {
        // 1,创建udp服务。通过DatagramSocket对象。DatagramSocket(int port)可以绑定本地端口
        DatagramSocket ds = new DatagramSocket();

        // 2,确定数据,并封装成数据包。DatagramPacket(byte[] buf, int length, InetAddress
        // address, int port)

        byte[] buf = "udp ge men lai le ".getBytes();
        DatagramPacket dp = new DatagramPacket(buf, buf.length,
                InetAddress.getByName("127.0.0.1"), 10000);

        // 3,通过socket服务,将已有的数据包发送出去。通过send方法。
        ds.send(dp);

        // 4,关闭资源。

        ds.close();

    }
}

 

class UdpRece {
    public static void main(String[] args) throws Exception {
        // 1,创建udp socket,建立端点。
        DatagramSocket ds = new DatagramSocket(10000);
        while (true) {
            // 2,定义数据包。用于存储数据。
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf, buf.length);

            // 3,通过服务的receive方法将收到数据存入数据包中。
            ds.receive(dp);// 阻塞式方法。

            // 4,通过数据包的方法获取其中的数据。  获取发送端端口
            String ip = dp.getAddress().getHostAddress();

            String data = new String(dp.getData(), 0, dp.getLength());

            int port = dp.getPort();

            System.out.println(ip + "::" + data + "::" + port);

        }
        // 5,关闭资源
        // ds.close();

    }
}
/*
udp多线程实现

编写一个聊天程序。
有收数据的部分,和发数据的部分。
这两部分需要同时执行。
那就需要用到多线程技术。
一个线程控制收,一个线程控制发。

因为收和发动作是不一致的,所以要定义两个run方法。
而且这两个方法要封装到不同的类中。

*/
import java.io.*;
import java.net.*;
class Send implements Runnable
{
    private DatagramSocket ds;
    public Send(DatagramSocket ds)
    {
        this.ds = ds;
    }


    public void run()
    {
        try
        {
            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

            String line = null;

            while((line=bufr.readLine())!=null)
            {
                

                byte[] buf = line.getBytes();

                DatagramPacket dp = 
                    new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);

                ds.send(dp);

                if("886".equals(line))
                    break;
            }
        }
        catch (Exception e)
        {
            throw new RuntimeException("发送端失败");
        }
    }
}

class Rece implements Runnable
{

    private DatagramSocket ds;
    public Rece(DatagramSocket ds)
    {
        this.ds = ds;
    }
    public void run()
    {
        try
        {
            while(true)
            {
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacket(buf,buf.length);

                ds.receive(dp);


                String ip = dp.getAddress().getHostAddress();

                String data = new String(dp.getData(),0,dp.getLength());

                if("886".equals(data))
                {
                    System.out.println(ip+"....离开聊天室");
                    break;
                }


                System.out.println(ip+":"+data);
            }
        }
        catch (Exception e)
        {
            throw new RuntimeException("接收端失败");
        }
    }
}


class  ChatDemo
{
    public static void main(String[] args) throws Exception
    {
        DatagramSocket sendSocket = new DatagramSocket();
        DatagramSocket receSocket = new DatagramSocket(10002);

        new Thread(new Send(sendSocket)).start();
        new Thread(new Rece(receSocket)).start();

    }
}

 

java网络之udp

标签:style   blog   io   ar   color   os   sp   java   on   

原文地址:http://www.cnblogs.com/wikiki/p/4141835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!