Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation
Components
gb
gb.args
gb.cairo
gb.chart
gb.clipper
gb.complex
gb.compress
gb.crypt
gb.data
gb.db
gb.db.form
gb.db.mysql
gb.db.odbc
gb.db.postgresql
gb.db.sqlite2
gb.db.sqlite3
gb.dbus
gb.dbus.trayicon
gb.debug
gb.desktop
gb.desktop.gnome.keyring
gb.desktop.x11
gb.eval
gb.eval.highlight
gb.form
gb.form.dialog
gb.form.editor
gb.form.htmlview
gb.form.mdi
gb.form.print
gb.form.stock
gb.form.terminal
gb.gmp
gb.gsl
gb.gtk
gb.gtk.opengl
gb.gtk3
gb.gtk3.opengl
gb.gtk3.webview
gb.gui
gb.gui.opengl
gb.gui.qt
gb.gui.qt.opengl
gb.gui.qt.webkit
gb.gui.trayicon
gb.gui.webview
gb.hash
gb.highlight
gb.image
gb.image.effect
gb.image.imlib
gb.image.io
gb.inotify
gb.jit
gb.libxml
gb.logging
gb.map
gb.markdown
gb.media
gb.media.form
gb.memcached
gb.mime
gb.mysql
gb.ncurses
gb.net
.UdpSocket.Multicast
DnsClient
Net
SerialPort
ServerSocket
Socket
_new
Closed
Connect
Error
Found
Host
LocalHost
LocalPort
Path
Peek
Port
Read
Ready
RemoteHost
RemotePort
Server
Status
Timeout
Write
UdpSocket
gb.net.curl
gb.net.pop3
gb.net.smtp
gb.openal
gb.opengl
gb.opengl.glsl
gb.opengl.glu
gb.opengl.sge
gb.openssl
gb.option
gb.pcre
gb.pdf
gb.poppler
gb.qt4
gb.qt4.ext
gb.qt4.opengl
gb.qt4.webkit
gb.qt4.webview
gb.qt5
gb.qt5.ext
gb.qt5.opengl
gb.qt5.webkit
gb.qt5.webview
gb.report
gb.report2
gb.scanner
gb.sdl
gb.sdl.sound
gb.sdl2
gb.sdl2.audio
gb.settings
gb.signal
gb.term
gb.test
gb.util
gb.util.web
gb.v4l
gb.vb
gb.web
gb.web.feed
gb.web.form
gb.web.gui
gb.xml
gb.xml.html
gb.xml.rpc
gb.xml.xslt
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
Wiki License
Wiki Manual

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 creatable.

Properties
Host   The host to which you would like to connect.
LocalHost   Once a TCP connection is established, this property reflects local IP used by this connection.
LocalPort   Once a TCP connection is established, this property reflects local port (1-65535) used by this connection.
Path   Return or set the path of the local socket you want to connect.
Port   The port to which you would like to connect on the remote host.
RemoteHost   Once a TCP connection is established, this property reflects remote IP used by this connection.
RemotePort   Once a TCP connection is established, this property reflects remote port (1-65535) used by this connection.
Server   If that socket has been created by the Accept method of a ServerSocket, then return that ServerSocket object.
Status   Reflects the current status of a Socket object.
StatusText  
Timeout   Return or set the timeout of the socket, in milliseconds.

Inherited properties
Blocking   Returns or sets if the stream is blocking.
ByteOrder   Returns or sets the byte order used to read or write binary data to the stream.
EndOfFile   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.
EndOfLine   Return or set the newline separator used by this stream.
Eof   Return if a stream reached its end.
Handle   Returns the system file descriptor associated with this Stream.
IsTerm   Return if a stream is associated with a terminal.
Lines   Returns a virtual object that allows you to enumerate a stream line by line.
NoShare  
NullTerminatedString   Return or set if strings are null-terminated when they are serialized.
Tag   Returns or sets the tag associated with the stream.
Term   Return a virtual object that allows to manage the terminal associated with the stream.

Methods
Connect   Use this method to establish a TCP or Local (Unix) connection with a remote TCP or Local server.
Peek   This function allows you to receive data from Socket.

Inherited methods
Begin   Start to buffer data written to the stream, so that everything will be sent when the Send method is called.
Close   Closes the stream.
Drop   Drop the data that has been buffered since the last call to the Begin method.
ReadLine   Read a line of text from the stream, like the LINE INPUT instruction.
Send   Send all the data, in one shot, since the last call to Begin.
Watch   Start or stop watching the stream file descriptor for reading or writing, after it has been opened.

Events
Closed   This event raises after the remote side of the socket has closed the connection.
Error   This event raises when some failed (for instance remote host refused connection).
Found   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
Read   This event raises when data arrives to the socket from the other side of connection.
Ready   This event raises after a connection has been established successfully.
Write   This event is raised when the internal socket send buffers can take in more data.

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.