|
-
Jan 2nd, 2008, 11:55 AM
#9
Re: [2008] Can I add a TCPClient Component
 Originally Posted by perito
so I will follow Atheist's example since it looks pretty simple
The client side would be then
Code:
Dim listener As Net.Sockets.TcpListener Dim listenThread As Threading.Thread Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load listener = New Net.Sockets.TcpListener("127.0.0.1", 32111) listener.Start() listenThread = New Threading.Thread(AddressOf DoListen) listenThread.IsBackground = True listenThread.Start() End Sub
The Server side would be
Code:
Dim listener As Net.Sockets.TcpListener
Private Sub DoListen() 'This is the sub that does all the actual "listening". Its an endless loop that calls the AcceptTcpClient method over and over. 'If there is no connecting TcpClient, it will raise an exception but we will ignore this by catching it in a try statement and doing nothing with it. 'On the other hand, If a client has connected, it proceeds to the next line and a streamreader reads the incoming stream and shows it in a messagebox. Dim sr As IO.StreamReader Do Try Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient sr = New IO.StreamReader(client.GetStream) MessageBox.Show(sr.ReadToEnd) sr.Close() Catch End Try Loop End Sub
right?
this will only connect the two programs?
how to send information after connection?
My example was very basic and needs some adjustment to be more effective. But basically the server side should have a TcpListener listening on a specified port and the client side should have a TcpClient connecting to the server on the same port.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|