TCP/IP 
Author Message
 TCP/IP

I need to write a VB.NET component for listening,
reswponding, and accepting documents from another
computer.  VB6 has a TCP/IP control.  Does .NET have an
equalent control?  If not, does anyone have any
suggestions on where to begin looking for help with this
project?  We are currently using VB6 with a 3rd party
control called Dolphin.

Thanks!!!!
Robert
.



Wed, 22 Jun 2005 06:32:04 GMT  
 TCP/IP
No Winsock control equivalent but a dedicated namespace under
system.net.sockets
MSDN have an example using TCPListener and TCPClient, have a look they're
wrappers around base Winsock class

HTH

--
Corrado Cavalli [Microsoft MVP]
UGIdotNET - http://www.ugidotnet.org



Wed, 22 Jun 2005 06:53:22 GMT  
 TCP/IP
Robert,
  VB .net has some really useful stuff for data transfer over TCP/IP
(They're objects, not controls).  The System.Net.Sockets namespace has both
TCP and UDP services, and even has the TCPListener and TCPClient objects for
simple interactions.

Dracolytch


Quote:
> I need to write a VB.NET component for listening,
> reswponding, and accepting documents from another
> computer.  VB6 has a TCP/IP control.  Does .NET have an
> equalent control?  If not, does anyone have any
> suggestions on where to begin looking for help with this
> project?  We are currently using VB6 with a 3rd party
> control called Dolphin.

> Thanks!!!!
> Robert
> .



Wed, 22 Jun 2005 07:22:22 GMT  
 TCP/IP
hi
i wrote  a wraper around the socket class trying to imitate the winsock
control
it is not 100% comptetable with win sock but it is real close.
also it is not fully done but it is working
hoped it helped

#Region "Imports"

Imports System

Imports System.IO

Imports System.Net

Imports System.Net.Sockets

Imports System.Text

Imports System.Threading

#End Region

Public Class DtiSocket

#Region "Declares"

Private LocalEndPoint As IPEndPoint

Private mState As Status

Private SocketStream As Socket

Private Listener As Socket

Private ActAsServer As Boolean

Dim NewData As String

Dim ClosingCOnnection As Boolean

#End Region

#Region "Enums"

Public Enum Status

Disconnected = 0

Listening = 1

Connected = 3

Connecting = 2

End Enum

#End Region

#Region "Events"

Public Event Close()

Public Event ClosedByRemote()

Public Event Connected(ByRef Sender As DtiSocket)

'Public Event ConnectionRequest()

Public Event DataArrival(ByRef Sender As DtiSocket, ByVal Data As String)

Public Event SocketError(ByRef Sender As DtiSocket, ByVal Number As Long,
ByVal Description As String)

Public Event StatusChange(ByRef Sender As DtiSocket, ByVal NewStatus As
Status)

#End Region

#Region "Property Variables"

Private mLocalHost As String = "127.0.0.1"

Private mPort As Integer = 1937

Private mRemote_Host As String = "127.0.0.1"

#End Region

#Region "Properties"

Public Tag As String

Public ReciveTimeOut As Integer

Public Property LocalHost() As String

Get

Return mLocalHost

End Get

Set(ByVal Value As String)

mLocalHost = Value

End Set

End Property

Public Property Port()

Get

Return mPort

End Get

Set(ByVal Value)

mPort = Value

End Set

End Property

Public Property RemoteHost() As String

Get

RemoteHost = mRemote_Host

End Get

Set(ByVal Value As String)

mRemote_Host = Value

End Set

End Property

'Public Property RemotePort()

' Get

' RemotePort = mRemote_Port

' End Get

' Set(ByVal Value)

' mRemote_Port = Value

' End Set

'End Property

Public ReadOnly Property State() As Status

Get

State = mState

End Get

End Property

#End Region

#Region "Methods"

Public Sub CloseSocket()

On Error Resume Next

Dim RaiseEventStatus As Boolean

ClosingCOnnection = True

If mState <> Status.Disconnected Then RaiseEventStatus = True

mState = 0

If Not SocketStream Is Nothing Then
SocketStream.Shutdown(SocketShutdown.Both)

If Not SocketStream Is Nothing Then SocketStream.Close()

If Not Listener Is Nothing Then Listener.Shutdown(SocketShutdown.Both)

If Not Listener Is Nothing Then Listener.Close()

'SocketStream = Nothing

If RaiseEventStatus = True Then RaiseEvent StatusChange(Me,
Status.Disconnected)

ClosingCOnnection = False

End Sub

Public Sub Connect()

If mState = Status.Disconnected Then

ActAsServer = False

mState = 2 'Set State flag to Connecting

RaiseEvent StatusChange(Me, Status.Connecting)

ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf
Connect2))

Else

RaiseEvent SocketError(Me, 0, "Connect is only allowd when the socket is in
disconnect state")

End If

End Sub 'Start trying to connect to socket

Private Sub Connect2(ByVal State As Object)


Dim a As String

'Dim LIP As IPAddress = IPAddress.Parse(LocalIP)

Try

Dim RIP As IPAddress = Dns.Resolve(RemoteHost).AddressList(0)

'Dns.Resolve(mRemote_Host).AddressList(0)

Dim LocalEndPoint As New IPEndPoint(RIP, mPort)

SocketStream = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

SocketStream.Blocking = True

SocketStream.Connect(LocalEndPoint)

' SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 15000)

' SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 5000)

mState = Status.Connected 'Set state flag to connected

RaiseEvent StatusChange(Me, Status.Connected)

RaiseEvent Connected(Me)

' If it all worked out, create stream objects

If Not (SocketStream Is Nothing) Then

ReceiveData(Nothing)

' RaiseEvent ClosedByRemote()

Else

RaiseEvent SocketError(Me, 7563, "Failed to Establish Connection")

End If

Catch e As SocketException

'RaiseEvent StatusChange(Status.Disconnected)

If ClosingCOnnection = False Then RaiseEvent SocketError(Me, e.ErrorCode,
e.Message)

End Try

End Sub

Public Sub Listen()

If mState = Status.Disconnected Then

ActAsServer = True

mState = 1 'Set State flag to Listening

RaiseEvent StatusChange(Me, Status.Listening)

ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf
Listen2))

Else

RaiseEvent SocketError(Me, 0, "Listening is only allowd when the socket is
in disconnect state")

End If

End Sub 'Start listening to socket

Private Sub Listen2(ByVal State As Object)

'This is called until accept

' Dim Listener As Socket

'Dim LIP As IPAddress = IPAddress.Parse(LocalIP)

Try

Dim LocalEndPoint As New IPEndPoint(Dns.Resolve(mLocalHost).AddressList(0),
mPort)

Listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

Listener.Blocking = True 'Only let us listen to this port

Listener.Bind(LocalEndPoint)

RaiseEvent StatusChange(Me, Status.Listening)

Listener.Listen(0)

SocketStream = Listener.Accept()

Listener.Close()

SocketStream.Blocking = True

'Stream = New NetworkStream(SocketStream)

'LSockReader = New StreamReader(Stream)

RaiseEvent StatusChange(Me, Status.Connected)

RaiseEvent Connected(Me)

mState = 3 'Set State flag to connected

ReceiveData(Nothing)

Catch e As SocketException

If ClosingCOnnection = False Then RaiseEvent SocketError(Me, e.ErrorCode,
e.Message)

End Try

End Sub 'Listen Address

Public Function CompressString(ByVal str As String) As Byte()

Dim ms As IO.MemoryStream = New IO.MemoryStream()

' attach compressor stream to memory stream

Dim sw As C1.C1Zip.C1ZStreamWriter = New C1.C1Zip.C1ZStreamWriter(ms)

' write data into compressor stream

Dim writer As IO.StreamWriter = New IO.StreamWriter(sw)

writer.Write(str)

' flush any pending data

writer.Flush()

' return the memory buffer

CompressString = ms.GetBuffer()

End Function

' Send text to remote connection if an error occer returen the error msg

Public Function SendData(ByVal newText As String) As String

Dim enc As New System.Text.ASCIIEncoding()

'Dim SendBuffer(20480) As Byte

Dim CompressedBuffer() As Byte

Dim t, t1 As Date

Dim i As Integer

On Error GoTo Hell

If newText = "" Then Err.Raise(666, , "Can't send zero lengh text")

t = Now

CompressedBuffer = CompressString(newText)

i = Now.Subtract(t).TotalMilliseconds

SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 5000)

'SendBuffer = enc.GetBytes(newText)

SocketStream.Send(CompressedBuffer) 'SendBuffer)

SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 0)

Return ""

Hell:

Return Err.Description

End Function 'SendData

Public Function ExpandString(ByVal buffer As Byte()) As String

Try

' turn buffer into a memory stream

Dim ms As MemoryStream = New MemoryStream(buffer)

' attach decompressor stream to memory stream

Dim sr As C1.C1Zip.C1ZStreamReader = New C1.C1Zip.C1ZStreamReader(ms)

' read uncompressed data

Dim reader As StreamReader = New StreamReader(sr)

ExpandString = reader.ReadToEnd()

Catch e As Exception

Return "Failed to UnZip String " & e.Message

End Try

End Function

Private Sub ReceiveData(ByVal State As Object)

Static EchoStr As String

Static EchoTimeStamp As Date = Now

Dim ts As TimeSpan

Dim enc As New System.Text.ASCIIEncoding()

Dim ReceiveBuffer(20480), EmptyBuffer(20480), DynaArr() As Byte

Dim str, strCompress As String

Dim i, j As Integer

Try

' SocketStream.Blocking = True

' While (Not SocketStream Is Nothing) AndAlso mState = Status.Connected

'ReDim ReceiveBuffer()

If SocketStream Is Nothing OrElse mState <> Status.Connected Then Exit Sub

SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, ReciveTimeOut)

SocketStream.Receive(ReceiveBuffer)

SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 0)

i = Array.LastIndexOf(ReceiveBuffer, CByte(255))

If i <= 0 Then

CloseSocket()

Exit Sub

Else

ReDim DynaArr(i)

ReceiveBuffer.Copy(ReceiveBuffer, DynaArr, i + 1)

str = ExpandString(DynaArr)

End If

'i = ReceiveBuffer.GetUpperBound(0)

' str = enc.GetString(ReceiveBuffer, 0, i)

'ReceiveBuffer = EmptyBuffer

'str()

'str = LSockReader.Read(oneBuffer, 0, oneBuffer.GetLength(0))

If str.Length = 0 OrElse str.Chars(0) = ChrW(0) Then

'RaiseEvent SocketError(666, "Socket Endless Loop Bug ,the socket will be
cloesed")

CloseSocket()

Else

' ts = Now.Subtract(EchoTimeStamp)

' If ts.TotalSeconds > 4.5 OrElse str <> EchoStr Then

' System.Console.WriteLine(Now.ToString & " " & EchoTimeStamp.ToString & " "
& ts.TotalSeconds)

' System.Console.WriteLine(str)

' System.Console.WriteLine(EchoTimeStamp)

NewData = str

'EchoStr = str

' EchoTimeStamp = Now

ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf
New_Data_Arrived))

' Else

' EchoTimeStamp = Now

' System.Console.WriteLine("Recived Echo " & ts.TotalSeconds)

' System.Console.WriteLine(EchoStr)

'End If

str = ""

End If

'End While

Catch e As SocketException

If ClosingCOnnection = False Then RaiseEvent SocketError(Me, e.ErrorCode,
e.Message)

'Select Case e.ErrorCode

' Case 10053 'local close connection

' RaiseEvent Close()

' Case 10054 'remote close connection

' RaiseEvent ClosedByRemote()

' Case Else

' RaiseEvent SocketError(e.ErrorCode, e.Message)

' CloseSocket()

'End Select

End Try

End Sub 'ReceiveData

Private Sub New_Data_Arrived(ByVal State As Object)

Dim a As String

a = NewData

NewData = ""

RaiseEvent DataArrival(Me, a)

ReceiveData(Nothing)

'ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf
ReceiveData))

End Sub

#End Region

Public Sub New()

End Sub

End Class
...

read more »



Wed, 22 Jun 2005 15:00:09 GMT  
 TCP/IP
Hi Ran,
Where can I find C1.C1Zip.C1ZStreamWriter?

Quote:
>-----Original Message-----
>hi
>i wrote  a wraper around the socket class trying to
imitate the winsock
>control
>it is not 100% comptetable with win sock but it is real
close.
>also it is not fully done but it is working
>hoped it helped

>#Region "Imports"

>Imports System

>Imports System.IO

>Imports System.Net

>Imports System.Net.Sockets

>Imports System.Text

>Imports System.Threading

>#End Region

>Public Class DtiSocket

>#Region "Declares"

>Private LocalEndPoint As IPEndPoint

>Private mState As Status

>Private SocketStream As Socket

>Private Listener As Socket

>Private ActAsServer As Boolean

>Dim NewData As String

>Dim ClosingCOnnection As Boolean

>#End Region

>#Region "Enums"

>Public Enum Status

>Disconnected = 0

>Listening = 1

>Connected = 3

>Connecting = 2

>End Enum

>#End Region

>#Region "Events"

>Public Event Close()

>Public Event ClosedByRemote()

>Public Event Connected(ByRef Sender As DtiSocket)

>'Public Event ConnectionRequest()

>Public Event DataArrival(ByRef Sender As DtiSocket,

ByVal Data As String)
Quote:

>Public Event SocketError(ByRef Sender As DtiSocket,

ByVal Number As Long,

- Show quoted text -

Quote:
>ByVal Description As String)

>Public Event StatusChange(ByRef Sender As DtiSocket,
ByVal NewStatus As
>Status)

>#End Region

>#Region "Property Variables"

>Private mLocalHost As String = "127.0.0.1"

>Private mPort As Integer = 1937

>Private mRemote_Host As String = "127.0.0.1"

>#End Region

>#Region "Properties"

>Public Tag As String

>Public ReciveTimeOut As Integer

>Public Property LocalHost() As String

>Get

>Return mLocalHost

>End Get

>Set(ByVal Value As String)

>mLocalHost = Value

>End Set

>End Property

>Public Property Port()

>Get

>Return mPort

>End Get

>Set(ByVal Value)

>mPort = Value

>End Set

>End Property

>Public Property RemoteHost() As String

>Get

>RemoteHost = mRemote_Host

>End Get

>Set(ByVal Value As String)

>mRemote_Host = Value

>End Set

>End Property

>'Public Property RemotePort()

>' Get

>' RemotePort = mRemote_Port

>' End Get

>' Set(ByVal Value)

>' mRemote_Port = Value

>' End Set

>'End Property

>Public ReadOnly Property State() As Status

>Get

>State = mState

>End Get

>End Property

>#End Region

>#Region "Methods"

>Public Sub CloseSocket()

>On Error Resume Next

>Dim RaiseEventStatus As Boolean

>ClosingCOnnection = True

>If mState <> Status.Disconnected Then RaiseEventStatus =
True

>mState = 0

>If Not SocketStream Is Nothing Then
>SocketStream.Shutdown(SocketShutdown.Both)

>If Not SocketStream Is Nothing Then SocketStream.Close()

>If Not Listener Is Nothing Then Listener.Shutdown

(SocketShutdown.Both)

- Show quoted text -

Quote:

>If Not Listener Is Nothing Then Listener.Close()

>'SocketStream = Nothing

>If RaiseEventStatus = True Then RaiseEvent StatusChange
(Me,
>Status.Disconnected)

>ClosingCOnnection = False

>End Sub

>Public Sub Connect()

>If mState = Status.Disconnected Then

>ActAsServer = False

>mState = 2 'Set State flag to Connecting

>RaiseEvent StatusChange(Me, Status.Connecting)

>ThreadPool.QueueUserWorkItem(New

System.Threading.WaitCallback(AddressOf
Quote:
>Connect2))

>Else

>RaiseEvent SocketError(Me, 0, "Connect is only allowd

when the socket is in

- Show quoted text -

Quote:
>disconnect state")

>End If

>End Sub 'Start trying to connect to socket

>Private Sub Connect2(ByVal State As Object)


>Dim a As String

>'Dim LIP As IPAddress = IPAddress.Parse(LocalIP)

>Try

>Dim RIP As IPAddress = Dns.Resolve

(RemoteHost).AddressList(0)

- Show quoted text -

Quote:

>'Dns.Resolve(mRemote_Host).AddressList(0)

>Dim LocalEndPoint As New IPEndPoint(RIP, mPort)

>SocketStream = New Socket(AddressFamily.InterNetwork,
SocketType.Stream,
>ProtocolType.Tcp)

>SocketStream.Blocking = True

>SocketStream.Connect(LocalEndPoint)

>' SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>SocketOptionName.ReceiveTimeout, 15000)

>' SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>SocketOptionName.SendTimeout, 5000)

>mState = Status.Connected 'Set state flag to connected

>RaiseEvent StatusChange(Me, Status.Connected)

>RaiseEvent Connected(Me)

>' If it all worked out, create stream objects

>If Not (SocketStream Is Nothing) Then

>ReceiveData(Nothing)

>' RaiseEvent ClosedByRemote()

>Else

>RaiseEvent SocketError(Me, 7563, "Failed to Establish
Connection")

>End If

>Catch e As SocketException

>'RaiseEvent StatusChange(Status.Disconnected)

>If ClosingCOnnection = False Then RaiseEvent SocketError
(Me, e.ErrorCode,
>e.Message)

>End Try

>End Sub

>Public Sub Listen()

>If mState = Status.Disconnected Then

>ActAsServer = True

>mState = 1 'Set State flag to Listening

>RaiseEvent StatusChange(Me, Status.Listening)

>ThreadPool.QueueUserWorkItem(New

System.Threading.WaitCallback(AddressOf

- Show quoted text -

Quote:
>Listen2))

>Else

>RaiseEvent SocketError(Me, 0, "Listening is only allowd
when the socket is
>in disconnect state")

>End If

>End Sub 'Start listening to socket

>Private Sub Listen2(ByVal State As Object)

>'This is called until accept

>' Dim Listener As Socket

>'Dim LIP As IPAddress = IPAddress.Parse(LocalIP)

>Try

>Dim LocalEndPoint As New IPEndPoint(Dns.Resolve

(mLocalHost).AddressList(0),

- Show quoted text -

Quote:
>mPort)

>Listener = New Socket(AddressFamily.InterNetwork,
SocketType.Stream,
>ProtocolType.Tcp)

>Listener.Blocking = True 'Only let us listen to this port

>Listener.Bind(LocalEndPoint)

>RaiseEvent StatusChange(Me, Status.Listening)

>Listener.Listen(0)

>SocketStream = Listener.Accept()

>Listener.Close()

>SocketStream.Blocking = True

>'Stream = New NetworkStream(SocketStream)

>'LSockReader = New StreamReader(Stream)

>RaiseEvent StatusChange(Me, Status.Connected)

>RaiseEvent Connected(Me)

>mState = 3 'Set State flag to connected

>ReceiveData(Nothing)

>Catch e As SocketException

>If ClosingCOnnection = False Then RaiseEvent SocketError
(Me, e.ErrorCode,
>e.Message)

>End Try

>End Sub 'Listen Address

>Public Function CompressString(ByVal str As String) As
Byte()

>Dim ms As IO.MemoryStream = New IO.MemoryStream()

>' attach compressor stream to memory stream

>Dim sw As C1.C1Zip.C1ZStreamWriter = New

C1.C1Zip.C1ZStreamWriter(ms)

- Show quoted text -

Quote:

>' write data into compressor stream

>Dim writer As IO.StreamWriter = New IO.StreamWriter(sw)

>writer.Write(str)

>' flush any pending data

>writer.Flush()

>' return the memory buffer

>CompressString = ms.GetBuffer()

>End Function

>' Send text to remote connection if an error occer

returen the error msg

- Show quoted text -

Quote:

>Public Function SendData(ByVal newText As String) As
String

>Dim enc As New System.Text.ASCIIEncoding()

>'Dim SendBuffer(20480) As Byte

>Dim CompressedBuffer() As Byte

>Dim t, t1 As Date

>Dim i As Integer

>On Error GoTo Hell

>If newText = "" Then Err.Raise(666, , "Can't send zero
lengh text")

>t = Now

>CompressedBuffer = CompressString(newText)

>i = Now.Subtract(t).TotalMilliseconds

>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>SocketOptionName.SendTimeout, 5000)

>'SendBuffer = enc.GetBytes(newText)

>SocketStream.Send(CompressedBuffer) 'SendBuffer)

>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>SocketOptionName.SendTimeout, 0)

>Return ""

>Hell:

>Return Err.Description

>End Function 'SendData

>Public Function ExpandString(ByVal buffer As Byte()) As
String

>Try

>' turn buffer into a memory stream

>Dim ms As MemoryStream = New MemoryStream(buffer)

>' attach decompressor stream to memory stream

>Dim sr As C1.C1Zip.C1ZStreamReader = New

C1.C1Zip.C1ZStreamReader(ms)

- Show quoted text -

Quote:

>' read uncompressed data

>Dim reader As StreamReader = New StreamReader(sr)

>ExpandString = reader.ReadToEnd()

>Catch e As Exception

>Return "Failed to UnZip String " & e.Message

>End Try

>End Function

>Private Sub ReceiveData(ByVal State As Object)

>Static EchoStr As String

>Static EchoTimeStamp As Date = Now

>Dim ts As TimeSpan

>Dim enc As New System.Text.ASCIIEncoding()

>Dim ReceiveBuffer(20480), EmptyBuffer(20480), DynaArr()
As Byte

>Dim str, strCompress As String

>Dim i, j As Integer

>Try

>' SocketStream.Blocking = True

>' While (Not SocketStream Is Nothing) AndAlso mState =
Status.Connected

>'ReDim ReceiveBuffer()

>If SocketStream Is Nothing OrElse mState <>

Status.Connected Then Exit Sub

- Show quoted text -

Quote:

>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>SocketOptionName.ReceiveTimeout, ReciveTimeOut)

>SocketStream.Receive(ReceiveBuffer)

>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>SocketOptionName.ReceiveTimeout, 0)

>i = Array.LastIndexOf(ReceiveBuffer, CByte(255))

>If i <= 0 Then

>CloseSocket()

>Exit Sub

>Else

>ReDim DynaArr(i)

>ReceiveBuffer.Copy(ReceiveBuffer, DynaArr, i + 1)

>str = ExpandString(DynaArr)

>End If

>'i = ReceiveBuffer.GetUpperBound(0)

>' str = enc.GetString(ReceiveBuffer, 0, i)

>'ReceiveBuffer = EmptyBuffer

>'str()

>'str = LSockReader.Read(oneBuffer, 0, oneBuffer.GetLength
(0))

>If str.Length = 0 OrElse str.Chars(0) = ChrW(0) Then

>'RaiseEvent SocketError(666, "Socket Endless Loop

Bug ,the socket will be
Quote:
>cloesed")

>CloseSocket()

>Else

>' ts = Now.Subtract(EchoTimeStamp)

>' If ts.TotalSeconds > 4.5 OrElse str <> EchoStr Then

>' System.Console.WriteLine(Now.ToString & " " &

EchoTimeStamp.ToString & " "
Quote:
>& ts.TotalSeconds)

>' System.Console.WriteLine(str)

>' System.Console.WriteLine(EchoTimeStamp)

>NewData = str

>'EchoStr = str

>' EchoTimeStamp = Now

>ThreadPool.QueueUserWorkItem(New

System.Threading.WaitCallback(AddressOf

- Show quoted text -

Quote:
>New_Data_Arrived))

>' Else

>' EchoTimeStamp = Now

>' System.Console.WriteLine("Recived Echo " &
ts.TotalSeconds)

>' System.Console.WriteLine(EchoStr)

>'End If

>str = ""

>End If

>'End While

>Catch e As SocketException

>If ClosingCOnnection = False Then RaiseEvent SocketError

(Me, ...

read more »



Fri, 24 Jun 2005 09:58:29 GMT  
 TCP/IP
Ran,

Thanks for your help!  I also need to know about
C1.C1Zip.C1ZStreamWriter.

Robert

Quote:
>-----Original Message-----
>Hi Ran,
>Where can I find C1.C1Zip.C1ZStreamWriter?

>>-----Original Message-----
>>hi
>>i wrote  a wraper around the socket class trying to
>imitate the winsock
>>control
>>it is not 100% comptetable with win sock but it is real
>close.
>>also it is not fully done but it is working
>>hoped it helped

>>#Region "Imports"

>>Imports System

>>Imports System.IO

>>Imports System.Net

>>Imports System.Net.Sockets

>>Imports System.Text

>>Imports System.Threading

>>#End Region

>>Public Class DtiSocket

>>#Region "Declares"

>>Private LocalEndPoint As IPEndPoint

>>Private mState As Status

>>Private SocketStream As Socket

>>Private Listener As Socket

>>Private ActAsServer As Boolean

>>Dim NewData As String

>>Dim ClosingCOnnection As Boolean

>>#End Region

>>#Region "Enums"

>>Public Enum Status

>>Disconnected = 0

>>Listening = 1

>>Connected = 3

>>Connecting = 2

>>End Enum

>>#End Region

>>#Region "Events"

>>Public Event Close()

>>Public Event ClosedByRemote()

>>Public Event Connected(ByRef Sender As DtiSocket)

>>'Public Event ConnectionRequest()

>>Public Event DataArrival(ByRef Sender As DtiSocket,
>ByVal Data As String)

>>Public Event SocketError(ByRef Sender As DtiSocket,
>ByVal Number As Long,
>>ByVal Description As String)

>>Public Event StatusChange(ByRef Sender As DtiSocket,
>ByVal NewStatus As
>>Status)

>>#End Region

>>#Region "Property Variables"

>>Private mLocalHost As String = "127.0.0.1"

>>Private mPort As Integer = 1937

>>Private mRemote_Host As String = "127.0.0.1"

>>#End Region

>>#Region "Properties"

>>Public Tag As String

>>Public ReciveTimeOut As Integer

>>Public Property LocalHost() As String

>>Get

>>Return mLocalHost

>>End Get

>>Set(ByVal Value As String)

>>mLocalHost = Value

>>End Set

>>End Property

>>Public Property Port()

>>Get

>>Return mPort

>>End Get

>>Set(ByVal Value)

>>mPort = Value

>>End Set

>>End Property

>>Public Property RemoteHost() As String

>>Get

>>RemoteHost = mRemote_Host

>>End Get

>>Set(ByVal Value As String)

>>mRemote_Host = Value

>>End Set

>>End Property

>>'Public Property RemotePort()

>>' Get

>>' RemotePort = mRemote_Port

>>' End Get

>>' Set(ByVal Value)

>>' mRemote_Port = Value

>>' End Set

>>'End Property

>>Public ReadOnly Property State() As Status

>>Get

>>State = mState

>>End Get

>>End Property

>>#End Region

>>#Region "Methods"

>>Public Sub CloseSocket()

>>On Error Resume Next

>>Dim RaiseEventStatus As Boolean

>>ClosingCOnnection = True

>>If mState <> Status.Disconnected Then RaiseEventStatus =
>True

>>mState = 0

>>If Not SocketStream Is Nothing Then
>>SocketStream.Shutdown(SocketShutdown.Both)

>>If Not SocketStream Is Nothing Then SocketStream.Close()

>>If Not Listener Is Nothing Then Listener.Shutdown
>(SocketShutdown.Both)

>>If Not Listener Is Nothing Then Listener.Close()

>>'SocketStream = Nothing

>>If RaiseEventStatus = True Then RaiseEvent StatusChange
>(Me,
>>Status.Disconnected)

>>ClosingCOnnection = False

>>End Sub

>>Public Sub Connect()

>>If mState = Status.Disconnected Then

>>ActAsServer = False

>>mState = 2 'Set State flag to Connecting

>>RaiseEvent StatusChange(Me, Status.Connecting)

>>ThreadPool.QueueUserWorkItem(New
>System.Threading.WaitCallback(AddressOf
>>Connect2))

>>Else

>>RaiseEvent SocketError(Me, 0, "Connect is only allowd
>when the socket is in
>>disconnect state")

>>End If

>>End Sub 'Start trying to connect to socket

>>Private Sub Connect2(ByVal State As Object)


>>Dim a As String

>>'Dim LIP As IPAddress = IPAddress.Parse(LocalIP)

>>Try

>>Dim RIP As IPAddress = Dns.Resolve
>(RemoteHost).AddressList(0)

>>'Dns.Resolve(mRemote_Host).AddressList(0)

>>Dim LocalEndPoint As New IPEndPoint(RIP, mPort)

>>SocketStream = New Socket(AddressFamily.InterNetwork,
>SocketType.Stream,
>>ProtocolType.Tcp)

>>SocketStream.Blocking = True

>>SocketStream.Connect(LocalEndPoint)

>>' SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>>SocketOptionName.ReceiveTimeout, 15000)

>>' SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>>SocketOptionName.SendTimeout, 5000)

>>mState = Status.Connected 'Set state flag to connected

>>RaiseEvent StatusChange(Me, Status.Connected)

>>RaiseEvent Connected(Me)

>>' If it all worked out, create stream objects

>>If Not (SocketStream Is Nothing) Then

>>ReceiveData(Nothing)

>>' RaiseEvent ClosedByRemote()

>>Else

>>RaiseEvent SocketError(Me, 7563, "Failed to Establish
>Connection")

>>End If

>>Catch e As SocketException

>>'RaiseEvent StatusChange(Status.Disconnected)

>>If ClosingCOnnection = False Then RaiseEvent SocketError
>(Me, e.ErrorCode,
>>e.Message)

>>End Try

>>End Sub

>>Public Sub Listen()

>>If mState = Status.Disconnected Then

>>ActAsServer = True

>>mState = 1 'Set State flag to Listening

>>RaiseEvent StatusChange(Me, Status.Listening)

>>ThreadPool.QueueUserWorkItem(New
>System.Threading.WaitCallback(AddressOf
>>Listen2))

>>Else

>>RaiseEvent SocketError(Me, 0, "Listening is only allowd
>when the socket is
>>in disconnect state")

>>End If

>>End Sub 'Start listening to socket

>>Private Sub Listen2(ByVal State As Object)

>>'This is called until accept

>>' Dim Listener As Socket

>>'Dim LIP As IPAddress = IPAddress.Parse(LocalIP)

>>Try

>>Dim LocalEndPoint As New IPEndPoint(Dns.Resolve
>(mLocalHost).AddressList(0),
>>mPort)

>>Listener = New Socket(AddressFamily.InterNetwork,
>SocketType.Stream,
>>ProtocolType.Tcp)

>>Listener.Blocking = True 'Only let us listen to this port

>>Listener.Bind(LocalEndPoint)

>>RaiseEvent StatusChange(Me, Status.Listening)

>>Listener.Listen(0)

>>SocketStream = Listener.Accept()

>>Listener.Close()

>>SocketStream.Blocking = True

>>'Stream = New NetworkStream(SocketStream)

>>'LSockReader = New StreamReader(Stream)

>>RaiseEvent StatusChange(Me, Status.Connected)

>>RaiseEvent Connected(Me)

>>mState = 3 'Set State flag to connected

>>ReceiveData(Nothing)

>>Catch e As SocketException

>>If ClosingCOnnection = False Then RaiseEvent SocketError
>(Me, e.ErrorCode,
>>e.Message)

>>End Try

>>End Sub 'Listen Address

>>Public Function CompressString(ByVal str As String) As
>Byte()

>>Dim ms As IO.MemoryStream = New IO.MemoryStream()

>>' attach compressor stream to memory stream

>>Dim sw As C1.C1Zip.C1ZStreamWriter = New
>C1.C1Zip.C1ZStreamWriter(ms)

>>' write data into compressor stream

>>Dim writer As IO.StreamWriter = New IO.StreamWriter(sw)

>>writer.Write(str)

>>' flush any pending data

>>writer.Flush()

>>' return the memory buffer

>>CompressString = ms.GetBuffer()

>>End Function

>>' Send text to remote connection if an error occer
>returen the error msg

>>Public Function SendData(ByVal newText As String) As
>String

>>Dim enc As New System.Text.ASCIIEncoding()

>>'Dim SendBuffer(20480) As Byte

>>Dim CompressedBuffer() As Byte

>>Dim t, t1 As Date

>>Dim i As Integer

>>On Error GoTo Hell

>>If newText = "" Then Err.Raise(666, , "Can't send zero
>lengh text")

>>t = Now

>>CompressedBuffer = CompressString(newText)

>>i = Now.Subtract(t).TotalMilliseconds

>>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>>SocketOptionName.SendTimeout, 5000)

>>'SendBuffer = enc.GetBytes(newText)

>>SocketStream.Send(CompressedBuffer) 'SendBuffer)

>>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>>SocketOptionName.SendTimeout, 0)

>>Return ""

>>Hell:

>>Return Err.Description

>>End Function 'SendData

>>Public Function ExpandString(ByVal buffer As Byte()) As
>String

>>Try

>>' turn buffer into a memory stream

>>Dim ms As MemoryStream = New MemoryStream(buffer)

>>' attach decompressor stream to memory stream

>>Dim sr As C1.C1Zip.C1ZStreamReader = New
>C1.C1Zip.C1ZStreamReader(ms)

>>' read uncompressed data

>>Dim reader As StreamReader = New StreamReader(sr)

>>ExpandString = reader.ReadToEnd()

>>Catch e As Exception

>>Return "Failed to UnZip String " & e.Message

>>End Try

>>End Function

>>Private Sub ReceiveData(ByVal State As Object)

>>Static EchoStr As String

>>Static EchoTimeStamp As Date = Now

>>Dim ts As TimeSpan

>>Dim enc As New System.Text.ASCIIEncoding()

>>Dim ReceiveBuffer(20480), EmptyBuffer(20480), DynaArr()
>As Byte

>>Dim str, strCompress As String

>>Dim i, j As Integer

>>Try

>>' SocketStream.Blocking = True

>>' While (Not SocketStream Is Nothing) AndAlso mState =
>Status.Connected

>>'ReDim ReceiveBuffer()

>>If SocketStream Is Nothing OrElse mState <>
>Status.Connected Then Exit Sub

>>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>>SocketOptionName.ReceiveTimeout, ReciveTimeOut)

>>SocketStream.Receive(ReceiveBuffer)

>>SocketStream.SetSocketOption(SocketOptionLevel.Socket,
>>SocketOptionName.ReceiveTimeout, 0)

>>i = Array.LastIndexOf(ReceiveBuffer, CByte(255))

>>If i <= 0 Then

>>CloseSocket()

>>Exit Sub

>>Else

>>ReDim DynaArr(i)

>>ReceiveBuffer.Copy(ReceiveBuffer, DynaArr, i + 1)

>>str = ExpandString(DynaArr)

>>End If

>>'i = ReceiveBuffer.GetUpperBound(0)

>>' str = enc.GetString(ReceiveBuffer, 0, i)

>>'ReceiveBuffer = EmptyBuffer

>>'str()

>>'str = LSockReader.Read(oneBuffer, 0, oneBuffer.GetLength
>(0))

>>If str.Length = 0 OrElse str.Chars(0) = ChrW(0) Then

>>'RaiseEvent SocketError(666, "Socket Endless Loop
>Bug ,the socket will be
>>cloesed")

>>CloseSocket()

>>Else

>>' ts = Now.Subtract(EchoTimeStamp)

...

read more »



Sat, 25 Jun 2005 00:09:06 GMT  
 TCP/IP
Hi
C1Zip is a zip compression library that is sold by component
one(www.componentone.com)
i am using it to compress the data before sending it to save on network
bandwidth
if you are sending small MSGs or dont have the component just get rid of the
call in the recieve and send  method and send the string as is

modifyed Send  function

Public Function SendData(ByVal newText As String) As String
Dim enc As New System.Text.ASCIIEncoding()
'Dim SendBuffer(20480) As Byte
'Dim CompressedBuffer() As Byte
Dim t, t1 As Date
Dim i As Integer
On Error GoTo Hell
If newText = "" Then Err.Raise(666, , "Can't send zero lengh text")
t = Now
'CompressedBuffer = CompressString(newText)
i = Now.Subtract(t).TotalMilliseconds
SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 5000)
SendBuffer = enc.GetBytes(newText)
SocketStream.Send(SendBuffer)
SocketStream.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 0)

Return ""

Hell:

Return Err.Description

End Function 'SendData

in the recive function dont look for the byte 255 but look for the first
byte that eqaul 0 to know where the msg end ,if you have problem fixing it
tell me and i will rewrite this function also



Sat, 25 Jun 2005 01:31:41 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. TCP/IP not TCP/IPing

2. Setting the Gateway IP (in TCP/IP Settings) via program

3. Tcp/ip or Ip number ?

4. TCP/IP IP address and multiple adapters

5. TCP/IP Messages

6. tcp/ip socket from vba

7. TCP/IP from VBA?

8. VB.NET TCP/IP Communication

9. Connect with MySQL database over TCP/IP

10. TCP/IP communications

11. Change TCP/IP port configuration

12. TCP/IP release renew in VB.net or C#

 

 
Powered by phpBB® Forum Software