|
-
Mar 13th, 2002, 02:24 PM
#1
Thread Starter
Addicted Member
Socket example
Could someone make me a very simple prog that listens to port 2500, waits for incomming data and puts this data into a messagebox?? I know about the examples that are included with visual studio.net but i dont really get them.
Thnx
-
Mar 13th, 2002, 02:55 PM
#2
ok I make no guarantee that this will work..it is the TCP socket class I am wroking on...but it SHOULD work as is......
First off..here is the class
Code:
Imports System
Imports System.Net.Sockets
Imports System.Text
Namespace Sockets
' All socket connection types
Namespace TCP
' For TCP comm
Public Class TCPServer
' For TCP servers
Private m_Port As Integer
Private tcpConn As TcpClient
Private m_gID As Guid = Guid.NewGuid
Private mobjClient As TcpClient
Private marData(1024) As Byte
Private m_ClientIP As String
Private m_State As Short
Public Event ConnectionRequest()
Public Event Connected(ByVal sender As TCPServer)
Public Event Disconnected(ByVal sender As TCPServer)
Public Event DataReceived(ByVal sender As TCPServer, ByVal Data As String)
Public Sub New(ByVal client As TcpClient)
' A new instance of the class started meaning a new connection
mobjClient = client
RaiseEvent Connected(Me)
mobjClient.GetStream.BeginRead(marData, 0, 1024, _
AddressOf Receive, Nothing)
End Sub
Public Sub SendData(ByVal Data As String)
' Server sending to client. String Data
SyncLock mobjClient.GetStream
Dim w As New IO.StreamWriter(mobjClient.GetStream)
w.Write(Data)
w.Flush()
End SyncLock
End Sub
Priate Sub Receive()
' Start the data receiving process
Dim intCount As Integer
Try
SyncLock mobjClient.GetStream
intCount = mobjClient.GetStream.EndRead(ar)
End SyncLock
If intCount < 1 Then
RaiseEvent Disconnected(Me)
Exit Sub
End If
BuildString(marData, 0, intCount)
SyncLock mobjClient.GetStream
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf Receive, Nothing)
End SyncLock
Catch e As Exception
RaiseEvent Disconnected(Me)
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
Dim intIndex As Integer
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 13 Then
RaiseEvent DataReceived(Me, mobjText.ToString)
mobjText = New StringBuilder()
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Public ReadOnly Property requestID() As String
' A connections requestID to identify it.
Get
Return m_gID.ToString
End Get
End Property
Public Property Port() As Integer
' Port property
Get
Return m_Port
End Get
Set(ByVal Value As Integer)
m_Port = Value
End Set
End Property
Public ReadOnly Property ClientIP() As String
' The ip address of the connected client
Get
Return m_ClientIP
End Get
End Sub
Public ReadOnly Property State() As Short
' The ip address of the connected client
Get
Return m_State
End Get
End Sub
End Class
End Namespace
End Namespace
now give me a minute or 2 to type up the usage......
-
Mar 13th, 2002, 03:02 PM
#3
from form or whatever
Code:
' The imports
Imports System.Net
Imports System.Net.Sockets
Imports Sockets.TCP
Code:
Private mobjListener As TcpListener
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DoListen()
End Sub
Private Sub DoListen()
Try
mobjListener = New TcpListener(2500)
mobjListener.Start()
Do
Dim x As New TCPServer(mobjListener.AcceptTcpClient)
AddHandler x.DataReceived, AddressOf OnLineReceived
Loop Until False
Catch
End Try
End Sub
Private Sub OnDataReceived(ByVal sender As TCPServer, ByVal Data As String)
MessageBox.Show(Data)
End Sub
That SHOULD work...I hope..it would be nice if it does.
-
Mar 13th, 2002, 03:44 PM
#4
Thread Starter
Addicted Member
there seem to be some mistakes in the code, could u put them into a project, zip it and attach it to this post? i would be very thankfull if you did.
-
Mar 13th, 2002, 03:53 PM
#5
what you see is all there is.....Like I said..it has not been tested yet, but I will be as soon as I can.
It was written in notepad so it could be just a bunch of minor things.
-
Mar 13th, 2002, 04:14 PM
#6
Thread Starter
Addicted Member
yeah i fixed the minor mistakes, were just some spelling mistakes.
The last things that needs fixing is this:
mobjText is not declared, what should i declare it as?
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf Receive, Nothing)
Receive aint OK according to VB...
thnx
Last edited by JpEgy; Mar 13th, 2002 at 04:20 PM.
-
Mar 13th, 2002, 04:21 PM
#7
oopps..
Private mobjText As New StringBuilder()
put that with the other declarations in the class
-
Mar 13th, 2002, 04:22 PM
#8
ill have to look into why AddressOf Receive is not ok.
-
Mar 13th, 2002, 04:24 PM
#9
ahhh change the REceive delcaration to
Private Sub Receive(ByVal ar As IAsyncResult)
-
Mar 13th, 2002, 05:22 PM
#10
Thread Starter
Addicted Member
ok thnx, just 1 last question, were do i need to place the namespace? i just put it in a .vb file and it doesn't seem to work cause the main form says that namespace for TCP can't be found.
-
Mar 13th, 2002, 08:49 PM
#11
try compilling the namespace/class to a dll then add a ref to it.
-
Mar 14th, 2002, 10:18 AM
#12
Thread Starter
Addicted Member
OK, i compiled the DLL, included it into thw project and Imported it. Now i found 1 more mistake in the form code
Do
Dim x As New TCPServer(mobjListener.AcceptTcpClient)
AddHandler x.DataReceived, AddressOf OnLineReceived
Loop Until False
OnLineReceived is not declared.
For the rest i found no mistakes, thnx, this helped me very much :-)
-
Mar 14th, 2002, 10:22 AM
#13
that should be OnDataReceived..I thought I had changed that when I posted the class..I guess I didnt 
stupid me......
Please let it work now!!!
-
Mar 14th, 2002, 11:38 AM
#14
Thread Starter
Addicted Member
hmmm it works once, after that u have to restart in order to use it again, otherwise the window doesn;t come up. I think that has to to with the fact that the socket isn't unbind at close or something....
-
Mar 14th, 2002, 11:45 AM
#15
well it partially working makes me very happy....hmmm let me think...
-
Mar 14th, 2002, 11:48 AM
#16
ahh I have an idea..in the form code...put that DoListen into another Thread
Add this Imports
Imports System.Threading
This variable declaration:
Private ListenThread As Thread
and isntead of just DoListen()
do this:
Code:
ListenThread = New Thread(AddressOf DoListen)
ListenThread.Start()
-
Mar 14th, 2002, 11:52 AM
#17
Can you post the class as you have it now so I can record the changes and spelling mistakes you fixed? I will give you credit for the finished product for basically being an alpha tester for it! 
I am about to get .NET reinstalled here at work so I can actually test my own component....
-
Mar 14th, 2002, 12:30 PM
#18
Thread Starter
Addicted Member
ok. BTW, it does allow a client to connect.
Imports System.Net.Sockets
Imports System.Text
Namespace Sockets
' All socket connection types
Namespace TCP
' For TCP comm
Public Class TCPServer
' For TCP servers
Private m_Port As Integer
Private tcpConn As TcpClient
Private m_gID As Guid = Guid.NewGuid
Private mobjClient As TcpClient
Private marData(1024) As Byte
Private m_ClientIP As String
Private m_State As Short
Private mobjText As New StringBuilder()
Public Event ConnectionRequest()
Public Event Connected(ByVal sender As TCPServer)
Public Event Disconnected(ByVal sender As TCPServer)
Public Event DataReceived(ByVal sender As TCPServer, ByVal Data As String)
Public Sub New(ByVal client As TcpClient)
' A new instance of the class started meaning a new connection
mobjClient = client
RaiseEvent Connected(Me)
mobjClient.GetStream.BeginRead(marData, 0, 1024, _
AddressOf Receive, Nothing)
End Sub
Public Sub SendData(ByVal Data As String)
' Server sending to client. String Data
SyncLock mobjClient.GetStream
Dim w As New IO.StreamWriter(mobjClient.GetStream)
w.Write(Data)
w.Flush()
End SyncLock
End Sub
Private Sub Receive(ByVal ar As IAsyncResult)
' Start the data receiving process
Dim intCount As Integer
Try
SyncLock mobjClient.GetStream
intCount = mobjClient.GetStream.EndRead(ar)
End SyncLock
If intCount < 1 Then
RaiseEvent Disconnected(Me)
Exit Sub
End If
BuildString(marData, 0, intCount)
SyncLock mobjClient.GetStream
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf Receive, Nothing)
End SyncLock
Catch e As Exception
RaiseEvent Disconnected(Me)
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
Dim intIndex As Integer
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 13 Then
RaiseEvent DataReceived(Me, mobjText.ToString)
mobjText = New StringBuilder()
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Public ReadOnly Property requestID() As String
' A connections requestID to identify it.
Get
Return m_gID.ToString
End Get
End Property
Public Property Port() As Integer
' Port property
Get
Return m_Port
End Get
Set(ByVal Value As Integer)
m_Port = Value
End Set
End Property
Public ReadOnly Property ClientIP() As String
' The ip address of the connected client
Get
Return m_ClientIP
End Get
End Property
Public ReadOnly Property State() As Short
' The ip address of the connected client
Get
Return m_State
End Get
End Property
End Class
End Namespace
End Namespace
-
Mar 14th, 2002, 12:33 PM
#19
arrghh ...couldnt use the [ code ] tags could ya?

jk
Thanks.
Did the threading make any difference?
-
Mar 14th, 2002, 12:43 PM
#20
Thread Starter
Addicted Member
they have any use? because they are made for VB6 and not for .net...
-
Mar 14th, 2002, 12:45 PM
#21
The indentation....makes it easier to look at...its ok I got it straight now though.
-
Mar 14th, 2002, 03:06 PM
#22
Thread Starter
Addicted Member
when u got it finished could i have it :-> ?
-
Mar 14th, 2002, 03:16 PM
#23
here are the changes I have made.
Added a Connect method for use as a client
Added an Error event
Added State property
changed the class name to TCPConnection
make sure you cahnge TCPServer to TCPConnection in your form code
Code:
' Thanks goes out to my hommies at www.vbforums.com!
' Special Thanks To:
' JpEgy for fixing many minor mistakes and being the first tester.
Imports System
Imports System.Net.Sockets
Imports System.Text
Namespace Sockets
' All socket connection types
Namespace TCP
Public Class TCPConnection
' For TCP connections intercepted by the TcpListener
Private tcpConn As TcpClient
Private m_requestID As Guid = Guid.NewGuid
Private objClient As TcpClient
Private arData(1024) As Byte
Private m_ClientIP As String
Private m_State As Short
Private objText As New StringBuilder()
Public Event Connected(ByVal sender As TCPConnection)
Public Event Disconnected(ByVal sender As TCPConnection)
Public Event DataReceived(ByVal sender As TCPConnection, ByVal Data As String, ByVal BytesReceived As Long)
Public Event Error(ByVal strMessage As String)
Public Sub New(ByVal client As TcpClient)
' A new instance of the class started meaning a new connection
objClient = client
RaiseEvent Connected(Me)
m_State = 1
objClient.GetStream.BeginRead(arData, 0, 1024, _
AddressOf Receive, Nothing)
End Sub
Public Sub SendData(ByVal Data As String)
' Server sending to client. String Data
m_State = 3
SyncLock objClient.GetStream
Dim w As New IO.StreamWriter(objClient.GetStream)
w.Write(Data)
w.Flush()
End SyncLock
m_State = 1
End Sub
Private Sub Receive(ByVal ar As IAsyncResult)
' Start the data receiving process
Dim intCount As Integer
Try
SyncLock objClient.GetStream
intCount = objClient.GetStream.EndRead(ar)
End SyncLock
If intCount < 1 Then
RaiseEvent Disconnected(Me)
m_State = 0
Exit Sub
End If
BuildString(arData, 0, intCount)
SyncLock objClient.GetStream
objClient.GetStream.BeginRead(arData, 0, 1024, AddressOf Receive, Nothing)
End SyncLock
Catch e As Exception
RaiseEvent Disconnected(Me)
m_State = 0
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
Dim intIndex As Integer
m_State = 2
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 13 Then
RaiseEvent DataReceived(Me, objText.ToString. objText.Length)
objText = New StringBuilder()
Else
objText.Append(ChrW(Bytes(intIndex)))
End If
Next
m_State = 1
End Sub
Public ReadOnly Property requestID() As String
' A connections requestID to identify it.
Get
Return m_gID.ToString
End Get
End Property
Public ReadOnly Property ClientIP() As String
' The ip address of the connected client
Get
Return m_ClientIP
End Get
End Property
Public ReadOnly Property State() As Short
' The ip address of the connected client
' Possible values:
' 0 - Disconnected
' 1 - Connected And Ready
' 2 - Receiving
' 3 - Sending
Get
Return m_State
End Get
End Property
Public Sub Connect(ByVal strDomain As String, ByVal intPort As Integer)
Try
objClient = New TcpClient(strDomain, intPort)
RaiseEvent Connected(Me)
objClient.ReceiveBufferSize = 1024
objClient.NoDelay = True
Catch e As Exception
RaiseEvent Error(e.Message.ToString)
End Try
End Sub
End Class
End Namespace
End Namespace
-
Mar 14th, 2002, 05:44 PM
#24
Thread Starter
Addicted Member
Public Event Error(ByVal strMessage As String)
was not allowed, i changed it into
Public Event Errorl(ByVal strMessage As String)
and changed
RaiseEvent Errorl(e.Message.ToString)
into
RaiseEvent Errorl(e.Message.ToString)
just 1 thing left that i can't get done :-|,
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 13 Then
RaiseEvent DataReceived(Me, objText.ToString.objText.Length)
objText = New StringBuilder()
Else
objText.Append(ChrW(Bytes(intIndex)))
End If
Next
it says that objText aint a member of string....
Could u fix this plz?? thnx
-
Mar 14th, 2002, 06:14 PM
#25
Me, objText.ToString.objText.Length)
oops should be a comma between ToString and the second objText
objText.Length should return the BytesRecieved in the Event
-
Mar 15th, 2002, 06:51 AM
#26
Thread Starter
Addicted Member
OK, last thing (i hope :-))
m_gID is not declared
-
Mar 15th, 2002, 09:29 AM
#27
change m_gID to m_requestID
I just changed some variable names to reflict better what they where for
-
Mar 15th, 2002, 08:01 PM
#28
Thread Starter
Addicted Member
OK, here's an update again :-)
Do
Dim x As New TCPConnection(mobjListener.AcceptTcpClient)
AddHandler x.DataReceived, AddressOf OnDataReceived
Loop Until False
OnDataReceived doesnt have the same signature as the sub itself, that's (in short) what the IDE tells me....
-
Mar 17th, 2002, 05:16 PM
#29
Thread Starter
Addicted Member
could u plz fix this i wanna go on with my prog :->...
-
Mar 18th, 2002, 02:52 PM
#30
check that the parameters for you OnDataRecceived sub coincides with the DataReceived event...Remember we added that thrid parameter for BytesReceived....
so I assume from the error, you Sub in the main form only still has the 2 orginal parameters... Add ByVal BytesReceived As Long for the third one.
-
Mar 18th, 2002, 03:04 PM
#31
Thread Starter
Addicted Member
doesnt work :-( it doesn't bring up the window.... i recompiled the DLL and i restarted the IDE and stuff but it doesn;t give me a window...
-
Mar 18th, 2002, 03:09 PM
#32
well try some debugging with breakpoints and all that and see where it goes and what it does...Unfortunatly that is the best I can offer until I can get to a point to be able to intall .NET here so I can start testing...
-
Mar 18th, 2002, 03:50 PM
#33
Thread Starter
Addicted Member
then i will wait for u :-)
the error has to be somewhere in the DLL so it's hard to debug...
-
Mar 18th, 2002, 03:56 PM
#34
i think now that i think about it, if you just add the class to the project and dont put in that imports I mentioned before, it should work then., then it can be debugged....Ill try to get .NET installed soon to test all this.
-
Mar 18th, 2002, 07:17 PM
#35
Fanatic Member
ahem...
what happened to winsock? heh... i liked my winsock control.. kept things nice n simple..
i'm assuming you guys are working on some kind of wrapper for this code?
-
Mar 19th, 2002, 05:08 PM
#36
ok made some changes to the class and tested myself..it works
Code:
' Thanks goes out to my hommies at www.vbforums.com!
' Special Thanks To:
' JpEgy for fixing many minor mistakes and being the first tester.
Imports System
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Sockets
' All socket connection types
Namespace TCP
Public Class TCPConnection
' For TCP connections intercepted by the TcpListener
Private tcpConn As TcpClient
Private m_requestID As Guid = Guid.NewGuid
Private objClient As TcpClient
Private arData(1024) As Byte
Private m_ClientIP As String
Private m_State As Short
Private objText As New StringBuilder()
Public Event Connected(ByVal sender As TCPConnection)
Public Event Disconnected(ByVal sender As TCPConnection)
Public Event DataReceived(ByVal sender As TCPConnection, ByVal Data As String, ByVal BytesReceived As Long)
Public Event SockError(ByVal strMessage As String)
Public Sub New(ByVal client As TcpClient)
' A new instance of the class started meaning a new connection
objClient = client
RaiseEvent Connected(Me)
Console.Writeline("Test")
m_State = 1
objClient.GetStream.BeginRead(arData, 0, 1024, _
AddressOf Receive, Nothing)
End Sub
Public Sub SendData(ByVal Data As String)
' Server sending to client. String Data
m_State = 3
SyncLock objClient.GetStream
Dim w As New IO.StreamWriter(objClient.GetStream)
w.Write(Data)
w.Flush()
End SyncLock
m_State = 1
End Sub
Private Sub Receive(ByVal ar As IAsyncResult)
' Start the data receiving process
Dim intCount As Integer
Console.Writeline("Received")
Try
SyncLock objClient.GetStream
intCount = objClient.GetStream.EndRead(ar)
End SyncLock
If intCount < 1 Then
RaiseEvent Disconnected(Me)
m_State = 0
Exit Sub
End If
BuildString(arData, 0, intCount)
SyncLock objClient.GetStream
objClient.GetStream.BeginRead(arData, 0, 1024, AddressOf Receive, Nothing)
End SyncLock
Catch e As Exception
RaiseEvent SockError(e.Message.ToString)
RaiseEvent Disconnected(Me)
m_State = 0
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
Dim intIndex As Integer
Dim txt As String
Console.Writeline("BuildString")
m_State = 2
objText = New StringBuilder()
For intIndex = offset To offset + count - 1
txt = ChrW(Bytes(intIndex))
objText.Append(txt)
Next
RaiseEvent DataReceived(Me, objText.ToString, objText.Length)
m_State = 1
End Sub
Public ReadOnly Property requestID() As String
' A connections requestID to identify it.
Get
Return m_requestID.ToString
End Get
End Property
Public ReadOnly Property ClientIP() As String
' The ip address of the connected client
Get
Return m_ClientIP
End Get
End Property
Public ReadOnly Property State() As Short
' The ip address of the connected client
' Possible values:
' 0 - Disconnected
' 1 - Connected And Ready
' 2 - Receiving
' 3 - Sending
' 4 - Connecting
Get
Return m_State
End Get
End Property
Public Sub Connect(ByVal strDomain As String, ByVal intPort As Integer)
m_State = 4
Try
objClient = New TcpClient(strDomain, intPort)
RaiseEvent Connected(Me)
m_State = 1
objClient.ReceiveBufferSize = 1024
objClient.NoDelay = True
Catch e As Exception
RaiseEvent SockError(e.Message.ToString)
m_State = 0
End Try
End Sub
End Class
End Namespace
End Namespace
-
Mar 19th, 2002, 05:09 PM
#37
there are some console.writelines in the class I used for debugging..remove them if you wish..
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
|