How to C# Socket Programming using Visual Studio.Net

Learn step by step network programming using C#.Net socket, remoting, WCF and SignalR technology from lots of source code and description. All source code download link available at end of the blog post.

Client Socket Programming using C#.Net for Beginner

Now starts for Client part of socket application. Now I will show a simple client socket with an example:
You have to follow just few steps these are:
I. Create a Ip address object with Server Ip using Dns.
IPAddress []ipAddress= Dns.GetHostAddresses("localhost");

II. Again built an IPEndPoint with that IPAddress with same port of server.
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);

III. Now creates a socket object with three parameter like code.
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.IP);


IV. Now create a byte array to send data to server.
V. Finally send that byte array to server.
clientSock.Send(clientData);

VI. To receive data from server just wait socket using Receive function, when some data received by client socket then it fill a byte array and return a interger value received bytes length.
int len = clientSock.Receive(serverData);

VII. Now close the client socket.

The complete client code is giving below:


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Client_Socket
{
class Program
{
static void Main(string[] args)
{
IPAddress []ipAddress= Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
clientSock.Connect(ipEnd);

string strData = "Message from client end.";
byte[] clientData = new byte[1024];
clientData = Encoding.ASCII.GetBytes(strData);
clientSock.Send(clientData);

byte[] serverData = new byte[1024];
int len = clientSock.Receive(serverData);
Console.WriteLine(Encoding.ASCII.GetString(serverData,0,len));
clientSock.Close();

Console.ReadLine();

}
}
}
All C# blogs

Labels: , , , , , ,

  1. Blogger asif | November 5, 2008 at 2:00 PM |  

    Dear frined, I want to send the data from client to server through socket using Asp.net page, please provide me code.
    thanks.
    asif

  2. Blogger naveen | July 26, 2009 at 5:43 PM |  

    i work for a mnc level5 company as a software engineer in .net technologies.
    We use a third party component to transfer the files @ rate of greater than @20 MB size.
    I have got an idea to replace that component as we are facing many issues with that.So my requirement is i want to build a component susch that it should be a platform independent and it should be able to transfer the files across the organizations.As i believe it could be done using the Socket programming i just want to know the time it takes to transfer the files.i am much worried about it.Let me know anyone could have handled such type of scenarios.

  3. Blogger rc | September 11, 2010 at 4:53 AM |  

    Saludos..... Dejeme decirle que su blog esta espectacular, tiene muy buenos artículos y son muy entendibles, ah! por cierto le felicito por el blog y continue de esa manera contribuyendo con información útil para los visitantes, voy a continuar navenagdo por su blog y le visitare en otras ocasiones. :):):):):):) La entrada ha resultado muy interesante y entrenida, me he quedado un buen rato leyendola, me despido de usted y hasta la proxima oportunidad.

  4. Blogger Suman Biswas | September 13, 2010 at 2:14 PM |  

    RC's comment in English:
    Greetings ..... Let me tell you that your blog is spectacular, has very good
    items and are very understandable, ah! certainly congratulate him on the blog
    thus continue to contribute with useful information for
    visitors, I will continue navenagdo by your blog and will visit other
    occasions. :):):):):):) The entry has been very interesting and
    coach, I've been a good time reading it, I say goodbye to you and
    until the next opportunity.

leave a response