Documentation for Application Develpers

The Basic Idea

You will need to import the net.sourceforge.dghmux.* and net.sourceforge.dghmux.exceptions.* packages.

import net.sourceforge.dghmux.*;
import net.sourceforge.dghmux.exceptions.*;

In this example a local application will connect to a remote server via TCP/IP. After connnecting to a remote host using TCP/IP sockets, the local application will hand over the socket connection to DGHMux

DGHMux dghMux = new DGHMux(myTCPSocket, false);

The false indicates that the local application made the TCP/IP connection to the server. The remote server will do a similar step, except it will use true. It is important that one side of the connection gives false, and the other side of the connection gives true.

The next step is to create a stream to send data to the server. Any side of the TCP/IP connection can do this. In this example, the local application creates a stream to a DGHMux listener on the remote server.

/* on the client */ Socket myStream=dghMux.connect("serverListener");

The String "serverListener" is any string that you choose. DGHMux will go out and find the listener on the remote connection called "serverListener".

The server does a similar step to receive the connection

/* on the server */ Socket serverStream=dghMux.accept("serverListener");

From the myStream socket, the local application can get the InputStreams and OutputStreams.

InputStream in = myStream.getInputStream();
OutputStream out = myStream.getOutputStream();

When the stream is finished, close the streams and the DGHMux Socket.

in.close();
out.close();
myStream.close();

And once you are done with the TCP/IP connection, close the DGHMux object.

dghMux.close();

Below is an example code that includes the necessary try/catch blocks.

Sample Source Code

    try{

        DGHMux dghMux = new DGHMux(myTCPSocket, false);

        try {
        
            Socket myStream; 

            myStream = dghMux.connect("serverListener");
            

            try {
                InputStream in = s.getInputStream();
                try {
                    OutputStream out = s.getOutputStream();
                    try {

                        /* send and receive data */
                        
                    } 
                    finally {
                         out.close();
                    }
                }
                finally {
                    in.close();
                }
            }
            finally {
                myStream.close();
            }
            

        }
        finally {
            dghMux.close();
        }
    }
    catch(Exception e) {
        /* handle the Exception */
    }

But I thought the server could make a DGHMux connection to the client.

Yes, the server can make a DGHMux connection to the client. Just change "connect" in the above code to "accept" and the client will now be waiting for a DGHMux connection.

When is a an exception thrown if no listener picks up the DGHMux stream?

Since there is no latency from starting a connection and sending data, you could be notified while reading or writting to the InputStream or OutputStream, or when you close the DGHMux Socket. That's right. You may find out when you close the conneciton, that you never talked to anyone at all! The close method on the DGHMux Socket will wait until the connection is made, all the data is sent, and the connection is closed, so that you will be notified of any errors.

Your HTML pages look ugly!

It is true, and I even work for a software company that writes web applications (www.abelsolutions.com)! They do not let me design the web pages.

SourceForge Logo