Showing posts with label Client Socket. Show all posts
Showing posts with label Client Socket. Show all posts

Friday, November 2, 2007

Synchronous Client Socket using Network Stream in C# .Net Programming

Hi frends,I’m back again with two sample socket application. These two project also for beginner’s in C#.Net Socket world. One of these is Server socket application and another is Client socket application. Both application has used NetworkStream class to send and receive data between Client and Server. These two has written based on previous articles on Socket programming and these are very easy to learn. I hope any one can understood these very easyly. If you feel any problem then please reply me via comment I will response these.

Here Client socket with Network stream in C# .Net has written and in different blog post contains the Server application. Both two has written based on synchronous communication mode, but in future I will show you, how you can built asynchronous socket application by these synchronous socket. So lets starts code.


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace NetworkStreamSocketClient
{
class Program
{
static void Main(string[] args)
{
IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
clientSocket.Connect(ipEnd);

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

//Difference is here only, use of Network Stream class.
//Just create a object of Network Stream class with your estublished socket, then use it.
NetworkStream ns = new NetworkStream(clientSocket);

//Write function writes all bytes to send receiver end of connected socket.
ns.Write(clientData, 0, clientData.Length);

//Flush() use to clear all data from Stream object to future use.
ns.Flush();

byte[] serverData = new byte[1024];
//Now stream object wait for receive response from other end.
int length = ns.Read(serverData, 0, serverData.Length);
Console.WriteLine(Encoding.ASCII.GetString(serverData, 0, length));
//Communication completed, now free all resource
ns.Close();
clientSocket.Close();
Console.ReadLine();
}
}
}


Saturday, September 29, 2007

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