### ServerSocket Communication

Example of a Communication between a ServerSocket and Client.
This example makes use of the class AsyncStreamsText as provided by B4J.
Steps
1. Client establishes a connection to the server (using client As Socket).
2. Text is send (using AsyncStreamsText) from the client to the server (using server As ServerSocket)
3. Server replies to the client.
B4J Server Code
`Sub Process_Globals
Private server As ServerSocket
Private astream As AsyncStreamsText
End Sub`
`Sub AppStart (Args() As String)
Log("Init Server. Listening to Port 54321")
server.Initialize(54321, "server")
server.Listen
StartMessageLoop
End Sub`
`Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Log("Connected RemoteAddress: " & NewSocket.RemoteAddress)
If astream.IsInitialized Then astream.Close
astream.Initialize(Me, "astreamtext", NewSocket.InputStream, NewSocket.OutputStream)
End If
Log("Server listening")
server.Listen
End Sub`
`Sub astreamtext_NewText(text As String)
Log("New Text received: " & text)
astream.Write("Replying: " & text & CRLF)
End Sub`
B4J Client Code
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private txtLogs As TextArea
Private client As Socket
Private astream As AsyncStreamsText
Private txtInput As TextField
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UTILITY")
MainForm.RootPane.LoadLayout("1")
MainForm.Show
client.Initialize("client")
client.Connect("127.0.0.1", 54321, 30000)
End Sub`
`Sub client_Connected (Successful As Boolean)
If Successful Then
astream.Initialize(Me, "astreamtext", client.InputStream, client.OutputStream)
txtLogs.Text = "connected"
Else
txtLogs.Text = "Error: " & LastException
End If
End Sub`
`Sub astreamtext_NewText(text As String)
txtLogs.Text = txtLogs.Text & CRLF & text
txtLogs.SetSelection (txtLogs.Text.Length, txtLogs.Text.Length)
End Sub`
`Sub txtInput_Action
If astream.IsInitialized Then
astream.Write(txtInput.Text & CRLF)
txtInput.SelectAll
End If
End Sub`
AsyncStreamsText.bas
Type=Class
Version=4
ModulesStructureVersion=1
B4J=true
@EndOfDesignText@
Event: NewText (Text As String)
Event: Terminated
'version: 1.00
'Class module
`Sub Class_Globals
Private mTarget As Object
Private mEventName As String
Private astreams As AsyncStreams
Public charset As String = "UTF8"
Private sb As StringBuilder
End Sub`
`Public Sub Initialize (TargetModule As Object, EventName As String, In As InputStream, out As OutputStream)
mTarget = TargetModule
mEventName = EventName
astreams.Initialize(In, out, "astreams")
sb.Initialize
End Sub`
'Sends the text. Note that this subroutine does not add End of Line (EOL) characters.
`Public Sub Write(Text As String)
astreams.Write(Text.GetBytes(charset))
End Sub`
`Private Sub astreams_NewData (Buffer() As Byte)
Dim newDataStart As Int = sb.Length
sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
Dim s As String = sb.ToString
Dim start As Int = 0
For i = newDataStart To s.Length - 1
Dim c As Char = s.CharAt(i)
If i = 0 And c = Chr(10) Then '\n...
start = 1 'might be a broken end of line character
Continue
End If
If c = Chr(10) Then '\n
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
start = i + 1
Else If c = Chr(13) Then '\r
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
i = i + 1
End If
start = i + 1
End If
Next
If start > 0 Then sb.Remove(0, start)
End Sub`
`Private Sub astreams_Terminated
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub`
`Private Sub astreams_Error
Log("error: " & LastException)
astreams.Close
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub`
`Public Sub Close
astreams.Close
End Sub`

Changed: , Category: Server, File: T6C246M1784.htm, ID: 1784