| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
大量收QQ微信精准粉/交友粉,非诚勿扰千里之行 始于足下
共有 1127 人关注过本帖
标题:关于TCP通讯服务端与多个客户端进行连接
只看楼主 加入收藏
wjwokao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-1-30
结帖率:0
  已结贴   问题点数:20  回复次数:3   
关于TCP通讯服务端与多个客户端进行连接
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '指定ip和端口
        Dim ipAddress As System.Net.IPAddress = Nothing
        System.Net.IPAddress.TryParse(TextBox1.Text, ipAddress)
        Dim localEndPoint As New System.Net.IPEndPoint(ipAddress, TextBox2.Text)

        服务器端 = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        服务器端.Bind(localEndPoint)
        服务器端.Listen(10)

        监听线程 = New Threading.Thread(AddressOf 监听)
        监听线程.Start()

    End Sub

    Public Sub 监听()
        While True
            连接(连接编号) = 服务器端.Accept()
            TCP线程(连接编号) = New Threading.Thread(AddressOf 接收)
            TCP线程(连接编号).Start()
        End While
    End Sub

    Public Sub 接收()
        Dim 编号 As Byte = 连接编号
        '连接编号 = 连接编号 + 1
        While True
            Dim bytesRec As Integer = 连接(编号).Receive(bytes) '接收数据
            If bytesRec > 0 Then
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec)
            End If
        End While
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If data <> Nothing Then
            ListBox1.Items.Insert(0, data)
            data = Nothing
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox1.Items.Clear()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        On Error Resume Next
        监听线程.Abort()
        For i As Byte = 0 To 255
            TCP线程(i).Abort()
            连接(i).Shutdown(Net.Sockets.SocketShutdown.Both)
            连接(i).Close()
        Next
        服务器端.Shutdown(Net.Sockets.SocketShutdown.Both)
        服务器端.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        On Error Resume Next
        Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox3.Text)
        Dim bytesSent As Integer = 连接(连接编号).Send(msg) '发送数据
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        On Error Resume Next

        监听线程.Abort()
        For i As Byte = 0 To 255
            TCP线程(i).Abort()
            连接(i).Shutdown(Net.Sockets.SocketShutdown.Both)
            连接(i).Close()
        Next
        服务器端.Shutdown(Net.Sockets.SocketShutdown.Both)
        服务器端.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
2018-01-30 19:14
wjwokao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-1-30
  得分:0 
  目前这段代码只能实现,一对一。。。如何从这段代码里实现一对多??请教大神
2018-01-30 19:15
zcsor
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:19
专家分:105
注 册:2018-2-5
  得分:20 
又一个都是中文变量名的,不累么。
这个东西的思路是这样的,自己想想去:
对于服务器来说:
1、服务器监听时建立了一个套接字,这个套接字就是用来监听的,不干别的,不是拿来给你通讯的。
2、在有连接时,在回调中创建一个新套接字,用新的套接字和客户端通讯,这个套接字是专门给这个客户端来用的。而原来的监听套接字还在那监听。
2018-02-06 19:07
kwer
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-6-13
  得分:0 
这里有一个现成的,你看能不能用?
https://bbs.bccn.net/thread-487852-1-1.html
2018-06-13 12:44







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027800 second(s), 9 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved