2013-03-23 70 views

回答

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 

這將本地主機端口8000上打開「的TCPListener」每當客戶端將數據發送到監聽器,文本框Textbox1的文本發送到數據。

將數據發送到服務器,使用下面的代碼:「Hello World」的

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的,嘗試發送數據/串到服務器。

這可能是由於其設置爲以下的應用組合成一個:

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 

要延長這一點,並使其在實際應用中可使用,使用一個BackgroundWorker簡單地使服務器和客戶端上運行另一個線程。

0

如果你不想使用套接字或管道,我只能想到文件,這是更多的PC到PC比程序來編程。

+0

我嘗試了很多代碼,但沒有任何工作。你能告訴我如何在同一時間製作一個程序服務器和客戶端嗎?我想通過這些計劃發送(很多)文本,但我不知道如何 – user1608730 2013-03-23 15:46:53