• Welcome to Valhalla Legends Archive.
 

Help with .NET sockets

Started by ioSys, October 09, 2004, 03:23 PM

Previous topic - Next topic

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 :)

MyndFyre

QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Imperceptus

#2
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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

ioSys

#3
oh, I wanted help with sockets, not strings myndfyre =) but its okey. thanks anyway!

Banana fanna fo fanna

What are you talking about? That's _exactly_ what you wanted.

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.

Networks

Quote from: TehUser on July 05, 2005, 12:53 PM
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.

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?

shout

Quote from: Networks on July 06, 2005, 12:05 PM
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?

I just end up making something like this:

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([...]);
}

Networks

Quote from: Shout on July 06, 2005, 01:10 PM
Quote from: Networks on July 06, 2005, 12:05 PM
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?

I just end up making something like this:

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([...]);
}


What the hell is that?

shout

Quote from: Networks on July 06, 2005, 04:55 PM
Quote from: Shout on July 06, 2005, 01:10 PM
Quote from: Networks on July 06, 2005, 12:05 PM
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?

I just end up making something like this:

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([...]);
}


What the hell is that?

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.

MyndFyre

Polling is silly IMO.  BeginReceive initiates an I/O completion port (if available) and doesn't require you to have your own threading.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Networks

Quote from: MyndFyre on July 06, 2005, 06:59 PM
Polling is silly IMO.  BeginReceive initiates an I/O completion port (if available) and doesn't require you to have your own threading.

So how do you do it myndy?

MyndFyre

Quote from: Networks on July 09, 2005, 05:38 PM
So how do you do it myndy?
http://cvs.sourceforge.net/viewcvs.py/jinxbot/Jinx/Jinx.Core/Net/ConnectionBase.cs?rev=1.1&view=markup

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.