snipt

Ctrl+h for KB shortcuts

Java

A java server that accepts connections

try {
    ServerSocket skServidor = new ServerSocket( port );
	System.out.println("[log: System armed on port "+port+"]");

	while (1==1) {
		Socket skCliente = skServidor.accept();
		System.out.println("[log: Connection accepted]");

		connect( skCliente ); 
		}
} catch( Exception ex ) { ex.printStackTrace(); }

/* Here what you want to do with new connection */
protected void connect( Socket skCliente ) {  }
https://snipt.net/embed/a12823dee848ffbf55771e0aed0f374d/
/raw/a12823dee848ffbf55771e0aed0f374d/
a12823dee848ffbf55771e0aed0f374d
java
Java
14
2019-08-25T16:47:52
True
False
False
/api/public/snipt/39891/
a-java-server-that-accepts-connections
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">try</span> <span class="o">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">ServerSocket</span> <span class="n">skServidor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ServerSocket</span><span class="o">(</span> <span class="n">port</span> <span class="o">);</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;[log: System armed on port &quot;</span><span class="o">+</span><span class="n">port</span><span class="o">+</span><span class="s">&quot;]&quot;</span><span class="o">);</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a> <span class="k">while</span> <span class="o">(</span><span class="mi">1</span><span class="o">==</span><span class="mi">1</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">Socket</span> <span class="n">skCliente</span> <span class="o">=</span> <span class="n">skServidor</span><span class="o">.</span><span class="na">accept</span><span class="o">();</span> </span><span id="L-7"><a name="L-7"></a> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;[log: Connection accepted]&quot;</span><span class="o">);</span> </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a> <span class="n">connect</span><span class="o">(</span> <span class="n">skCliente</span> <span class="o">);</span> </span><span id="L-10"><a name="L-10"></a> <span class="o">}</span> </span><span id="L-11"><a name="L-11"></a><span class="o">}</span> <span class="k">catch</span><span class="o">(</span> <span class="n">Exception</span> <span class="n">ex</span> <span class="o">)</span> <span class="o">{</span> <span class="n">ex</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span> <span class="o">}</span> </span><span id="L-12"><a name="L-12"></a> </span><span id="L-13"><a name="L-13"></a><span class="cm">/* Here what you want to do with new connection */</span> </span><span id="L-14"><a name="L-14"></a><span class="kd">protected</span> <span class="kt">void</span> <span class="nf">connect</span><span class="o">(</span> <span class="n">Socket</span> <span class="n">skCliente</span> <span class="o">)</span> <span class="o">{</span> <span class="o">}</span> </span></pre></div> </td></tr></table>
client/server, java, socket