java习题:编写一个数据报通信程序,一端发送一个int型数据10000,另一端接收到发送的数据并将

java习题:编写一个数据报通信程序,一端发送一个int型数据10000,另一端接收到发送的数据并将,第1张

import java.io.DataInputStream

import java.io.DataOutputStream

import java.io.IOException

import java.io.InputStream

import java.io.OutputStream

import java.net.ServerSocket

import java.net.Socket

public class SocketTest {

    public static void main(String[] args) {

        new ServerThread().start()

        new ClientThread().start()

    }

    private static class ServerThread extends Thread {

        private InputStream inputStream

        private OutputStream outputStream

        public void run() {

            try {

                initServer()

                doServer()

            } catch (IOException e) {

                e.printStackTrace()

            } finally {

                closeServer()

            }

        }

        private void initServer() throws IOException {

            ServerSocket socket = new ServerSocket(8899)

            Socket accept = socket.accept()

            inputStream = accept.getInputStream()

        }

        private void doServer() throws IOException {

            DataInputStream dis = new DataInputStream(inputStream)

            while (dis.available() > 0) {

                System.out.println(dis.readInt())

            }

        }

        private void closeServer() {

            if (inputStream != null) {

                try {

                    inputStream.close()

                } catch (IOException e) {

                    e.printStackTrace()

                }

         如州   }

        }

    }

    private static class ClientThread extends Thread {

        private InputStream inputStream

        private OutputStream outputStream

        public void run() {

            try {

                initClient()

                doClient()

            } catch (IOException e) {

                e.printStackTrace()

            } finally {

                closeClient()

            }

        }

        private void initClient() throws IOException {

            Socket socket = new Socket("localhost", 8899)

            outputStream = socket.getOutputStream()

        }

        private void doClient() 渣卖蔽throws IOException {

            DataOutputStream dos = new DataOutputStream(outputStream)

            dos.writeInt(10000)

            dos.writeInt(10001)

            dos.writeInt(10002)

            dos.flush()

        }

        private void closeClient() {

            if (outputStream != 配碧null) {

                try {

                    outputStream.close()

                } catch (IOException e) {

                    e.printStackTrace()

                }

            }

        }

    }

}

这是java网络编程里的UDPServer 和UDPClient 之间简单通信程序啊,等等我宏仿看看你的程序再说

你想要的是不是这种效果?

import java.net.*

class UdpSend{

  public static void main(String[] args)throws Exception{

      //1.创建udp服务,通过DatagramSocket对象

      DatagramSocket ds = new DatagramSocket(8888)//---这儿的端口号不要和UdpRece的一样

      //2.确定数据,并封装成数据包。

      //DatagramPacket(byte[] buf, int length, InetAddress address,

int port)

          //构造数据报包,用来将长度为 length 的包发送到蔽清纤指定主机上的

指定端口号。

      byte[] buf = "udp shu ju lai le".getBytes()

      DatagramPacket dp =

          new DatagramPacket(buf,buf.length,InetAddress.getByName

("169.254.200.14"),10000)//----这儿的端口号保持与UdpRece一样正竖

      //3.通过Socket服务,将已有的数据包发送出去。通过send方法。

      ds.send(dp)

      //4.关闭资源

      ds.close()

  }

}

最后想说,先运行UdpRece 再运行UdpSend,反过来UdpSend要运行2遍

Windows下开发视频采集方面的东西,你去找网上找一下DirectShow发中哗面的资料,很多了,先要熟悉一下ActiveX编搏培乱程不是很难。

Java应该是不能写驱动,因为java跨平台,而各个平台之间底层驱动完全不同,而且java的.class要靠虚拟机解释,由于虚拟机工作在ring3级,驱动程序却必须是工作在内核的ring0级,因此.class无法运行在ring0级。只知道VC能写VXD或者WDM驱动。

但是在windows环境下application模式的Java可以调用本地的DLL中的函数,你可以用VC写驱动,把相应的功能用VC、Delphi做成DLL供java调用,不过要记住必须是本地的基档DLL。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12402847.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存