0

有没有办法在没有客户端和服务器程序的情况下通过 PC 发送文本?只需简单地将文本从一个程序发送到另一个程序。

4

2 回答 2

0

如果您不想使用套接字或管道,我只能想到文件,这比程序到程序更多的是 pc 到 pc。

于 2013-03-23T15:36:44.657 回答
0

即使您不想使用客户端和服务器,这也是最简单的方法。服务器是在命令提示符下运行的服务器,但在您的程序后台运行。服务器和客户端不会以任何方式可见。只需几行代码的简单答案是 TCP 通信。这使用两台计算机的 IP 地址并建立服务器/客户端连接。

每个通信都需要承载它的东西,为了实现这一点,您将程序编码为包含以下内容:

Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listener.Stop()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
listener.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim Data As String = ""
Dim nStart As Integer
Dim nLast As Integer
If listener.Pending = True Then
Client = listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream)
While Reader.Peek > -1
Data &= Convert.ToChar(Reader.Read()).ToString
End While
If Not Data = "" Then
msgbox("This is the data recieved: " & Data)
End If
End If
End Sub
End Class

这将在 localhost 端口 8000 上打开一个“TCPListener”。每当客户端向侦听器发送数据时,文本框 Textbox1 的文本就会向数据发送。

要将数据发送到服务器,请使用以下代码:

Option Explicit On
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim Client As TcpClient
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
//Ip to the local or remote, forwarded server. 127.0.0.1 is localhost - the same machine.
Client = New TcpClient("127.0.0.1", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write("Hello World!")
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

这将在按下 Button1 时尝试发送数据/字符串“Hello World!” 到服务器。

通过将应用程序设置如下,可以将其合并为一个:

Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listener.Stop()
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
listener.Start()
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Dim Data As String = ""
Dim nStart As Integer
Dim nLast As Integer
If listener.Pending = True Then
Client = listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream)
While Reader.Peek > -1
Data &= Convert.ToChar(Reader.Read()).ToString
End While
If Not Data = "" Then
'Change the string
End If
TextBox1.Text = Data
End If
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
//This has to be the address to the remote 
Client = New TcpClient("xx.xx.xx.xx", 8000)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(TextBox2.Text)
Writer.Flush()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

要扩展它并使其在实际应用程序中可用,请使用后台工作程序来简单地使服务器和客户端在另一个线程上运行。

于 2013-03-23T17:24:17.623 回答