-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|637|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Article Home -> Advanced Techniques


 
Scherererer
Created : 13 September 2006
Edited : 08 March 2009
Language : C/C++

Basic Sockets in C#

Gets you a little boost in C# sockets

My first article, and the first article on SoCoder (yay). I hope to expand on this article later, outlining some other topics, and providing full working source code for this tutorial to make an echo client/server. Anyway, let's get started!

I've found it a bit easier in some cases to use the Socket class as opposed to the TcpClient and the UdpClient wrappers that they have available.

when you're defining a TCP socket, you initialize it with these parameters:


eventually we'll all be using AddressFamily.InterNetwork6, but its not common enough atm to be used.

Anyway, after you've initialized the socket, you have to initialize its EndPoint, which is essentially the location that the socket will pull streams from. The first parameter we have to input is the IP address of the host that you want to connect to. if you have a name you want resolved like www.google.com or localhost you have to use the Dns.Resolve() method (in .net 2.0 this method is depreciated for Dns.GetHostEntry()). This method resolves a host name (be it an IP or a computer name or whatever) and you take the returned value of that method and can pull the AddressList[] for all the ip's under that address. You can usually just get away with writing Dns.GetHostEntry("hostname").AddressList[0]; which would return an IP address within the IpAddress class.

The second parameter for the IPEndPoint is the port that you want to use. I'm going to use port 26026 as an example. After getting the end point, we need only to connect to it.


Now we're ready to send a message. To do this, we must first convert the message into bytes, we can do this quite simply by using the Encoding class.


After we've got the bytes, we can send a message to the end point we declared earlier


And now the message is off! Next thing you have to do is receive the message on the host you connected to. In the example i connected to 127.0.0.1, which is a loopback connection, that means that anything you send to that ip address will be sent back to the computer that sent it. Because of the way sockets work, we're able to use the same socket for multiple connections, so, i can use this socket for sending and receiving TCP streams on multiple ports.

We're also going to receive back to the same byteBuffer that we initialized earlier, just to save variables. We're able to reuse the same end point again that we connected to, however you would have to define and connect to a different host if you want to be receiving from a different location than you are sending. After receiving, we simply display it, decoding it using the Encoding class again.


Remember to use try...catch blocks, also to define the objects (before initializing) outside of the try catch block, and initialize within like so...


Hope i didn't miss anything and i hope that helps you get a start.

 

Comments