| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>Connecting</title><link rel="stylesheet" href="./asciidoctor.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/spidermonkey/tutorial/connection.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/spidermonkey/tutorial/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>Connecting</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#simple-connections">Simple connections</a></li><li><a href="#connector-filters">Connector filters</a></li><li><a href="#discover-hosts">Discover hosts</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="admonitionblock warning"><table><tr><td class="icon"><i class="fa icon-warning" title="Warning"></i></td><td class="content"><div class="paragraph"><p>This article covers a deprecated <abbr title="Application Programming Interface">API</abbr>! See <a href="../../jme3/advanced/networking.html">networking</a> for current documentation.</p></div></td></tr></table></div>
- <div class="paragraph"><p>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.</p></div></div></div>
- <div class="sect1"><h2 id="simple-connections">Simple connections</h2><div class="sectionbody"><div class="paragraph"><p>Creating a server is as simple as doing this:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Server myServer = <span class="keyword">new</span> Server(<span class="integer">4040</span>, <span class="integer">5050</span>);
- myServer.start();</code></pre></div></div>
- <div class="paragraph"><p>This initializes and starts a server on TCP port 4040 and UDP port 5050. Now it’s time to connect a client:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Client client = <span class="keyword">new</span> Client(<span class="string"><span class="delimiter">"</span><span class="content">localhost</span><span class="delimiter">"</span></span>, <span class="integer">4040</span>, <span class="integer">5050</span>);
- client.start();</code></pre></div></div>
- <div class="paragraph"><p>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:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>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.</code></pre></div></div>
- <div class="paragraph"><p>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.</p></div></div></div>
- <div class="sect1"><h2 id="connector-filters">Connector filters</h2><div class="sectionbody"><div class="paragraph"><p>You can also filter connections (or connectors as I call them) in SpiderMonkey. You can do this by implementing the ConnectorFilter interface:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">MyConnectorFilter</span> <span class="directive">implements</span> ConnectorFilter {
- <span class="directive">public</span> <span class="predefined-type">String</span> filterConnector(<span class="predefined-type">InetSocketAddress</span> address) {
- <span class="keyword">if</span> (address.isLoopbackAddress()) <span class="keyword">return</span> <span class="string"><span class="delimiter">"</span><span class="content">I don't like locals!</span><span class="delimiter">"</span></span>;
- <span class="keyword">return</span> <span class="predefined-constant">null</span>;
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>Return null for no filtering, or a String with the reason if you want to filter this person.</p></div></div></div>
- <div class="sect1"><h2 id="discover-hosts">Discover hosts</h2><div class="sectionbody"><div class="paragraph"><p>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:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Client client = <span class="keyword">new</span> Client();
- <span class="predefined-type">List</span><<span class="predefined-type">InetAddress</span>> foundHosts = client.discoverHosts(<span class="integer">5050</span>, <span class="integer">5000</span>);</code></pre></div></div>
- <div class="paragraph"><p>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:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">for</span> (<span class="predefined-type">InetAddress</span> host : foundHosts) {
- client.connect(host.getCanonicalHostName(), <span class="integer">4040</span>, <span class="integer">5050</span>);
- }
- client.start();</code></pre></div></div>
- <div class="paragraph"><p>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.</p></div>
- <div class="paragraph"><p>This concludes the first tutorial. In the next tutorial, it’s time to send and listen for messages!</p></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2018-03-29 18:43:41 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
- apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
- indexName: 'jmonkeyengine',
- inputSelector: '#doc-search',
- debug: false // Set debug to true if you want to inspect the dropdown
- });</script></body></html>
|