C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

socket using asp.net 2.0 for client - server communication


Posted Date: 21 Nov 2008      Posted By: prasannakumar      Member Level: Gold     Points: 1   Responses: 2



hi friends,
i need a source code for socket programming like client-server communication using asp.net?


Regards
K.Prasannakumar





Responses

Author: Praveen    21 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

http://www.devarticles.com/c/a/C-Sharp/Socket-Programming-in-C-sharp-Part-II/1/

hope this will help u



Author: Venkat Tammineni    21 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

hi,
Socket Programming using .Net 2.0 System.Net.Socket

Socket is a communication mechanism where we use to connect client and the server. Basically the Server will listen to the client connections on specific port number. A client can connect to the Server by using the relevant hostname and the port number. Upon the successful client connection server will gets a new socket bound to a different port. It needs the original socket for listen to other client connections while communicating with the connected client. Below is the simple client /server example done using the .Net 2.0. Review the code & you will find it interesting. (I made it very simple to make you understand) You can modify the code to meet your needs.

Server Code:

class MyServer
{
//Listing to the client connections.
public void Listen()
{
byte[] ipArray = new byte[4];
ipArray[0] = 127;
ipArray[1] = 0;
ipArray[2] = 0;
ipArray[3] = 1;
IPAddress add = new IPAddress(ipArray);
//This is obselete in .Net 2.0
//TcpListener tcpListener = new TcpListener(10);

TcpListener tcpListener = new TcpListener(add, 10);
tcpListener.Start();
Socket socketForClient = tcpListener.AcceptSocket();
if (socketForClient.Connected)
{
Util.Out("client connected..");
NetworkStream netStream = new NetworkStream(socketForClient);
StreamWriter writer = new StreamWriter(netStream);
StreamReader reader = new StreamReader(netStream);
string str = "Message from Server : " + DateTime.Now.ToLongDateString();
writer.WriteLine(str);
Util.Out("What meessage do you want to send to client?");
writer.WriteLine(Console.ReadLine());
writer.Flush();
str = reader.ReadLine();
Util.Out(str);
reader.Close();
writer.Close();
netStream.Close();
}
socketForClient.Close();
Util.Out("Exiting...");
}
}

Client Code :

public class MyClient
{
//Connects to the server
public void Connect()
{
TcpClient socketForServer = null;
try
{
socketForServer = new TcpClient("localhost", 10);
Util.Out("Connected to the Server");
}
catch
{
Util.Out("Failed to connecto localhost");
Util.Out("Exiting..");
return;
}
NetworkStream netstream = socketForServer.GetStream();
StreamReader reader = new StreamReader(netstream);
StreamWriter writer = new StreamWriter(netstream);
try
{
string output1, output2;
output1 = reader.ReadLine();
output2 = reader.ReadLine();
Util.Out(output1);
Util.Out(output2);
writer.Flush();
reader.Close();
}
catch
{
Util.Out("Exception Occured! Exiting...");
}
netstream.Close();
}
}

This is small helper class…
public class Util
{
public static void Out(string s)
{
Console.WriteLine(s);
}
}

This is a simple console application. You can create two console application one for the server & one for the client. Run the server first then client app. If you want you can create nice windows applications using this two simple classes to communicate over network.
Thanks



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : how to add a value from a gridview with radio button and that value was selected by defau
Previous : Doubt about website application
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use