instance of ASP.net socket

Source: Internet
Author: User
Tags bind socket

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

)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.