472,794 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

sockets

hi!

i'm really not into networking at all and have now been asigned the task of
porting a vb6-code into vb.net (compact framework, in this case) and the
code uses the winsock-control. i quickly found out that .net uses
system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse me...
:o| and help would be really great! links, code, wrappers, tips,
whateveryougot...

one of my main problems, i guess, is: why don't sockets raise events like
mysock_gotdata()?! (or like vb6's winsock...) wouldn't that be usefull in
any case? why not have it as part of the framework?

i found sample code where the "receiving" is done in a "while true" loop -
with no exit; except possibly by catching an error, which was done outside
the loop (but i don't think so). wouldn't the program "hang" in it's endless
loop? i found no sign of or any comment on any "multi-threading", but i'm
reallyreally not into that - is it part of the solution?

coming freshly from vb6, vb.net is a joy! with quick and cool results...
....and now this ;o]...

thanks,
m.
Nov 21 '05 #1
3 4225
I'm pretty new at sockets like you but what I found is that some of the
calls (like updclient.receive) is a blocking call which just assigns the
socket data to an object. I don't use much of the TCP but I would assume it
would be similar with the blocking. I also use an endless loop to receive
the data and simply put the procedure in a thread. Every time I run into an
error it does stop inside the thread and I use the try..catch to trap it

Probably didn't do much to answer your question...just my 2cents.

"Michael Maercker" <mi**********@ish.de> wrote in message
news:4251affe@news-fe-01...
hi!

i'm really not into networking at all and have now been asigned the task
of
porting a vb6-code into vb.net (compact framework, in this case) and the
code uses the winsock-control. i quickly found out that .net uses
system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse
me...
:o| and help would be really great! links, code, wrappers, tips,
whateveryougot...

one of my main problems, i guess, is: why don't sockets raise events like
mysock_gotdata()?! (or like vb6's winsock...) wouldn't that be usefull in
any case? why not have it as part of the framework?

i found sample code where the "receiving" is done in a "while true" loop -
with no exit; except possibly by catching an error, which was done outside
the loop (but i don't think so). wouldn't the program "hang" in it's
endless
loop? i found no sign of or any comment on any "multi-threading", but i'm
reallyreally not into that - is it part of the solution?

coming freshly from vb6, vb.net is a joy! with quick and cool results...
...and now this ;o]...

thanks,
m.

Nov 21 '05 #2
sockets do raise event download the sample
http://msdn.microsoft.com/vbasic/dow...s/default.aspx

"Michael Maercker" <mi**********@ish.de> wrote in message
news:4251affe@news-fe-01...
hi!

i'm really not into networking at all and have now been asigned the task
of
porting a vb6-code into vb.net (compact framework, in this case) and the
code uses the winsock-control. i quickly found out that .net uses
system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse
me...
:o| and help would be really great! links, code, wrappers, tips,
whateveryougot...

one of my main problems, i guess, is: why don't sockets raise events like
mysock_gotdata()?! (or like vb6's winsock...) wouldn't that be usefull in
any case? why not have it as part of the framework?

i found sample code where the "receiving" is done in a "while true" loop -
with no exit; except possibly by catching an error, which was done outside
the loop (but i don't think so). wouldn't the program "hang" in it's
endless
loop? i found no sign of or any comment on any "multi-threading", but i'm
reallyreally not into that - is it part of the solution?

coming freshly from vb6, vb.net is a joy! with quick and cool results...
...and now this ;o]...

thanks,
m.

Nov 21 '05 #3
Option Explicit On
Option Strict Off

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class StateObject
Public workSocket As Socket = Nothing
Public BufferSize As Integer = 32767
Public buffer(32767) As Byte
Public sb As New StringBuilder
End Class

Public Class SockFactory
Public Event onConnect()
Public Event onError(ByVal Description As String)
'Public Event onDataArrival(ByVal Data As Byte(), ByVal
TotalBytes As Integer)
Public Event onDataArrival(ByVal Data As String, ByVal
TotalBytes As Integer)
Public Event onDisconnect()
Public Event onSendComplete(ByVal DataSize As Integer)

Private Shared Response As [String] = [String].Empty
Private Shared Port As Integer = 44
Private Shared ipHostInfo As IPHostEntry = Dns.Resolve("localhost")
Private Shared ipAddress As ipAddress = ipHostInfo.AddressList(0)
Private Shared Client As New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim clsReader As New PirateChat.clsReadWriteXML
Public bConnected As Boolean
Private mConnected As Boolean
#Region "Socketconnted "
Private Sub sockConnected(ByVal ar As IAsyncResult)
Try
If Client.Connected = False Then RaiseEvent
onError("Connection refused.") : Exit Sub
Dim state As New StateObject
state.workSocket = Client
Client.BeginReceive(state.buffer, 0, state.BufferSize,
0, AddressOf sockDataArrival, state)
RaiseEvent onConnect()
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub
#End Region
#Region " DataArrival "

Private Sub sockDataArrival(ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
Dim bytesRead As Integer
' If mConnected = True Then Exit Sub
Try
bytesRead = client.EndReceive(ar)
Catch
Exit Sub
End Try

Try
Dim Data() As Byte = state.buffer
If bytesRead = 0 Then
client.Shutdown(SocketShutdown.Both)
client.Close()
RaiseEvent onDisconnect()
Exit Sub
End If
ReDim state.buffer(32767)

client.BeginReceive(state.buffer, 0, state.BufferSize,
0, AddressOf sockDataArrival, state)
RaiseEvent
onDataArrival(ASCIIEncoding.ASCII.GetString(Data), bytesRead)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try

End Sub
#End Region
#Region "Connect "
'Socket programming in .NET is made possible by the Socket class
present inside the System.Net.Sockets namespace. This Socket class has
several method and properties and a constructor. The first step is to
create an object of this class. Since there is only one constructor we
have no choice but to use it.
'The first parameter is the address family which we will use, in
this case, interNetwork (which is IP version 4) - other options include
Banyan NetBios, AppleTalk etc. (AddressFamily is an enum defined in
Sockets namespace). Next we need to specify socket type: and we would
use reliable two way connection-based sockets (stream) instead of
un-reliable Connectionless sockets (datagrams) . So we obviously specify
stream as the socket type and finally we are using TCP/IP so we would
specify protocol type as Tcp.

'Once we have created a Socket we need to make a connection to
the server since we are using connection-based communication. To connect
to the remote computer we need to know the IP Address and port at which
to connect. In .NET there is a class under System.Net namespace called
IPEndPoint which represents a network computer as an IP address and a
port number. The IPEndPoint has two constructors - one that takes a IP
Address and Port number and one that takes long and port number.

Public Sub Connect(ByVal RemoteHostName As String, ByVal
Remoteport As Integer)
Try
Client = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Port = Remoteport
ipHostInfo = Dns.Resolve(RemoteHostName)
ipAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, Port)
Client.BeginConnect(remoteEP, AddressOf sockConnected,
Client)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try

End Sub
#End Region
#Region "Disconnect "
Public Sub Disconnect()
Try
'Kill the socket.
Client.Shutdown(SocketShutdown.Both)
'Kill any data being sent or received.
'let the functions know that we are disconnecting()
'this prevents data being sent after the socket closes
'apparently .Shutdown doesn't work as documented()
'this is the work(around)
mConnected = False
'Kill socket
Client = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Catch : End Try
End Sub
#End Region
#Region "SockSendEnd "
Private Sub sockSendEnd(ByVal ar As IAsyncResult)
Try
Dim client As Socket = CType(ar.AsyncState, Socket)
Dim bytesSent As Integer = client.EndSend(ar)
RaiseEvent onSendComplete(bytesSent)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub
#End Region
#Region " SendData "
Public Sub SendData(ByVal Data() As Byte)
Try
Dim byteData As Byte() = Data
Client.BeginSend(byteData, 0, byteData.Length, 0,
AddressOf sockSendEnd, Client)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub
#End Region
#Region "StringToBytes "
Public Function StringToBytes(ByVal Data As String) As Byte()
StringToBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
End Function
#End Region
#Region "BytesToString "
Public Function BytestoString(ByVal Data As Byte()) As String
BytestoString = System.Text.ASCIIEncoding.ASCII.GetString(Data)
End Function
#End Region
#Region "Connected "
Public Function Connected() As Boolean
Try
Return Client.Connected
Catch
RaiseEvent onError(Err.Description)
Exit Function
End Try
End Function
#End Region

End Class

regards

Michael Maercker wrote:
hi!

i'm really not into networking at all and have now been asigned the task of
porting a vb6-code into vb.net (compact framework, in this case) and the
code uses the winsock-control. i quickly found out that .net uses
system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse me...
:o| and help would be really great! links, code, wrappers, tips,
whateveryougot...

one of my main problems, i guess, is: why don't sockets raise events like
mysock_gotdata()?! (or like vb6's winsock...) wouldn't that be usefull in
any case? why not have it as part of the framework?

i found sample code where the "receiving" is done in a "while true" loop -
with no exit; except possibly by catching an error, which was done outside
the loop (but i don't think so). wouldn't the program "hang" in it's endless
loop? i found no sign of or any comment on any "multi-threading", but i'm
reallyreally not into that - is it part of the solution?

coming freshly from vb6, vb.net is a joy! with quick and cool results...
...and now this ;o]...

thanks,
m.


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
3
by: Logan McKinley | last post by:
I have a C# program that uses blocking sockets and want to allow the user to stop the server. The problem I am having is the socket blocks on...
1
by: Adam Clauss | last post by:
I am (attempting) to move an existing socket application to use raw sockets. Right now, my application is essentially a port forwarder. Upon receiving a connection, it will open a connection to...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
7
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.