Skip to content

TCPConnections

Thomas Schwotzer edited this page Sep 11, 2023 · 9 revisions

We need two things to happen to set up a TCP connection:

  • One process provides an open TCP port (SocketServer in Java). There is usually a thread running in an applications that waits for new connection attempts.
  • Another process connects to this port (Socket in Java).

It is not even complicated to program this be your own. You can also use our classes which we needed to implement our test peer. Let's have a look into a code example.

(A full and working example can be found in unit test connectAliceAndBob_2 in ConnectPeers.)

// create a peer (not even a test peer)
ASAPConnectionHandler alicePeerFS = new ASAPPeerFS(TestConstants.ALICE_ID, aliceFolder, formats);

// get port number - in tests we create a new one for each unit test.
int portNumber = TestHelper.getPortNumber();

// that listener will call its peer if a new connection was establised
StreamPairCreatedListenerImpl aliceStreamPairCreatedListener = new StreamPairCreatedListenerImpl(alice);
// see explanations below.
SocketFactory socketFactory = new SocketFactory(portNumber, aliceStreamPairCreatedListener);
// start it
Thread socketFactoryThread = new Thread(socketFactory);
socketFactoryThread.start();

SocketFactory makes the trick. It better runs in a thread. It creates a TCP server socket and waits to accept connection requests. Each new connection is notified to the StreamPairCreatedListener. In out case, aliceStreamPairCreatedListener will ask peer alice to handle the newly established connection.

It's a slim implementation. Each new connection is send to the peer to handle it (handleConnection) Connecting to an open TCP port does not require any further support. Soem code from our unit test example.

Create connections

// peer as connection handler
ASAPConnectionHandler bob = new ASAPPeerFS(TestConstants.BOB_ID, bobFolder, formats);
// create a connection 
// remote peer address comes most likely from user input
// real apps should define a well-known port
Socket socket = new Socket(addressRemotePeer, portNumber);

bob.handleConnection(socket.getInputStream(), socket.getOutputStream());

A real app will connect to other peers. That connection establishment might be initiated by user or happens on some regular intervals. In any case, the code above would connect to another peer and ask the local peer to run an ASAP session.

Open Issues

Using TCP as communication protocol is difficult. The little supported that is provided is hopefully nice but hardly necessary. But we ignored some issues which can sooner or later up in a real infrastructure. Just to name two:

  • We call of those issues flickering. Especially in wireless ad-hoc networks connections break down and re-establishment alternates annoyingly quickly when they reach the perimeter communication range.
  • Network connections can be a rare resource. In some environments, a device can only establish a single connection on a given protocol. It would be very helpful to close such idle connections. ASAP peers cannot create and will never close a connection, though.

Our EncounterManager helps.