= Connecting
:author: 
:revnumber: 
:revdate: 2016/03/17 20:48
:relfileprefix: ../../
:imagesdir: ../..
ifdef::env-github,env-browser[:outfilesuffix: .adoc]



[WARNING]
====
This article covers a deprecated +++<abbr title="Application Programming Interface">API</abbr>+++! See <<jme3/advanced/networking#,networking>> for current documentation.
====


This very first tutorial is going to teach you how to open a server and a client, and connect them to eachother. I'll also discuss how connection registration works. Since this is a very simple process in SpiderMonkey, this tutorial will be quite short.


== Simple connections

Creating a server is as simple as doing this:

[source,java]
----

Server myServer = new Server(4040, 5050);
myServer.start();

----

This initializes and starts a server on TCP port 4040 and UDP port 5050. Now it's time to connect a client:

[source,java]
----

Client client = new Client("localhost", 4040, 5050);
client.start();

----

This initializes and starts a client, and it will immediately connect to localhost, TCP port 4040, and UDP port 5050. In the log, you'll get to see this:

[source]
----

Sep 16, 2010 11:52:16 AM com.jme3.network.connection.TCPConnection bind
INFO: [Server#1][TCP] Bound to 0.0.0.0/0.0.0.0:4040
Sep 16, 2010 11:52:16 AM com.jme3.network.connection.UDPConnection bind
INFO: [Server#1][UDP] Bound to 0.0.0.0/0.0.0.0:5050
Sep 16, 2010 11:52:16 AM com.jme3.network.connection.Server start
INFO: [Server#1][???] Started server.
Sep 16, 2010 11:52:16 AM com.jme3.network.connection.TCPConnection connect
INFO: [Client#1][TCP] Connecting to localhost/127.0.0.1:4040
Sep 16, 2010 11:52:16 AM com.jme3.network.connection.UDPConnection connect
INFO: [Client#1][UDP] Set target to localhost/127.0.0.1:5050
Sep 16, 2010 11:52:16 AM com.jme3.network.connection.TCPConnection accept
INFO: [Server#1][TCP] A client connected with address /127.0.0.1
Sep 16, 2010 11:52:16 AM com.jme3.network.connection.TCPConnection connect
INFO: [Client#1][TCP] Connection succeeded.

----

As you can see, this is a combined log of the client and server. Even though it looks like only a connection has been made, the Client registration has already happened at this point as well. Client registration is necessary so you can call TCP and UDP methods on only one Client instance on the server. You don't have to worry about client registration, since SpiderMonkey does this automatically on connection.


== Connector filters

You can also filter connections (or connectors as I call them) in SpiderMonkey. You can do this by implementing the ConnectorFilter interface:

[source,java]
----

public class MyConnectorFilter implements ConnectorFilter {
   public String filterConnector(InetSocketAddress address) {
      if (address.isLoopbackAddress()) return "I don't like locals!";
      return null;
   }  
}

----

Return null for no filtering, or a String with the reason if you want to filter this person.


== Discover hosts

SpiderMonkey Clients are also able to discover hosts running in the +++<abbr title="Local Area Network">LAN</abbr>+++. This is also a very simple process, and can be done as follows:

[source,java]
----

Client client = new Client();
List<InetAddress> foundHosts = client.discoverHosts(5050, 5000);

----

This starts the host discovery on port 5050, and listens for servers for 5 seconds. Typically, servers respond pretty fast so a few seconds should be enough. To do something with these hosts it's as simple as doing:

[source,java]
----

for (InetAddress host : foundHosts) {
    client.connect(host.getCanonicalHostName(), 4040, 5050);
}
client.start();

----

Do note that this would connect to every host found, so this does not work properly, but the idea is that you can configure it the way you want it. Don't forget to start() the client.

This concludes the first tutorial. In the next tutorial, it's time to send and listen for messages!
