Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Network/Raw Socket/RawSocket accept
Function:
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Network/Raw Socket/RawSocket accept
This example is the version from Sun, 15th Sep 2018.
Project "RawSocket accept.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open() ServerSocket = new MyServerSocket(MyServerSocket.AddressFamilyINet, MyServerSocket.SocketTypeStream, MyServerSocket.ProtocolTCP) // set options ServerSocket.ReuseAddress = true ServerSocket.ReusePort = true call ServerSocket.Bind(8000) dim e as integer = ServerSocket.Lasterror if e <> 0 then break else log "bound to port "+str(ServerSocket.LocalPort)+" on "+ServerSocket.LocalIP end if ServerSocket.Listen e = ServerSocket.Lasterror if e <> 0 then break else log "Listening..." end if End EventHandler
Property Clients() As MyClientSocket
Property ServerSocket As MyServerSocket
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyServerSocket Inherits RAWSocketMBS
EventHandler Sub DataAvailable() dim a as MyClientSocket do dim RemoteAddress as string dim RemotePort as integer dim c as new MyClientSocket(MyClientSocket.AddressFamilyINet, MyClientSocket.SocketTypeStream, MyClientSocket.ProtocolTCP) dim v as Variant = me.Accept(RemoteAddress, RemotePort, c) a = v if a <> nil then log "Accepted connection from "+RemoteAddress+" on port "+str(RemotePort) MainWindow.Clients.Append a end if loop until a = nil End EventHandler
EventHandler Sub Error() log CurrentMethodName End EventHandler
End Class
Class MyClientSocket Inherits RAWSocketMBS
EventHandler Sub DataAvailable() // read what is coming dim s as string = me.ReadAll log "Received "+str(len(s))+" bytes" // we always answer here wiht HTTP answer dim lines( )as string lines.append "HTTP/1.0 200 OK" lines.append "Date: Sat, 28 Nov 2009 04:36:25 GMT" lines.append "Server: test" lines.append "Connection: close" lines.append "Content-Type: text/plain" lines.Append "" lines.Append "Hello World" dim m as MemoryBlock = Join(lines,EndOfLine.Windows) call me.Send(m, m.size) // close connection, so browser doesn't wait me.Close End EventHandler
EventHandler Sub Error() break End EventHandler
End Class
Module UtilModule
Sub Log(s as string) MainWindow.List.AddRow s End Sub
End Module
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Network Plugin.




Links
MBS Xojo Plugins