Socket (gb.net)

This class implements a socket client to allow your programs to connect with socket servers. TCP and Local (Unix sockets) connections are implemented.

This class inherits Stream in gb.
This class is
Creates a new Socket.
creatable
.

Properties

Methods Events
Returns or sets if the stream is blocking.
Blocking  
Returns or sets the byte order used to read or write binary data to the stream.
ByteOrder  
This property signals whether the last use of LINE INPUT reached the end of file, instead of reading a full line with an end-of-line character.
EndOfFile  
Returns or sets the newline separator used by this stream.
EndOfLine  
Returns the system file descriptor associated with this Stream.
Handle  
The host to which you would like to connect.
Host  
Return if a stream is associated with a terminal.
IsTerm  
Returns a virtual object that allows you to enumerate a stream line by line.
Lines  
Once a TCP connection is established, this property reflects local IP used by this connection.
LocalHost  
Once a TCP connection is established, this property reflects local port (1-65535) used by this connection.
LocalPort  
Return or set the path of the local socket you want to connect.
Path  
The port to which you would like to connect on the remote host.
Port  
Once a TCP connection is established, this property reflects remote IP used by this connection.
RemoteHost  
Once a TCP connection is established, this property reflects remote port (1-65535) used by this connection.
RemotePort  
If that socket has been created by the Accept method of a ServerSocket, then return that ServerSocket object.
Server  
Reflects the current status of a Socket object.
Status  
Returns or sets the tag associated with the stream.
Tag  
Return a virtual object that allows to manage the terminal associated with the stream.
Term  
Return or set the timeout of the socket, in milliseconds.
Timeout  
Start to buffer data written to the stream, so that everything will be sent when the Send method is called.
Begin  
Closes the stream.
Close  
Use this method to establish a TCP or Local (Unix) connection with a remote TCP or Local server.
Connect  
Drop the data that has been buffered since the last call to the Begin method.
Drop  
This function allows you to receive data from Socket.
Peek  
Read a line of text from the stream, like the LINE INPUT instruction.
ReadLine  
Send all the data, in one shot, since the last call to Begin.
Send  
Start or stop watching the stream file descriptor for reading or writing, after it has been opened.
Watch  
This event raises after the remote side of the socket has closed the connection.
Closed  
This event raises when some failed (for instance remote host refused connection).
Error  
When connecting using TCP sockets, first step is to translate host name to host IP. The object will raise this event when this part of the connection process has been finished
Found  
This event raises when data arrives to the socket from the other side of connection.
Read  
This event raises after a connection has been established successfully.
Ready  
This event is raised when the socket is ready for writing.
Write  

This class performs its work asynchronously, so the program will not be stopped while connecting, sending or receiving data.

This class is derived from the class Stream, so you can use standard Stream & Input/Output functions to send and receive data, and to close the socket.

Sockets can be used if the library "gb.net" is included in the project. To include this library use the menu [Project] [Properties] [Components] and tick the component "gb.net".

Example

' Gambas class file

Public MySock As Socket

Public Sub Button1_Click()

  Dim sBuf As String

  MySock = New Socket
  MySock.Connect("localhost", 7000)

  Do While (MySock.Status <> Net.Connected) And (MySock.Status > 0)
    Wait 0.1
  Loop

  If MySock.Status <> Net.connected Then
    Print "Error"
    Quit
  End If

  sBuf = "Hello over there.\n"
  Write #MySock, sBuf, Len(sBuf)

  Do While Lof(MySock) = 0
    Wait 0.1
  Loop
  Read #MySock, sBuf, Lof(MySock)
  Print sBuf

  Close #MySock

End

Remarks on that example:

  • Even though polling is shown here, you should really use callbacks to react on different states.

  • See Network Programming, the tutorial about the usage of networking. There are comments to each line of this example.

  • There is another example in the distribution under Networking: ClientSocket.