• 大小: 0.63M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-01-30
  • 语言: VB
  • 标签: Socket  客户端  服务  c  工具  

资源简介

Socket 客户端与服务端对话工具




核心代码:

Private Sub RunServer()
        Dim ConClient As Socket
        Dim strClientName As String = ""
        Dim bCExist As Boolean    '客户端是否已经在列表中

        Dim ClientIpEP As IPEndPoint

        Dim CNetWorkStream As NetworkStream
        Dim Reader As BinaryReader

        Do
            bCExist = False
            ConClient = CTcpListener.AcceptSocket    '接收客户端的连接,返回一个socket

            SentButton.Enabled = True
            Try
                ClientIpEP = CType(ConClient.RemoteEndPoint, IPEndPoint) '获取socket的ipendpoint
                strClientName = Dns.GetHostEntry(ClientIpEP.Address).HostName  '获取客户端的名称
            Catch ex As SocketException
                ClientIpEP = Nothing
                MessageBox.Show(ex.Message)
                Continue Do
            End Try

            '检测该客户端是否已在列表中
            Dim strName As String = ""

            For Each strName In ClientListBox.Items
                If String.Compare(strClientName, strName) = 0 Then
                    bCExist = True
                    'Exit For '存在则退出查找
                End If
            Next

            ' 客户列表中添加客户
            If bCExist = False Then
                ClientListBox.Items.Add(strClientName)
                HistoryRichText.SelectionColor = Color.Green
                HistoryRichText.AppendText(strClientName & " 加入会话" & vbCrLf)
                HistoryRichText.ScrollToCaret()
                Continue Do
            End If

            Try
                CNetWorkStream = New NetworkStream(ConClient)
                Reader = New BinaryReader(CNetWorkStream)
                HistoryRichText.SelectionColor = Color.Blue
                HistoryRichText.AppendText(strClientName & "说" & vbCrLf)
                HistoryRichText.SelectionColor = Color.Black
                HistoryRichText.AppendText(Reader.ReadString() & vbCrLf)
                HistoryRichText.ScrollToCaret()
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                ClientIpEP = Nothing
                CNetWorkStream = Nothing
                Reader = Nothing
            End Try
            ConClient.Disconnect(True) ' 关闭套接字
        Loop
        System.Environment.Exit(System.Environment.ExitCode)
    End Sub

资源截图

代码片段和文件信息

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      90624  2015-10-26 14:46  ChartClient\Chart\bin\Debug\Chart.exe

     文件      38400  2015-10-26 14:46  ChartClient\Chart\bin\Debug\Chart.pdb

     文件      11608  2015-10-26 14:48  ChartClient\Chart\bin\Debug\Chart.vshost.exe

     文件        490  2010-03-17 22:39  ChartClient\Chart\bin\Debug\Chart.vshost.exe.manifest

     文件        620  2015-10-26 14:46  ChartClient\Chart\bin\Debug\Chart.xml

     文件       6286  2015-10-21 17:18  ChartClient\Chart\Chart.vbproj

     文件        478  2015-10-21 17:18  ChartClient\Chart\Chart.vbproj.user

     文件       4510  2015-10-26 14:46  ChartClient\Chart\frmChartclient.Designer.vb

     文件       5814  2015-10-26 14:46  ChartClient\Chart\frmChartclient.resx

     文件       6068  2015-10-26 14:46  ChartClient\Chart\frmChartclient.vb

     文件       1568  2015-10-21 17:18  ChartClient\Chart\My Project\Application.Designer.vb

     文件        478  2012-08-27 06:58  ChartClient\Chart\My Project\Application.myapp

     文件       1099  2012-08-27 06:58  ChartClient\Chart\My Project\AssemblyInfo.vb

     文件       3070  2015-10-21 17:18  ChartClient\Chart\My Project\Resources.Designer.vb

     文件       6199  2012-08-27 06:58  ChartClient\Chart\My Project\Resources.resx

     文件       2984  2015-10-21 17:18  ChartClient\Chart\My Project\Settings.Designer.vb

     文件        279  2012-08-27 06:58  ChartClient\Chart\My Project\Settings.settings

     文件        294  2012-08-27 06:58  ChartClient\Chart\obj\Chart.vbproj.FileList.txt

     文件      90624  2015-10-26 14:46  ChartClient\Chart\obj\Debug\Chart.exe

     文件        180  2015-10-26 14:46  ChartClient\Chart\obj\Debug\Chart.frmChartclient.resources

     文件      38400  2015-10-26 14:46  ChartClient\Chart\obj\Debug\Chart.pdb

     文件      68150  2015-10-21 17:23  ChartClient\Chart\obj\Debug\Chart.Resources.resources

     文件        891  2015-10-26 14:48  ChartClient\Chart\obj\Debug\Chart.vbproj.FileListAbsolute.txt

     文件       1019  2012-08-27 06:58  ChartClient\Chart\obj\Debug\Chart.vbproj.GenerateResource.Cache

     文件        620  2015-10-26 14:46  ChartClient\Chart\obj\Debug\Chart.xml

     文件       5440  2015-10-26 14:46  ChartClient\Chart\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       3698  2015-10-26 14:46  ChartClient\Chart\obj\Debug\ResGen.read.1.tlog

     文件       1020  2015-10-26 14:46  ChartClient\Chart\obj\Debug\ResGen.write.1.tlog

     文件       7680  2015-10-21 17:18  ChartClient\Chart\obj\Debug\TempPE\My Project.Resources.Designer.vb.dll

     文件      67665  2012-08-27 06:58  ChartClient\Chart\Resources\初夏.jpg

............此处省略58个文件信息

评论

共有 条评论