WebService Simple.
Como hacer un servicio Web muy simple, en unas pocas líneas de código.
' Gambas class file
Private $iId As Integer
Private CantiMax As Integer
Public Sub Form_Open()
CantiMax = 100
' define options for MyServerSocket object
MyServerSocket.Type = Net.Internet
MyServerSocket.Port = 33333
MyServerSocket.Listen(100)
End
Public Sub MyServerSocket_Connection(sHost As String)
Dim Obj As Socket
If MyServerSocket.Status <= Net.Inactive Then Return txtLog.Text = txtLog.Text & "Connection request from : " & sHost & Chr(13) Obj = MyServerSocket.Accept() Obj.Blocking = False Inc $iId Obj.Tag = [$iId, 0, ""] If Obj.Status = Net.Connected Then txtLog.Text = txtLog.Text & "Connection from " & Obj.RemoteHost & ":" & Obj.RemotePort & " accepted (local port " & Obj.LocalPort & ")" & Chr(13) End If End Public Sub Socket_Read() Dim sBuf As String If Last.Status <> Net.Connected Then Return
Read #Last, sBuf, Lof(Last)
txtLog.Text &= "Socket #" & Last.Tag[0] & " --> " & sBuf & "\n"
Last.Tag[1] = 0
Last.Tag[2] = sBuf
Socket_Write()
End
Public Sub Socket_Write()
Dim hSocket As Socket = Last
Dim iInd As Integer
iInd = hSocket.Tag[1]
If iInd < 0 Then Return Do Inc iInd If iInd > 1 Then
hSocket.Tag[1] = -1
Return
Endif
Try Print #hSocket, iInd & ":" & hSocket.Tag[2] & "\n";
If Error Then
Debug Error.Text
Break
Endif
Loop
hSocket.Tag[1] = iInd
Catch
End
Public Sub Form_Close()
MyServerSocket.Close()
End
Public Sub MyServerSocket_Error()
Message.Error("Unable to bind socket")
End
Public Sub Button1_Click()
MyServerSocket.Close()
End
Public Sub Button2_Click()
MyServerSocket.Listen(100)
End
Comentarios
Publicar un comentario