Results 1 to 6 of 6
Like Tree4Likes
  • 1 Post By ra4king
  • 1 Post By ra4king
  • 1 Post By Grkn
  • 1 Post By Grkn

Thread: Socket/SocketChannel and DataInputStream incompatibilities

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default Socket/SocketChannel and DataInputStream incompatibilities

    I tried to post this on another Java forum in their "File I/O..." section, but it didn't seem to show up, so I'll try here:

    I'm trying to send and receive messages over sockets using non-blocking sockets. I found the following example (http://www.exampledepot.com/egs/java...tSocket.html):

    {code}
    // Creates a non-blocking socket channel for the specified host name and port.
    // connect() is called on the new channel before it is returned.
    public static SocketChannel createSocketChannel(String hostName, int port) throws IOException {
    // Create a non-blocking socket channel
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);

    // Send a connection request to the server; this method is non-blocking
    sChannel.connect(new InetSocketAddress(hostName, port));
    return sChannel;
    }

    // Create a non-blocking socket and check for connections
    try {
    // Create a non-blocking socket channel on port 80
    SocketChannel sChannel = createSocketChannel("hostname.com", 80);

    // Before the socket is usable, the connection must be completed
    // by calling finishConnect(), which is non-blocking
    while (!sChannel.finishConnect()) {
    // Do something else
    }
    // Socket channel is now ready to use
    } catch (IOException e) {
    }
    {code}

    ...but I get the compile-time error "InetSocketAddress cannot be resolved to a type" on this line:

    {code}
    sChannel.connect(new InetSocketAddress(hostName, port));
    {code}

    ...but when trying to incorporate it into my existing code, I get:

    "The method getInputStream() is undefined for the type SocketChannel"
    -and:
    "The method getOutputStream() is undefined for the type SocketChannel"

    ...here (last two lines):

    {code}
    try {
    // Create a non-blocking socket channel on port 80
    sDispatchChannel = RT_TCP_JCP_TDS_OO_State_SortSim_Utils.createSocket Channel(sIPAddr,
    Integer.parseInt(tfDispatchPort.getText()));
    inDispatchFile = new DataInputStream(
    sDispatchChannel.getInputStream());
    outDispatchFile = sDispatchChannel.getOutputStream();
    ...
    {code}

    Does anybody know how I can initialize a DataInputStream using a SocketChannel (as opposed to a Socket)? Or have I been drinking too much Congress Water ("you are what your drink")?

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default Re: Socket/SocketChannel and DataInputStream incompatibilities

    1. Try importing InetSocketAddress?
    2. Those methods do not exist for SocketChannel.
    3. SocketChannel and DatagramChannel are two completely different classes, one is TCP and the other is UDP respectively.
    blackbird likes this.

  3. #3
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default Re: Socket/SocketChannel and DataInputStream incompatibilities

    "1. Try importing InetSocketAddress?
    2. Those methods do not exist for SocketChannel.
    3. SocketChannel and DatagramChannel are two completely different classes, one is TCP and the other is UDP respectively."

    OK, I'm finally able to respond after that long outage:

    1) I did - it's there
    2) So what would the corresponding methods be?
    3) I don't think I mentioned DatagramChannel anywhere...?

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default Re: Socket/SocketChannel and DataInputStream incompatibilities

    1. So what problem do you have?
    2. Look at the socket() method in SocketChannel. You can also use the utility methods in java.nio.Channels (like Channels.newInputStream(channel) and Channels.newOutputStream(channel)) ;)
    3. whoops my bad :)
    Last edited by ra4king; 10-20-2011 at 12:32 AM.
    blackbird likes this.

  5. #5
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Socket/SocketChannel and DataInputStream incompatibilities

    socketchannel has read method so you don't need to call InputStream or OutputStream



    SocketVariable.read(buff);
    blackbird likes this.

  6. #6
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Socket/SocketChannel and DataInputStream incompatibilities

    Maybe I can explain you the reason InputStream blocks the thread and waits for input when data is not available.
    In nonblocking socket you can not block the flow. So Socket itself deals with communication
    blackbird likes this.

Similar Threads

  1. DataInputStream
    By robs in forum Java Gaming
    Replies: 7
    Last Post: 04-14-2011, 07:27 PM
  2. socketchannel and thread
    By spiderman in forum New To Java
    Replies: 0
    Last Post: 08-20-2010, 02:59 PM
  3. Losing Packets/Bytes using DataInputStream and socket
    By ajordanneve in forum Networking
    Replies: 0
    Last Post: 03-24-2009, 06:22 AM
  4. Problem with DataInputStream
    By ZivonHunter in forum New To Java
    Replies: 1
    Last Post: 11-21-2008, 06:10 AM
  5. Replies: 1
    Last Post: 08-07-2007, 08:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •