简体   繁体   English

将套接字的OutputStream与DataOutputStream进行比较

[英]Compare the OutputStream of a Socket with a DataOutputStream

I have a bunch of DataOutputStream s in Hashtable outputStreams; 我在Hashtable outputStreams;有一堆DataOutputStream Hashtable outputStreams; . There is a Socket for each of them. 每个都有一个Socket I want to loop through them but I want to exclude one particular Socket . 我想遍历它们,但是我想排除一个特定的Socket I've been trying with the following algorithm but it doesn't seem to work. 我一直在尝试以下算法,但似乎不起作用。

for(Enumeration e = outputStreams.elements(); e.hasMoreElements(); ) {

    DataOutputStream dout = (DataOutputStream)e.nextElement();
    OutputStream sdout = null;
    try {
        sdout = socket.getOutputStream();
    } catch (IOException ie) {
        ie.printStackTrace();
    }

    if (dout != sdout) {
        try {
            dout.writeUTF(message);
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}

This is presumably not working because socket.getOutputStream() is returning the "raw" output stream, and somewhere you'll have created a DataOutputStream for each of those raw output streams. 大概这是行不通的,因为socket.getOutputStream()返回“原始”输出流,并且在某个地方您将为每个原始输出流创建一个DataOutputStream These will never be the same. 这些将永远是不一样的。

My best guess what you're trying to achieve, would be to iterate through the sockets, skipping the one you don't want to write to, and then finding the respective DataOutputStream and writing to it. 我最好的猜测是要遍历套接字,跳过不想写的套接字, 然后找到相应的DataOutputStream并对其进行写入。

It appears that a Hashtable will insert a copy of the instance into its backing structure. 似乎Hashtable会将实例的副本插入其支持结构。 Andypandy is right about your problem, but then I wondered if a table of OuputStream s (where you wouldn't wrap the socket output stream) would fix it and it didn't. Andypandy对您的问题是正确的,但是随后我想知道OuputStream的表(您将不会包装套接字输出流的表)是否可以解决该问题,而不能解决。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Hashtable;

public class Main
{

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

        Hashtable<Integer, OutputStream> ht = new Hashtable<Integer, OutputStream>();

        for(int i = 0; i < 5; i++)
            ht.put( i, new DataOutputStream(new ByteArrayOutputStream()) );

        Socket sock = new Socket("google.com", 80);
        ht.put( ht.size(), sock.getOutputStream() );

        for(OutputStream dos : ht.values())
            System.out.println(dos);

        System.out.println(sock.getOutputStream());
    }
}

Output: 输出:

java.net.SocketOutputStream@112b853
java.io.DataOutputStream@36428
java.io.DataOutputStream@a4b78b
java.io.DataOutputStream@e3f6d
java.io.DataOutputStream@1660d22
java.io.DataOutputStream@e84763
java.net.SocketOutputStream@1a6c368

The socket's output stream that is inserted into the table has a different address than that of the original output stream. 插入表中的套接字的输出流的地址与原始输出流的地址不同。 Since there's no equals override, there's no easy way to meaningfully compare streams without having knowledge of their corresponding sockets. 由于没有equals覆盖,因此没有简单的方法就可以在不了解其相应套接字的情况下有意义地比较流。 You should re-think your design completely, perhaps using the socket descriptor as your key. 您应该完全重新考虑设计,也许使用套接字描述符作为键。

EDIT 编辑

As blackcompe pointed out, neither DataOutputStream nor OutputStream override Object.equals. 正如blackcompe所指出的,DataOutputStream和OutputStream都不会覆盖Object.equals。 So this does not help. 因此,这无济于事。 Anyway the getOutpuStream() method might create a new Object anyway each time its called. 无论如何,每次调用getOutpuStream()方法都可能会创建一个新的Object。

You will probably have to create new Objects that hold the Outputstreams and the information about their socket. 您可能必须创建新的对象来保存Outputstreams及其有关套接字的信息。 And iterate over them. 并遍历它们。

The output stream of a socket is never equal to any instance of DataOutputStream. 套接字的输出流从不等于DataOutputStream的任何实例。 You need to rethink what it is you are actually asking here. 您需要重新考虑您实际上在这里问的是什么。

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

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