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

java网络编程之UDP实例

时间:2014-06-15 14:03:26      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   java   

package Socket;

import java.net.DatagramPacket;
import java.net.InetAddress;

public class Dgram {

    public static DatagramPacket toDatagram(String s, InetAddress destIA,
            int destPort) {
        byte[] buf = new byte[s.length() + 1];
        s.getBytes(0, s.length(), buf, 0);
        return new DatagramPacket(buf, buf.length, destIA, destPort);
    }

    public static String toString(DatagramPacket p) {
        return new String(p.getData(), 0, p.getLength());
    }
}

 

package Socket;

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

public class ChatterServer {
    static final int INPORT = 1712;
    private byte[] buf = new byte[1000];
    private DatagramPacket dp = new DatagramPacket(buf, buf.length);
    private DatagramSocket socket;

    public ChatterServer() {
        try {
            socket = new DatagramSocket(INPORT);// 创建一接收消息的对象,而不是每次接收消息都创建一个
            System.out.println("Server started");
            while (true) {
                socket.receive(dp);
                //接收到客户端的消息
                String rcvd = Dgram.toString(dp) + ",from address:"
                        + dp.getAddress() + ",port:" + dp.getPort();
                System.out.println("From Client:"+rcvd);
                
                String echoString = "From Server Echoed:" + rcvd;
                DatagramPacket echo = Dgram.toDatagram(echoString,
                        dp.getAddress(), dp.getPort());
                //将数据包发送给客户端
                socket.send(echo);
            }
        } catch (SocketException e) {
            System.err.println("Can‘t open socket");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Communication error");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new ChatterServer();
    }
}

 

package Socket;

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 ChatterClient extends Thread {

    private DatagramSocket s;
    private InetAddress hostAddress;
    private byte[] buf = new byte[1000];
    private DatagramPacket dp = new DatagramPacket(buf, buf.length);
    private int id;

    public ChatterClient(int identifier) {
        id = identifier;
        try {
            s = new DatagramSocket();
            hostAddress = InetAddress.getByName("localhost");

        } catch (UnknownHostException e) {
            System.err.println("Cannot find host");
            System.exit(1);
        } catch (SocketException e) {
            System.err.println("Can‘t open socket");
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("ChatterClient starting");
    }

    public void run() {
        try {
            for (int i = 0; i < 25; i++) {
                String outMessage = "Client #" + id + ",message #" + i;
                s.send(Dgram.toDatagram(outMessage, hostAddress,
                        ChatterServer.INPORT));
                s.receive(dp);
                String rcvd = "Client #" + id + ",rcvd from " + dp.getAddress()
                        + ", " + dp.getPort() + ":" + Dgram.toString(dp);
                System.out.println(rcvd);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new ChatterClient(i).start();
        }
    }
}

 

java网络编程之UDP实例,布布扣,bubuko.com

java网络编程之UDP实例

标签:des   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/zhuawang/p/3789291.html

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