Valhalla Legends Forums Archive | .NET Platform | Help with .NET sockets

AuthorMessageTime
ioSys
I need help with an example of how to use sockets in VB.NET.
I want a SIMPLE example!  :)

What I need to know is
(1) how to send a string to bnet
(2) how to receive a string from bnet

I am familiar with how connections to bnet with vb6 works

Hope you guys can give me a simple understandable example :)
October 9, 2004, 8:23 PM
Myndfyr
[url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetsocketsnetworkstreamclasstopic.asp[/url]

[url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOStreamReaderClassTopic.asp[/url]

[url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamwriterclasstopic.asp[/url]

Basically, you can set up a NetworkStream and then use StreamReaders and StreamWriters to read and write (respectively) off of the network stream.
October 9, 2004, 9:51 PM
Imperceptus
Not to sure what this does yet beyond it being related to sockets

MyndFyre edit: Code that you did not write is not welcome here unless it's linked to the original source.  If you don't know what it does, then you definitely did not write it.
October 22, 2004, 7:40 PM
ioSys
oh, I wanted help with sockets, not strings myndfyre =) but its okey. thanks anyway!
July 4, 2005, 8:26 AM
St0rm.iD
What are you talking about? That's _exactly_ what you wanted.
July 5, 2005, 4:44 AM
TehUser
ioSys: Using the TcpClient might be the best bet for you.  From what I've seen, it basically functions like the VB 6 Winsock control.

Dim myClient as System.Net.Sockets.TcpClient
myClient = New System.Net.Sockets.TcpClient()

myClient.Connect()

And so on...  I've never actually used the TcpClient class, but that seems to be the way most recommended for simple clients.
July 5, 2005, 5:53 PM
Networks
[quote author=TehUser link=topic=9068.msg118999#msg118999 date=1120586029]
ioSys: Using the TcpClient might be the best bet for you.  From what I've seen, it basically functions like the VB 6 Winsock control.

Dim myClient as System.Net.Sockets.TcpClient
myClient = New System.Net.Sockets.TcpClient()

myClient.Connect()

And so on...  I've never actually used the TcpClient class, but that seems to be the way most recommended for simple clients.
[/quote]

Use the object NetworkStream and myClient.GetStream() to be able to read and write data from the socket.

I have a question, how do you create events much like VB where you can call a function upon recieving data? The Winsock control allowed for this, how do you make up for it using this object. Is there someway polymorphically?
July 6, 2005, 5:05 PM
shout
[quote author=Networks link=topic=9068.msg119266#msg119266 date=1120669545]
I have a question, how do you create events much like VB where you can call a function upon recieving data? The Winsock control allowed for this, how do you make up for it using this object. Is there someway polymorphically?
[/quote]

I just end up making something like this:
[code]
public event SockEventHandler Received;

private System.Threading.Timer rcv_timer;
private TimerCallback timer_cb;

private rec_start(object n)
{
n = null;
int avil = this.socket.Available;
if (avil > 0)
{
socket.BeginReceive([...]);
}
Received([...]);
}
[/code]
July 6, 2005, 6:10 PM
Networks
[quote author=Shout link=topic=9068.msg119273#msg119273 date=1120673441]
[quote author=Networks link=topic=9068.msg119266#msg119266 date=1120669545]
I have a question, how do you create events much like VB where you can call a function upon recieving data? The Winsock control allowed for this, how do you make up for it using this object. Is there someway polymorphically?
[/quote]

I just end up making something like this:
[code]
public event SockEventHandler Received;

private System.Threading.Timer rcv_timer;
private TimerCallback timer_cb;

private rec_start(object n)
{
n = null;
int avil = this.socket.Available;
if (avil > 0)
{
socket.BeginReceive([...]);
}
Received([...]);
}
[/code]
[/quote]

What the hell is that?
July 6, 2005, 9:55 PM
shout
[quote author=Networks link=topic=9068.msg119316#msg119316 date=1120686904]
[quote author=Shout link=topic=9068.msg119273#msg119273 date=1120673441]
[quote author=Networks link=topic=9068.msg119266#msg119266 date=1120669545]
I have a question, how do you create events much like VB where you can call a function upon recieving data? The Winsock control allowed for this, how do you make up for it using this object. Is there someway polymorphically?
[/quote]

I just end up making something like this:
[code]
public event SockEventHandler Received;

private System.Threading.Timer rcv_timer;
private TimerCallback timer_cb;

private rec_start(object n)
{
n = null;
int avil = this.socket.Available;
if (avil > 0)
{
socket.BeginReceive([...]);
}
Received([...]);
}
[/code]
[/quote]

What the hell is that?
[/quote]

It receives every x amount of milliseconds then triggers an event containing the data, if the alivailable socket data > 0. That is, if you fill in between the lines.
July 6, 2005, 11:16 PM
Myndfyr
Polling is silly IMO.  BeginReceive initiates an I/O completion port (if available) and doesn't require you to have your own threading.
July 6, 2005, 11:59 PM
Networks
[quote author=MyndFyre link=topic=9068.msg119347#msg119347 date=1120694384]
Polling is silly IMO.  BeginReceive initiates an I/O completion port (if available) and doesn't require you to have your own threading.
[/quote]

So how do you do it myndy?
July 9, 2005, 10:38 PM
Myndfyr
[quote author=Networks link=topic=9068.msg119879#msg119879 date=1120948687]
So how do you do it myndy?
[/quote]
[url]http://cvs.sourceforge.net/viewcvs.py/jinxbot/Jinx/Jinx.Core/Net/ConnectionBase.cs?rev=1.1&view=markup[/url]

I'll explain this code on a function-by-function basis.

The ConnectionBase class is intended to provide an implementation of a socket that does everything except send data on its own.  It even does that, but not automatically, i.e., it doesn't have an application-level protocol.  It follows a chain-of-responsibility pattern for filtering incoming and outgoing data, such that any number of filters can be applied to an incoming data stream for decoding of incoming or outgoing data in a form of a linked list.  Any filter down the chain can cancel the data processing of a chunk of data so that new incoming data is appended to the current chunk.

Please note that this code is untested and based primarily on work that I've done previously.

Two constructors are provided -- one that is used to connect to an endpoint (the ConnectionBase(IPEndPoint) member), primarily designed as a client; and one with a Socket instance already initialized (the ConnectionBase(Socket) member), primarily designed as a server connection manager once a client has already established a TCP connection.

The OutgoingQueue property is a strongly-typed priority list (I meant to have an #if NET2...#endif setting for PriorityQueue<Buffer>, but I apparently forgot), which allows outgoing buffers to be queued by priority.  Rocket science, it is not.  :)

The IncoingFilterChain and OutgoingFilterChain properties are there (they should be virtual, d'oh!) for descendant classes to be able to access the filter list.

Methods that begin with Socket_ are Socket asynchronous callbacks.  They don't use polling (although I should probably have a check to Socket.Available in the received callback).

Socket_Connected handles that asynchronous BeginConnect callback.  It initializes a receive "loop," which is only named because it calls itself in its callback.

Socket_DataSent handles the data that has already been sent over the wire.  I store the Buffer object that I sent in the message as part of the callback data, so I can go back and use its data to figure out what I'm doing with that sent data. 

Socket_DataReceived handles any incoming data.  Essentially, in theory, when BeginReceiveLoop makes the call to Socket.BeginReceive, the callback shouldn't be called until there is data waiting in queue.  The goal is to quickly copy data from the queue, restart the receiving loop, and then process the data that has come in. 

OnDataReceiving is a virtual method, but shouldn't be overridden without a really good reason.  This method is responsible for processing all of the data that has come in.  It locks the ConnectionBase object so that its members aren't modified (because it automatically uses a thread pool thread to do the work during an async callback).  It runs the filters, and if the filters come back okay, it calls the virtual and should-be-overridden OnDataReceived method (if you want to turn that into something that would call an event, that's fine, because that's why it's implemented that way -- if you wanted to make a DataReceived event, that's where you would make it be called).

OnDataReceived is empty because that's where the application-layer protocol is implemented in descendant classes.  For example, the BNCS plugin I develop will inherit from the ConnectionBase class, and when data is received, it will call a ParseBNCS function.

OnDataSending is also a virtual method that shouldn't be overridden on a whim.  It has the filter objects process any changes to the data, and then asynchronously sends the data.

OnDataSent, like OnDataReceived, is empty.  In other BNCS-related projects, I've checked the buffer that was stored in the OnDataSent method to see if it was a SID_CHAT message.  If so, *that's* when I added sent messages to my chat display (so that they appeared in the correct order).

Connect() and Disconnect() I hope are adequately self-explanatory.
July 9, 2005, 11:46 PM

Search