snipt

Ctrl+h for KB shortcuts

Java

A Java client that connects and send nick, gps position and hello message

try {
    Socket skCliente = new Socket( "127.0.0.1" , port );

    OutputStream aux = skCliente.getOutputStream();
    DataOutputStream flujo= new DataOutputStream( aux );

    InputStream auxin = skCliente.getInputStream();
    DataInputStream flujoin= new DataInputStream( auxin );

    flujo.write( new String(" { msg: '0', nick:'Client "+id+"', gps: ['3333','55555'] }\n").getBytes() );
    flujo.write( new String(" { msg:'3', text:'Holaps "+id+"' }\n").getBytes() );

    flujo.flush();

    while (true) {
		System.out.println( "[log: Cliente "+id+":"+flujoin.readLine()+"]" );
		}
} catch( Exception ex ) { ex.printStackTrace(); }
https://snipt.net/embed/d667090ddafd3b9a6dc673e2fec77f30/
/raw/d667090ddafd3b9a6dc673e2fec77f30/
d667090ddafd3b9a6dc673e2fec77f30
java
Java
18
2019-06-26T12:58:09
True
False
False
/api/public/snipt/39890/
a-java-client-that-connects-and-send-nick-gps-position-and-hello-message
<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> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</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">Socket</span> <span class="n">skCliente</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Socket</span><span class="o">(</span> <span class="s">&quot;127.0.0.1&quot;</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><span id="L-4"><a name="L-4"></a> <span class="n">OutputStream</span> <span class="n">aux</span> <span class="o">=</span> <span class="n">skCliente</span><span class="o">.</span><span class="na">getOutputStream</span><span class="o">();</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">DataOutputStream</span> <span class="n">flujo</span><span class="o">=</span> <span class="k">new</span> <span class="n">DataOutputStream</span><span class="o">(</span> <span class="n">aux</span> <span class="o">);</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a> <span class="n">InputStream</span> <span class="n">auxin</span> <span class="o">=</span> <span class="n">skCliente</span><span class="o">.</span><span class="na">getInputStream</span><span class="o">();</span> </span><span id="L-8"><a name="L-8"></a> <span class="n">DataInputStream</span> <span class="n">flujoin</span><span class="o">=</span> <span class="k">new</span> <span class="n">DataInputStream</span><span class="o">(</span> <span class="n">auxin</span> <span class="o">);</span> </span><span id="L-9"><a name="L-9"></a> </span><span id="L-10"><a name="L-10"></a> <span class="n">flujo</span><span class="o">.</span><span class="na">write</span><span class="o">(</span> <span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">&quot; { msg: &#39;0&#39;, nick:&#39;Client &quot;</span><span class="o">+</span><span class="n">id</span><span class="o">+</span><span class="s">&quot;&#39;, gps: [&#39;3333&#39;,&#39;55555&#39;] }\n&quot;</span><span class="o">).</span><span class="na">getBytes</span><span class="o">()</span> <span class="o">);</span> </span><span id="L-11"><a name="L-11"></a> <span class="n">flujo</span><span class="o">.</span><span class="na">write</span><span class="o">(</span> <span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">&quot; { msg:&#39;3&#39;, text:&#39;Holaps &quot;</span><span class="o">+</span><span class="n">id</span><span class="o">+</span><span class="s">&quot;&#39; }\n&quot;</span><span class="o">).</span><span class="na">getBytes</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="n">flujo</span><span class="o">.</span><span class="na">flush</span><span class="o">();</span> </span><span id="L-14"><a name="L-14"></a> </span><span id="L-15"><a name="L-15"></a> <span class="k">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-16"><a name="L-16"></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: Cliente &quot;</span><span class="o">+</span><span class="n">id</span><span class="o">+</span><span class="s">&quot;:&quot;</span><span class="o">+</span><span class="n">flujoin</span><span class="o">.</span><span class="na">readLine</span><span class="o">()+</span><span class="s">&quot;]&quot;</span> <span class="o">);</span> </span><span id="L-17"><a name="L-17"></a> <span class="o">}</span> </span><span id="L-18"><a name="L-18"></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></pre></div> </td></tr></table>
client/server, java, socket