有没有办法在没有客户端和服务器程序的情况下通过PC发送文本?只是简单地将文本从一个程序发送到另一个程序。
发布于 2013-03-24 01:24:18
即使您不想使用客户端和服务器,这也是最简单的方法。服务器是在命令提示符下运行,但在程序后台运行的服务器。服务器和客户端将以任何方式不可见。一个只有几行代码的简单答案是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这将在本地主机端口8000上打开一个"TCPListener“。每当客户端向监听程序发送数据时,textbox的文本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-23 23:36:45
如果你不想使用套接字或管道,我只能想到文件,这更像是pc对pc而不是程序编程。
https://stackoverflow.com/questions/15588245
复制相似问题