zoukankan      html  css  js  c++  java
  • Java 最简单的UPD Server

    package com.casic.test;

    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 AudioServer implements Runnable {
     
        DatagramSocket socket;
        DatagramPacket packet;// 从客户端接收到的UDP包
        DatagramPacket sendPkt;// 转发给另一个客户端的UDP包
     
        byte[] pktBuffer = new byte[1024];
        int bufferSize = 1024;
        boolean isRunning = false;
        int myport = 5656;
     
        int clientPort = 5757;
     
        public AudioServer() {
            try {
                socket = new DatagramSocket(myport);
                packet = new DatagramPacket(pktBuffer, bufferSize);
            } catch (SocketException e) {
                e.printStackTrace();
            }
            System.out.println("服务器初始化完成");
        }
     
        public void startServer() {
            this.isRunning = true;
            new Thread(this).start();
        }
     
        public void run() {
            try {
                while (isRunning) {
                    socket.receive(packet);
                    sendPkt = new DatagramPacket(packet.getData(),
                            packet.getLength(), packet.getAddress(), clientPort);
                    socket.send(sendPkt);
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
            }
        }
     
        // main
        public static void main(String[] args) {
            new AudioServer().startServer();
        }
    }

  • 相关阅读:
    SQL中 patindex函数的用法
    纵表与横表互转的SQL
    几种数据库的大数据批量插入【转】
    使用cmd命令行窗口操作SqlServer
    C#性能优化实践
    C# 图像处理(二)—— 黑白效果
    SQLSERVER系统视图
    SQLServer行列转换 Pivot UnPivot
    C#图片处理常见方法性能比较
    GSM局数据制作1(Erision)
  • 原文地址:https://www.cnblogs.com/oftenlin/p/2968433.html
Copyright © 2011-2022 走看看