Click to See Complete Forum and Search --> : tcpclient


skyloveying91
07-07-2009, 01:56 AM
Imports System.Net.Sockets
Imports System.Text
Public Class frmMain


Dim Tcp80 As New TcpListener(80)
Dim tcp801 As New TcpClient


Public Sub Accept80()
If Not (Tcp80.Pending()) Then
Exit Sub
Else
If tcp801.Connected = False Then
tcp801 = Tcp80.AcceptTcpClient
End If

Dim networkStream As NetworkStream = tcp801.GetStream()
Dim bytes(tcp801.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcp801.ReceiveBufferSize))

If tcp802.Connected = False Then
tcp802.Connect("123.123.123.123", 80)
End If


Dim networkStream2 As NetworkStream = tcp802.GetStream()
If networkStream2.CanWrite And networkStream2.CanRead Then
networkStream2.Write(bytes, 0, tcp801.ReceiveBufferSize)
ReDim bytes(tcp802.ReceiveBufferSize)
networkStream2.Read(bytes, 0, CInt(tcp802.ReceiveBufferSize))
Else
'If Not networkStream2.CanRead Then
' tcp802.Close()
'Else
' If Not networkStream2.CanWrite Then
' tcp802.Close()
' End If
'End If
End If
networkStream.Write(bytes, 0, bytes.Length)
'tcp801.Close()
End If
End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tcp80.Start()
End Sub

Private Sub timerListen_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerListen.Tick
Accept80()
End Sub

End Class


how to change the connection to keep alive?
is it something related to Asynchronous ?
this codes does :
Open connection > send > receive > close connection

what i want:
Open connection > send .... receive ... send ... receive....
...
...
...
...
until it is not needed only close.
and with auto reconnect on dc.
i tried this but not working


If tcp802.Connected = False Then
tcp802.Connect("123.123.123.123", 80)
End If


pros please give advice.. thx in advance :D