简体   繁体   English

如何通过同一个套接字发送不同类型的数据

[英]How to send different kinds of data over the same socket

I am wondering how this works: https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readUTF() 我想知道它是如何工作的: https//docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readUTF()

If we use a DataOutputStream to writeUTF("dog") and writeUTF("cat") , does readUTF() called once get 'dog' and then called a 2nd time get 'cat'? 如果我们使用DataOutputStream来写入writeUTF("dog")writeUTF("cat") ,那么readUTF()调用一次得到'dog'然后调用第二次得到'cat'吗?

Also, what is the best way to accomplish this (from this code: https://github.com/richardrl/downloader/tree/master/src/main/java ): I have a file on 1 out of N servers. 此外,实现此目的的最佳方法是什么(从此代码: https//github.com/richardrl/downloader/tree/master/src/main/java ):我有一个N服务器中的1个文件。 I have the client query one of the servers. 我有客户端查询其中一个服务器。 If it's not on the server, I want the client to download the file from the correct server in at most 2 requests. 如果它不在服务器上,我希望客户端在最多2个请求中从正确的服务器下载文件。

My plan right now is to have a hashmap (of all files across all servers) that maps each file to the port number and domain name of the server it's on (the two properties to locate a server socket). 我现在的计划是拥有一个散列图(所有服务器中的所有文件),它将每个文件映射到它所在的服务器的端口号和域名(用于定位服务器套接字的两个属性)。 When the client requests from any server, the requested server will search the hashmap for the file. 当客户端从任何服务器请求时,请求的服务器将在hashmap中搜索该文件。 If not found on the current server, the current server will send an error (currently accomplished by writeBoolean(false) ) and then the port number and domain name of the correct server that holds the file. 如果在当前服务器上找不到,则当前服务器将发送错误(当前由writeBoolean(false) ),然后发送包含该文件的正确服务器的端口号和域名。

My current problem is I am getting NoRouteToHostException through the following process. 我目前的问题是我通过以下过程获得NoRouteToHostException。

  1. Clone all files here: https://github.com/richardrl/downloader/tree/master/target/classes 克隆所有文件: https//github.com/richardrl/downloader/tree/master/target/classes

  2. Go into "dummyserver" folder and type in Terminal: 'java DLServer 1124' to start server. 进入“dummyserver”文件夹并输入Terminal:'java DLServer 1124'以启动服务器。 As you can see, the requested file "dummy.txt" is in the same folder as this server. 如您所见,请求的文件“dummy.txt”与此服务器位于同一文件夹中。

  3. Go back up one level and type in: 'java DLServer 4444'. 返回上一级并输入:'java DLServer 4444'。

  4. Now type in 'java DownloaderClient localhost 4444' to start client. 现在输入'java DownloaderClient localhost 4444'来启动客户端。 Type in "dummy.txt" to request "dummy.txt". 输入“dummy.txt”以请求“dummy.txt”。 Instead of correctly connecting to the dummyserver, I get this error: 我没有正确连接到虚拟服务器,而是收到此错误:

    java DownloaderClient localhost 4444 In while loop dummy.txt Client: dummy.txt Exception in thread "main" java.net.NoRouteToHostException: Can't assign requested address at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.(Socket.java:425) at java.net.Socket.(Socket.java:208) at DownloaderClient.main(DownloaderClient.java:21) java DownloaderClient localhost 4444在while循环中dummy.txt客户端:dummy.txt线程“main”中的异常java.net.NoRouteToHostException:无法在java.net上的java.net.PlainSocketImpl.socketConnect(Native Method)中分配请求的地址。 java.net.SocksSocketImpl.connect上的java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)中的java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)上的AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)(SocksSocketImpl .java:392)java.net.Socket.connect(Socket.java:579)at java.net.Socket.connect(Socket.java:528)at java.net.Socket。(Socket.java:425)at java.net.Socket。(Socket.java:208)at DownloaderClient.main(DownloaderClient.java:21)

I'm a little confused about what your asking, so I'll just try to answer the question in the title. 我对你的要求有点困惑,所以我只想回答标题中的问题。 For the small amount of time I've used java sockets, whenever I sent data I first turned the data into an array of bytes then added a byte in the first index to determine what the byte array should be interpreted as when it is received. 在我使用java套接字的少量时间内,每当我发送数据时,我首先将数据转换为字节数组,然后在第一个索引中添加一个字节,以确定字节数组在接收时应解释为什么。 Check here to learn how to do that - http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte 点击此处了解如何操作 - http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte

I'm not sure if this answered your question, but maybe this can help. 我不确定这是否回答了你的问题,但也许这可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM