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
3 comments:
Dear frined, I want to send the data from client to server through socket using Asp.net page, please provide me code.
thanks.
asif
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.
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.
Post a Comment