The classes in the System.Net.Sockets namespace provide sockets support in .NET.
The System.Net.Sockets contains the following Properties,
AddressFamily
Gets the address family of the socket. The value is from the Socket.
AddressFamily enumeration.
Available
Returns the amount of available data to read.
Blocking
Gets or sets the value that indicates whether the socket is in blocking mode.
Connected
Returns the value that informs whether the socket is still connected
with the remote host.
LocalEndPoint
Gets the local endpoint.
ProtocolType
Get the protocol type of the socket.
RemoteEndPoint
Gets the remote endpoint of a socket.
SocketType
Gets the type of the socket.
SupportsIPv4
Returns a Boolean value indicating whether IPv4 is available on the current host.
This property is supported in only version 1.1 of the Framework.
SupportsIPv6
Returns a Boolean value indicating whether IPv6 is supported and available
on the current host. This property is supported in only version 1.1 of the Framework.
The System.Net.Sockets Contains the following methods,
Accept
Creates a new socket to handle the incoming connection request.
Bind
Associates a socket with the local endpoint for listening for incoming connections.
Close
Forces the socket to close itself.
Connect
Establishes a connection with the remote host.
GetSocketOption
Returns the value of a SocketOption.
IOControl
Sets low-level operating modes for the socket. This method provides low-level access to the underlying socket instance of the Socket class.
Listen
Places the socket in listening mode. This method is exclusively used to server applications.
Receive
Receives data from a connected socket.
Poll
Determines the status of the socket.
Select
Checks the status of one or more sockets.
Send
Sends data to the connected socket.
SetSocketOption
Sets a SocketOption.
Shutdown
Disables send and receive functions on a socket.
See the below examble,
Simply the Server is responding to client using Socket
Programming.
using System;
using System.Net.Sockets; //Namespace Supports Socket Programming
using System.Net;
using System.Text;
public class SocketServer
{
public static void Main(string [] args)
{
// Establish the local endpoint for the socket
IPHostEntry ipHost = Dns.Resolve(“localhost”);
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
// Create a TCP/IP socket
Socket sListener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen to the incoming sockets
try
{
sListener.Bind(ipEndPoint);
sListener.Listen(10);
// Start listening for connections
while (true)
{
Console.WriteLine(“Waiting for a connection on port {0}”,ipEndPoint);
// Program is suspended while waiting for an incoming connection
Socket handler = sListener.Accept();
string data = null;
// We got the client attempting to connect
while(true)
{
byte[] bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf(“<TheEnd>”) > -1)
{
break;
}
}
// Show the data on the console
Console.WriteLine(“Text Received: {0}”,data);
string theReply = “Thank you for those ” + data.Length.ToString()
+ ” characters…”;
byte[] msg = Encoding.ASCII.GetBytes(theReply);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
} // End of Main
}
Now,i hope you got atleast a basic idea of socket Programming,
Happy Coding
I enjoyed viewing your blog and I will be back to check it more in the future so please keep up your good quality work. I love the colors that you chose, you are quite talented!
Awesome work! Very inspiring!
Great blog , i will bookmark it right now !
Nice post…Thank you for sharing some good things.
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.
Well, I have been reading your blog posts daily and the reason I come on your blog frequently is its compelling content… Regards…
Guys, Great article and very very interesting blog. That’s one thing I’m really looking forward. Looking forward to reading more from you next week.
I feel you are too good to write Genius!Thanks for posting, maybe we can see more on this.
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.
Fantastic website. Keep undertaking.
Hi there! We are seeking for incoming writers, may you be attracted? This situation isn’t going to make you wealthy nonetheless there is an interesting pay and if you absolutely take pleasure in freelance writing then that gig is for you.
This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!
Your web blog is recording glitches on my Windows internet browser.
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.
Thank you for the sensible critique. Me & my neighbour were preparing to do some research about that. We got a good book on that matter from our local library and most books where not as influensive as your information.
You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material
How are you ? ! Do you maintain some variety of gift box where I might transmit donation in PayPal?
Hello sir,thanks for yours support, please use the donate button in header,it helps me to grow further.Thanks
I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.
I truly liked this post, this one is going right into my stumble upon account
I enjoy the way you catch the substance of the idea, truly great writting layout, I enojoyed it!
I’ve really enjoyed reading your articles. You obviously know what you are talking about! Your site is so easy to navigate too, I’ve bookmarked it in my favourites. . . . .
It’s good to see this information in your post, i was looking the same but there was not any proper resource, thanx now i have the link which i was looking for my research. . . . . .
Very good post. Hope to see more excellent posts in the future.
I really love your post. If it’s not too much trouble, can you perhaps show how to write works such as this. Trevor Crookshanks
I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.
Great news mann !
Im confident you do not know me so that well I do not know you. nonetheless I really like with regards to all you possess presented from the article higher than. whenever I really could uncover places to obtain inspiration to jot down that good. I am a critic in my america, but rarely I find penning this healthy. frankly I’m jealous as in you. by the way May I have your email address can I confer with? I would like to speak using your firm. say thanks a ton