<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../style.css">
<title>
Gambas Documentation - Socket
 (gb.net)</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../../help+en"><img class="flag" alt="Home" border="0" src="../../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../gb.net+en">Up</a>&nbsp;
<a href="serversocket/_new+en">Previous</a>&nbsp;
<a href="socket/_closed+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
Socket
 (gb.net)</h1>
This class implements a socket client to allow your programs to connect with socket servers. TCP and Local (Unix sockets) connections are implemented.
<p>
<div class="black"><font size="-2"><b>Symbols</b></font></div>
<div class="border">
This class inherits <a href="../gb/stream+en">Stream</a> in <a href="../gb+en">gb - Gambas Internal native classes</a>.<hr>
This class is <a class="none" href="socket/_new+en">creatable</a>.<br>
<hr><table class="none" cellspacing="0" cellpadding="0">
<tr>
<td align=left><b>Properties</b></td><td width="32">&nbsp;</td>
<td align=left><b>Methods</b></td><td width="32">&nbsp;</td>
<td align=left><b>Events</b></td><td width="32">&nbsp;</td>
</tr>
<tr valign=top>
<td>
<a class="none" href="../gb/stream/byteorder+en">ByteOrder</a>&nbsp;&nbsp; <a class="none" href="../gb/stream/endofline+en">EndOfLine</a>&nbsp;&nbsp; <a class="none" href="../gb/stream/handle+en">Handle</a>&nbsp;&nbsp; <a class="none" href="socket/host+en">Host</a>&nbsp;&nbsp; <a class="none" href="../gb/stream/id+en">Id</a>&nbsp;&nbsp; <a class="none" href="socket/localhost+en">LocalHost</a>&nbsp;&nbsp; <a class="none" href="socket/localport+en">LocalPort</a>&nbsp;&nbsp; <i>/comp/gb.net/socket/path</i>&nbsp;&nbsp; <a class="none" href="socket/port+en">Port</a>&nbsp;&nbsp; <a class="none" href="socket/remotehost+en">RemoteHost</a>&nbsp;&nbsp; <a class="none" href="socket/remoteport+en">RemotePort</a>&nbsp;&nbsp; <a class="none" href="socket/status+en">Status</a>&nbsp;&nbsp; </td><td>&nbsp;</td>
<td>
<a class="none" href="../gb/stream/close+en">Close</a>&nbsp;&nbsp; <a class="none" href="socket/connect+en">Connect</a>&nbsp;&nbsp; <a class="none" href="socket/peek+en">Peek</a>&nbsp;&nbsp; </td><td>&nbsp;</td>
<td>
<a class="none" href="socket/_closed+en">Closed</a>&nbsp;&nbsp; <a class="none" href="socket/_error+en">Error</a>&nbsp;&nbsp; <a class="none" href="socket/_found+en">Found</a>&nbsp;&nbsp; <a class="none" href="socket/_read+en">Read</a>&nbsp;&nbsp; <a class="none" href="socket/_ready+en">Ready</a>&nbsp;&nbsp; </td><td>&nbsp;</td>
</tr>
</table>
</div><p>

This class performs its work asynchronously, so the program will not be stopped while connecting, sending or receiving data.
<p>
This class is a derived from the class <a href="../gb/stream+en">Stream</a>, so you can use standard <a href="../../cat/stream+en">Stream &amp; Input/Output functions</a> to send and receive data, and to close the socket.
<p>
Sockets can be used if the library &quot;gb.net&quot; is included in the project.
To include this library use the menu [Project] [Properties] [Components] tick the component &quot;gb.net&quot;.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">' Gambas class file

PUBLIC MySock AS Socket

PUBLIC SUB Button1_Click()
DIM sBuf AS String

MySock = NEW Socket
MySock.Connect(&quot;localhost&quot;, 7000)

DO WHILE (MySock.Status &lt;&gt; 7) AND (MySock.Status &gt; 0)
  WAIT 0.1
LOOP

IF MySock.Status &lt;&gt; 7 THEN
  PRINT &quot;Error&quot;
  QUIT
END IF

sBuf = &quot;Hello over there.n&quot;
WRITE #MySock, sBuf, Len(sBuf)

DO WHILE Lof(MySock) = 0
  WAIT 0.1
LOOP
READ #MySock, sBuf, Lof(MySock)
PRINT sBuf

CLOSE #MySock

END</pre>
<p>
<h4>Remarks to this Example</h4>
<ul>
<li>Even if here the polling is shown, you should rather use callbacks to react on different states.
<li>See <a href="../../doc/network+en">Network Programming</a>, the tutorial about the usage of networking. There are comments to each line of this example.
<li>There is another example in the distribution under Networking ClientSocket.
</ul>

</div>
</body>
</html>

