ASP tutorials Instance of. NET Socket
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
public class Frmserver
Dim s As Socket = Nothing
Dim t As Thread
Public Sub Waitdata ()
s = new socket ( AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp) ' Using the TCP protocol
Dim localendpoint As New IPEndPoint (Ipaddress.parse ("192.168.1.6"), 1024) ' Specify IP and port
S.bind (localendpoint) ' bind to this socket
S.listen ' Listening, accepting up to 100 connections
while (true)
Dim bytes (1024) As byte is used to store received bytes
Dim ss As Socket = s.accept () ' If received, create a new socket to connect to it
ss.receive (bytes) ' receive data, if using Ss.send (Byte ()), Sends the data
Dim str = encoding.unicode.getstring (bytes)
lstmessage.items.insert (0, str) Insert it before the first item in the list box
Select Case STR
case "Listuser"
str = "Listuserok"
End Select
bytes = Encoding.unicode.getbytes (str)
Ss.send (bytes)
Ss.close () ' ============= do I need to close it here?
' May I ask the question to be comparatively elementary hehe ... The next question is how the client should receive it?
End While
End Sub
Private Sub btnStart_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) handles Btnstart.click
t = new Thread (AddressOf Waitdata) ' to create a newly threaded
T.start () ' Start thread
Btnstart.enabled = False
End Sub
Private Sub Btnend_click (ByVal sender As System.Object, ByVal e As System.EventArgs) handles Btnend. Click
Try
s.close () ' Close socket
T.abort () abort thread
catch
finally
Btnstart.enabled = True ' Enable Btnstart
End Try
end Sub
Private Sub Frmserver_formclosing (ByVal sender As Object, ByVal e As System.windows.forms.formclosingeventargs) handles M E.formclosing
Try
S.close ()
T.abort ()
Catch
End Try
End Sub
End Class
Example Two
Dim s As Socket = Nothing
Dim t As Thread
Public Sub Waitdata ()
s = new socket ( AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp) ' Using the TCP protocol
Dim localendpoint As New IPEndPoint (Ipaddress.parse ("192.168.1.6"), 1024) ' Specify IP and port
S.bind (localendpoint) ' bind to this socket
S.listen ' Listening, accepting up to 100 connections
while (true)
Dim bytes (1024) As byte is used to store received bytes
Dim ss As Socket = s.accept () ' If received, create a new socket to connect to it
ss.receive (bytes) ' Receive data
Dim str=encoding.unicode.getstring (bytes)
' =========================================
' Process data
str= "xxx|xxx"
How can I then send back the processed data to the client
' =================== ======================
End (
End Sub
)