当前位置:编程学习 > C#/ASP.NET >>

asp.net socket的实例

asp教程.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) '使用tcp协议
        dim localendpoint as new ipendpoint(ipaddress.parse("192.168.1.6"), 1024) '指定ip和port
        s.bind(localendpoint)  '绑定到该socket
        s.listen(100)  '侦听,最多接受100个连接
        while (true)
            dim bytes(1024) as byte '用来存储接收到的字节
            dim ss as socket = s.accept() '若接收到,则创建一个新的socket与之连接
            ss.receive(bytes)  '接收数据,若用ss.send(byte()),则发送数据

            dim str = encoding.unicode.getstring(bytes)
            lstmessage.items.insert(0, str) '将其插入到列表框的第一项之前
            select case str
                case "listuser"
                    str = "listuserok"
            end select

            bytes = encoding.unicode.getbytes(str)
            ss.send(bytes)
            ss.close() '=============这里需要用close关闭吗?
              '可能我问的问题比较初级呵呵... 下一个问题是客户端应该怎么接收?
        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) '建立新的线程
        t.start()  '启动线程
        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()  '关闭socket
            t.abort()  '中止线程
        catch
        finally
            btnstart.enabled = true '启用btnstart
        end try
    end sub

    private sub frmserver_formclosing(byval sender as object, byval e as system.windows.forms.formclosingeventargs) handles me.formclosing
        try
            s.close()
            t.abort()
        catch
        end try
    end sub
end class

//实例二

dim s as socket = nothing
    dim t as thread

    public sub waitdata()
        s = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp) '使用tcp协议
        dim localendpoint as new ipendpoint(ipaddress.parse("192.168.1.6"), 1024) '指定ip和port
        s.bind(localendpoint)  '绑定到该socket
        s.listen(100)  '侦听,最多接受100个连接
        while (true)
            dim bytes(1024) as byte '用来存储接收到的字节
            dim ss as socket = s.accept() '若接收到,则创建一个新的socket与之连接
            ss.receive(bytes)  '接收数据
            dim str=encoding.unicode.getstring(bytes)

          '=========================================
            '处理数据
            str="xxx|xxx"
          '请问然后怎么将处理过后的数据发回给客户端
            '=========================================
           
        end while
    end sub

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,