专栏首页VB小源码VB.NET 服务端源码

VB.NET 服务端源码

Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.Net.NetworkInformation Imports System.Text Imports System.Diagnostics Imports System.IO

Public Class Frm_tcp Dim Socketlisten As Socket '服务器Socket Dim b4aSocket As Socket '客户端Socket Dim Socketst As Boolean = False 'Socket状态 Dim listenThread As Thread 'Socket侦听进程

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    exit_listen()
    End
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    txt_ip.Text = Get_Ip()
    txt_prot.Text = 8880
    create_listen()
End Sub
Public Sub Listenb4a()
    Dim temp() As Byte = New [Byte](51200) {} '定义字节数组
    Dim get_data As String = ""
    Dim arr() As String
    Dim localEndPoint As New IPEndPoint(Net.IPAddress.Parse(txt_ip.Text), txt_prot.Text)
    Socketlisten = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)  '初始化socket
    Socketlisten.Bind(localEndPoint) '绑定端口
    Socketlisten.Listen(10) '开始监听
    '------------------------------------
    b4aSocket = Socketlisten.Accept
    Socketst = True
    While True
        Dim bytesRec As Integer = b4aSocket.Receive(temp)
        get_data = Encoding.UTF8.GetString(temp, 0, bytesRec)
        arr = get_data.Trim.Split("|")
        If arr(0) <> "" Then
            BeginInvoke(New EventHandler(AddressOf addlist), arr(0)) 'Invoke保证线程安全
        Else
            exit_listen()
            Socketst = False
        End If
    End While
End Sub

Sub addlist(ByVal sender As System.Object, ByVal e As System.EventArgs) '保证线程安全
    get_list.Items.Add(sender.ToString)
End Sub
Sub addtxt(ByVal sender As System.Object, ByVal e As System.EventArgs) '保证线程安全
    bt_st.Text = (sender.ToString)
End Sub
'退出侦听
Sub exit_listen()
    If Socketst = True Then
        Dim msg As Byte() = Encoding.UTF8.GetBytes("服务端退出....")
        Dim bytesSent As Integer = b4aSocket.Send(msg)
        BeginInvoke(New EventHandler(AddressOf addtxt), "停止侦听")
        Socketst = False
    End If
    Socketlisten.Close()
    b4aSocket = Nothing
    listenThread.Abort()
    bt_st.Text = "停止侦听"
End Sub
Sub create_listen() '创建侦听
    listenThread = New Thread(AddressOf Listenb4a)  '创建侦听新进程
    listenThread.Start()  '侦听进程开始
    bt_st.Text = "正在侦听"
End Sub
'获取本机IP地址
Public Function Get_Ip() As String
    Dim hostName As String = System.Net.Dns.GetHostName.ToString
    Dim IPString As String = "127.0.0.1"
    Dim ip As System.Net.IPAddress
    For Each ip In System.Net.Dns.GetHostEntry(hostName).AddressList()
        If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
            IPString = ip.ToString
            Exit For
        End If
    Next
    Return IPString
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_st.Click
    If listenThread.ThreadState = 0 Then
        exit_listen()
    ElseIf listenThread.ThreadState > 0 Then
        create_listen()
    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tmp As String = ""
    Dim path As String = "C:\Users\" & Environment.UserName & "\Desktop\手机扫码枪记录.txt"
    Dim i As Integer = 0
    While i < get_list.Items.Count
        tmp += get_list.Items(i) + vbCrLf
        i += 1
    End While
    Dim sw As New StreamWriter(path)
    sw.Write(tmp)
    sw.Flush()
    sw.Close()
    MessageBox.Show("记录导出成功!" & vbCrLf & "保存路径: " & path, "手机扫码枪")
End Sub

End Class

本文分享自微信公众号 - VB小源码(vb_xym),作者:VB小源码

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-01-18

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • vb6.0 FTP文件下载

    Inet1.AccessType = icUseDefault '设置与Internet连接的类型,默认值

    巴西_prince
  • VBA批量获取文件夹和文件名

    With Application.FileDialog(msoFileDialogFolderPicker)

    巴西_prince
  • VBA清空所有超链接

    巴西_prince
  • 推荐系列(一):什么是推荐?

    YouTube如何知道你可能希望接下来要观看的视频?Google Play商店如何为你挑选应用?这些恰到好处的推荐是魔法吗?答案当然是不,上述情况下,基于机器学...

    用户3578099
  • [实用]VB.NET PDF文件批量打印

    VB.NET PDF文件批量打印,可以指定打印的页面,倒叙打印,顺序打印!方便快捷!

    巴西_prince
  • 第二十九章:基于SpringBoot平台使用Lombok来优雅的编码

    恒宇少年
  • 日本关东地区政府采用OCR+RPA给政务提速

    不得不承认,在日本不管企业还是政府,对RPA的重视程度比其他国家高,所以分享的案例也比其他的国家多一些,这个案例是介绍的是横手市政府,今天介绍的案例是日本千叶市...

    RPA小葵
  • Web网站通知系统设计

    写在前面: 通知系统是网站信息传播机制的重要的一部分,足够写一大章来说明。本文只梳理设计原则,后续相关内容会持续更新。 这里的通知包括但不限于公告、提醒或消息(...

    wblearn
  • Spring和Ldap整合详解

    LDAP(Light Directory Access Portocol),它是基于X.500标准的轻量级目录访问协议。

    品茗IT
  • 前端进程间通讯的渗透之术

    父页面和子页面(iframe)处于2个不同的进程(即使在同一个域),进程之间的通讯必须通过每个进程内【事件监听所在的线程】来完成。最重要的是,通讯时传输的数据格...

    Jean

扫码关注云+社区

领取腾讯云代金券