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.ext
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.qt6
gb.qt6.ext
gb.qt6.opengl
gb.qt6.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

Event Socket.Write (gb.net)

Event Write ( )

This event is raised when the internal socket send buffers can take in more data.

This event is most useful if you want to send a large file. The Write event is not raised continuously (as a socket is almost always ready to take in data). You have to signal to Gambas that you have some data to write by doing an initial Write to the socket. After the initial write, the Write event will be raised continuously for as long as you write more data from the Write event handler.

Example

The following is an example of serving a large static file via HTTP. Since the socket's send buffers in kernel space are only a few kilobytes big, the data should be written in chunks to avoid blocking for too long. The kernel takes care of sending the data and notifies you with a Write event when more space is available. The overall memory consumption remains tiny, the whole file is never loaded into memory entirely. The HTTP header serves as the initial write.

Private $hServer As ServerSocket

Public Sub Main()
  $hServer = New ServerSocket As "Server"
  $hServer.Type = Net.Internet
  $hServer.Port = 8080
  $hServer.Listen
End

Public Sub Server_Connection(RemoteHostIP As String)
  $hServer.Accept()
End

Public Sub Socket_Read()
  Dim sBuf As String
  Dim sResponse As String

  ' Assemble and parse the HTTP request here...
  sBuf = Read #Last, -4096

  ' Just always send that one large file
  sResponse = Subst$("HTTP/1.1 200 OK\r\nContent-Length: &1\r\n\r\n", Stat("large_file.txt").Size)
  Write #Last, sResponse, Len(sResponse)
  ' The above Write kicks off a cycle of Write events which consume the stream
  ' registered in the socket's Tag property.
  Last.Tag = Open "large_file.txt" For Read
End

Public Sub Socket_Write()
  Dim hFile As Stream = Last.Tag
  Dim sBuf As String

  If Not Eof(hFile) Then
    ' Read a chunk of at most 4KiB from the file and send it to the socket.
    sBuf = Read #hFile, -4096
    Write #Last, sBuf
  Endif
  ' Once we didn't write anything, the cycle of Write events will stop.
End