博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java学习笔记】UDP客户端/服务器端
阅读量:6485 次
发布时间:2019-06-23

本文共 2368 字,大约阅读时间需要 7 分钟。

作者:

出处:

客户端:

import java.net.DatagramPacket;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.io.IOException;
public class UdpClient {
    public static void main(String arg[]) {
        String outstr;
        if(arg.length >= 1)
            outstr = arg[0];
        else
            outstr = "count";
        try {
            DatagramSocket socket = new DatagramSocket();
            byte outblock[] = outstr.getBytes();
            InetAddress address = InetAddress.getLocalHost();
            DatagramPacket outpacket =
                    new DatagramPacket(outblock,outblock.length,address,8765);
            socket.send(outpacket);
            System.out.println("Client sent: " + outstr);
            byte inblock[] = new byte[256];
            DatagramPacket inpacket =
                    new DatagramPacket(inblock,inblock.length);
            socket.receive(inpacket);
            String instr = new String(inpacket.getData(),0,inpacket.getLength());
            System.out.println("Client got: " + instr);
            socket.close();
        } catch(SocketException e) {
            System.out.println(e);
        } catch(UnknownHostException e) {
            System.out.println(e);
        } catch(IOException e) {
            System.out.println(e);
        }
    }
}

服务器端:

import java.net.DatagramSocket;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.io.IOException;
import java.util.Date;
public class UdpServer {
    public static void main(String arg[]) {
        DatagramSocket socket = null;
        int count = 0;
        try {
            socket = new DatagramSocket(8765);
        } catch(IOException e) {
            System.out.println(e);
        }
        while(true) {
            try {
                byte block[] = new byte[256];
                DatagramPacket inpacket
                        = new DatagramPacket(block,block.length);
socket.receive(inpacket);
                int length = inpacket.getLength();
                System.out.println("Length of the data received: " + length);
   byte inblock[] = inpacket.getData();
                String inmsg = new String(inblock,0,length);
                System.out.println("Server got: " + inmsg);
                count++;
                String outmsg;
                if(inmsg.equals("date")) {
                    Date date = new Date();
                    outmsg = date.toString();
                } else if(inmsg.equals("halt")) {
                    socket.close();
                    return;
                } else if(inmsg.equals("count")) {
                    outmsg = "Number of messages: " + count;
                } else {
                    outmsg = "What is " + inmsg + "?";
                }
                byte outblock[] = outmsg.getBytes();
                InetAddress returnaddress = inpacket.getAddress();
                int returnport = inpacket.getPort();
                DatagramPacket outpacket = new DatagramPacket(
                        outblock,outblock.length,returnaddress,returnport);
                socket.send(outpacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

作者:

出处:

转载地址:http://bgnuo.baihongyu.com/

你可能感兴趣的文章
hdu 2853
查看>>
VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试...
查看>>
Vue.js常用指令:v-show和v-if
查看>>
java自定义接口
查看>>
Codeforces Round #152 (Div. 2) B题 数论
查看>>
马云马化腾等大佬,是如何看待区块链的?
查看>>
10倍于行业增速!海尔冰箱套圈引领
查看>>
Java集合总结【面试题+脑图】,将知识点一网打尽!
查看>>
java基础(十) 数组类型
查看>>
小程序 Canvas绘图不同尺寸设备 UI兼容的两个解决方案
查看>>
产品规划,你通常规划多久的时间线?
查看>>
Android-MVP架构
查看>>
HTML5前端教程分享:CSS浏览器常见兼容问题
查看>>
Material Design之AppBarLayout
查看>>
让mysql不能为空的字段为空时也能插入
查看>>
一服多开
查看>>
从CVS迁移到SVN
查看>>
总部与前线
查看>>
微软推出Windows 10专业教育版:Cortana没了
查看>>
TensorFlow教程之API DOC 6.1.2Class tensorflow::EnvWrapper
查看>>