| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2149 人关注过本帖, 1 人收藏
标题:只能收发一次数据,跪求大神帮忙看下!我需要实现连续收发数据。
只看楼主 加入收藏
qqmushui
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-7-22
收藏(1)
 问题点数:0 回复次数:0 
只能收发一次数据,跪求大神帮忙看下!我需要实现连续收发数据。
Imports
Imports
Imports System.Threading
Imports System.Text
Imports System.Diagnostics
Imports



Public Class frmServer


    '服务器端的Socket
    Dim Clistener As Socket
    Dim Slistener As Socket
    '与客户端会话的Socket
    Dim CmySocket As Socket
    Dim SmySocket As Socket
    '服务器端的运行转态
    Dim IsRun As Boolean = False
    '监听接收数据线程
    Dim CmyThread As Thread
    Dim SmyThread As Thread
    Dim have As Integer



    Private cclient As Socket
    Private swWriter As StreamWriter
    Private nsStream As NetworkStream

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CmyThread = New Thread(AddressOf Listen)
        '   SmyThread = New Thread(AddressOf SListen)
        CmyThread.Start()
        ' SmyThread.Start()
    End Sub


    Public Sub Listen()

        Dim cbytes() As Byte = New [Byte](1024) {}
        Dim data As String = Nothing

        Dim ClocalEndPoint As New IPEndPoint(IPAddress.Parse("1.1.1.10"), 502)
        '初始化socket
        Clistener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        '绑定端口
        Clistener.Bind(ClocalEndPoint)
        '开始监听
        Clistener.Listen(10)
        BeginInvoke(New EventHandler(AddressOf AddInfo), "服务器端已启动,正在等待连接......")
        CmySocket = Clistener.Accept()
        IsRun = True

        ''''''''''''''''''''''''
        Dim remotePoint As New IPEndPoint(IPAddress.Parse("192.168.186.128"), 502)
        cclient = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        cclient.Connect(remotePoint)
        'cclient = New TcpClient("192.168.186.128", 502)
        'nsStream = cclient.GetStream()
        'swWriter = New StreamWriter(nsStream)

        '''''''''''''''''''''''''
        While True
            Dim bytesRec As Integer = CmySocket.Receive(cbytes)
            ''Dim judge As Byte = cbytes(0)

            Dim cc As Integer = cbytes(11)
            Dim sc As Integer = cc * 2 + 8
            'MsgBox(sc)
            'nsStream.Write(cbytes, 0, bytesRec)
            Dim ccbytes() As Byte = New [Byte](sc) {}
            cclient.Send(cbytes)
            cclient.Receive(ccbytes)
            '  Dim i As Integer = 0
            'While (nsStream.DataAvailable = False)
            'End While
            'ReDim ccbytes(sc)
            'nsStream.Read(ccbytes, 0, sc)
            ''ccbytes(1) += 1
            CmySocket.Send(ccbytes)

            '  MsgBox(i)
        End While
    End Sub
    Public Sub SListen()

        Dim sbytes() As Byte = New [Byte](1024) {}
        Dim data As String = Nothing

        Dim SlocalEndPoint As New IPEndPoint(IPAddress.Parse("2.2.2.2"), 502)
        '初始化socket
        Slistener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        '绑定端口
        Slistener.Bind(SlocalEndPoint)
        '开始监听
        Slistener.Listen(10)
        BeginInvoke(New EventHandler(AddressOf AddInfo), "服务器端已启动,正在等待连接......")
        SmySocket = Slistener.Accept()
        IsRun = True

        While True
            Dim SbytesRec As Integer = SmySocket.Receive(sbytes)
            ''Dim judge As Byte = cbytes(0)
            Dim cs As Integer = sbytes(11)
            Dim ss As Integer = cs * 2 + 8
            Dim ssbytes() As Byte = New [Byte](ss) {}
            Dim stmp As Integer = readReg(sbytes, SbytesRec, ssbytes, ss)
            SmySocket.Send(ssbytes)
        End While


    End Sub

    Sub AddInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ListBox1.Items.Insert(0, "远程信息: " + sender.ToString)
    End Sub


    ' 关闭窗口时发关退出信息并清理资源
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'If IsRun Then
        '    Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|服务器端退出: " + Me.Handle.ToString)
        '    Dim bytesSent As Integer = CmySocket.Send(msg)
        'End If
        Clistener.Close()
        Slistener.Close()
        CmySocket = Nothing
        SmySocket = Nothing
        CmyThread.Abort()
        SmyThread.Abort()
    End Sub

End Class
搜索更多相关主题的帖子: System Socket Dim New Byte 
2018-08-23 19:05
快速回复:只能收发一次数据,跪求大神帮忙看下!我需要实现连续收发数据。
数据加载中...
 
   



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

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