Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共9 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:这段Sockets代码在狐表怎么执行,需要引入什么

1楼
zhenghangbo 发表于:2024/8/8 16:07:00
Imports System.Net.Sockets
Imports System.Text

Public Class TCPClientExample
    Private client As TcpClient
    Private stream As NetworkStream

    Public Sub New()
        client = New TcpClient()
        stream = Nothing
    End Sub

    Public Sub Connect(host As String, port As Integer)
        Try
            client.Connect(host, port)
            stream = client.GetStream()
            Console.WriteLine("Connected to server.")
            ReceiveData() ' 开始接收数据
        Catch ex As Exception
            Console.WriteLine("Connection failed: " & ex.Message)
        End Try
    End Sub

    Private Async Sub ReceiveData()
        Dim buffer(1024) As Byte
        Dim received As Integer

        While True
            Try
                received = Await stream.ReadAsync(buffer, 0, buffer.Length)
                Dim data As String = Encoding.UTF8.GetString(buffer, 0, received)
                Console.WriteLine("Received from server: " & data)
            Catch ex As Exception
                Console.WriteLine("Receive failed: " & ex.Message)
                Exit While
            End Try
        End While
    End Sub

    Public Sub SendData(message As String)
        If stream IsNot Nothing Then
            Dim data As Byte() = Encoding.UTF8.GetBytes(message)
            Try
                stream.Write(data, 0, data.Length)
                Console.WriteLine("Sent to server: " & message)
            Catch ex As Exception
                Console.WriteLine("Send failed: " & ex.Message)
            End Try
        Else
            Console.WriteLine("Not connected to server.")
        End If
    End Sub

    Public Sub Disconnect()
        If stream IsNot Nothing Then
            stream.Close()
        End If
        If client IsNot Nothing Then
            client.Close()
        End If
    End Sub
End Class

Module Program
    Sub Main(args As String())
        Dim client As New TCPClientExample()

        client.Connect("localhost", 12345) ' 请替换为实际的服务端地址和端口号

        Console.WriteLine("Press Enter to send a message...")
        While Not Console.KeyAvailable
            If Console.KeyAvailable Then
                Dim input As String = Console.ReadLine()
                client.SendData(input)
            End If
        End While

        client.Disconnect()
        Console.WriteLine("Disconnected.")
        Console.ReadKey()
    End Sub
End Module
[此贴子已经被作者于2024/8/8 16:16:21编辑过]
2楼
有点蓝 发表于:2024/8/8 16:24:00
不需要引用什么,把class里面的内容放到全局代码,然后代码里写全命名空间即可,

比如
Private client As TcpClient
改为
Private client As System.Net.Sockets.TcpClient
3楼
zhenghangbo 发表于:2024/8/8 16:36:00
出错了,未定义类型“NetworkStream”
4楼
有点蓝 发表于:2024/8/8 16:48:00
看2楼,写全完整的命名空间啊。2楼只是举例,不是全部的,剩下的自己改
5楼
zhenghangbo 发表于:2024/8/9 9:03:00
Console.WriteLine("Press Enter to send a message...")
        While Not Console.KeyAvailable
            If Console.KeyAvailable Then
                Dim input As String = Console.ReadLine()
                client.SendData(input)
            End If
        End While
这里的Console.WriteLine与 Console.KeyAvailable是什么?
6楼
有点蓝 发表于:2024/8/9 9:17:00
这个是获取控制台的输入,Foxtable改为使用窗口的文本框获取

        Dim client As New TCPClientExample()

        client.Connect("localhost", 12345) ' 请替换为实际的服务端地址和端口号
client.SendData(e.form.controls("文本框1").text)
client.Disconnect()
[此贴子已经被作者于2024/8/9 9:44:41编辑过]
7楼
zhenghangbo 发表于:2024/8/9 9:29:00
那这段语句又得怎么改?
While Not Console.KeyAvailable
            If Console.KeyAvailable Then
                Dim input As String = Console.ReadLine()
                client.SendData(input)
            End If
        End Whil
8楼
zhenghangbo 发表于:2024/8/9 9:36:00
那我写在全局代码里面的那段代码也得要修改
9楼
有点蓝 发表于:2024/8/9 9:44:00
Module Program里面的内容全部不要,把6楼代码放到窗口按钮里
共9 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .03125 s, 2 queries.