Socket Programming in ASP.Net using C#
// This a class having two metnods 1) Server 2)Client
// You Should both the method for get request and Sent response
// This is a Server Part
///Create Server For Socket Programming
This code use to create server in Socket Programming.
private static void Server()
{
try
{
String str = "";
TcpListener clientListener;
#region TcpServer Code
try
{
clientListener = new TcpListener(9060);
clientListener.Start();
if (!clientListener.Pending())
{
Socket mySocket = clientListener.AcceptSocket();
Byte[] recieveBuff = new Byte[255];
try
{
mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None);
str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim();
GlobleProperty.GetValueForServer += str;
}
catch (Exception ex)
{
str = ".";
}
clientListener.Stop();
mySocket.Disconnect(true);
}
clientListener.Stop();
}
catch (Exception ex)
{
//MessageBox.Show("not Find a new Connection", ex.Message);
}
#endregion
}
catch (Exception e)
{
}
}
If you many system connected in tcp/Ip Socket programming then we use this function to handle the many request.
public static void CallServer()
{
try
{
Thread newthread = new Thread(new ThreadStart(Server));
newthread.Start();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
//Socket Client Code For Socket Programing
This code use to send to any string type data to another system which is connected through Socket programing.
public static string Client(string textvalue)
try
{
TcpClient serverListener;
DataSet ds = new DataSet();
cls_connection Con = new cls_connection();
//DataSet ds1 = new DataSet();
//ds1 = Con.select_with_SP("Get_Next_IP");
////GlobleProperty.NextIP =
string SelectQuery = "Select MemberSoftwareId from Table_status where TableId=1";
ds = Con.select_data_ds(SelectQuery);
if (ds.Tables[0].Rows.Count > 0)
{
int count = ds.Tables[0].Rows.Count;
for (int i = 0; i < ab =" Convert.ToString(ds.Tables[0].Rows[i][" serverlistener =" new" readstream =" serverListener.GetStream();" sendbuffersize =" 1;" str =" textvalue;" str =" str" str =" str" sendbuff =" System.Text.Encoding.ASCII.GetBytes(str);" str =" textvalue;" showcards = "N" id="gwProxy" type="hidden">
// You Should both the method for get request and Sent response
// This is a Server Part
///Create Server For Socket Programming
This code use to create server in Socket Programming.
private static void Server()
{
try
{
String str = "";
TcpListener clientListener;
#region TcpServer Code
try
{
clientListener = new TcpListener(9060);
clientListener.Start();
if (!clientListener.Pending())
{
Socket mySocket = clientListener.AcceptSocket();
Byte[] recieveBuff = new Byte[255];
try
{
mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None);
str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim();
GlobleProperty.GetValueForServer += str;
}
catch (Exception ex)
{
str = ".";
}
clientListener.Stop();
mySocket.Disconnect(true);
}
clientListener.Stop();
}
catch (Exception ex)
{
//MessageBox.Show("not Find a new Connection", ex.Message);
}
#endregion
}
catch (Exception e)
{
}
}
If you many system connected in tcp/Ip Socket programming then we use this function to handle the many request.
public static void CallServer()
{
try
{
Thread newthread = new Thread(new ThreadStart(Server));
newthread.Start();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
//Socket Client Code For Socket Programing
This code use to send to any string type data to another system which is connected through Socket programing.
public static string Client(string textvalue)
try
{
TcpClient serverListener;
DataSet ds = new DataSet();
cls_connection Con = new cls_connection();
//DataSet ds1 = new DataSet();
//ds1 = Con.select_with_SP("Get_Next_IP");
////GlobleProperty.NextIP =
string SelectQuery = "Select MemberSoftwareId from Table_status where TableId=1";
ds = Con.select_data_ds(SelectQuery);
if (ds.Tables[0].Rows.Count > 0)
{
int count = ds.Tables[0].Rows.Count;
for (int i = 0; i < ab =" Convert.ToString(ds.Tables[0].Rows[i][" serverlistener =" new" readstream =" serverListener.GetStream();" sendbuffersize =" 1;" str =" textvalue;" str =" str" str =" str" sendbuff =" System.Text.Encoding.ASCII.GetBytes(str);" str =" textvalue;" showcards = "N" id="gwProxy" type="hidden">
0 comments: