We have web application , it works as Socket Listner. I wanted to check before the AcceptSocket , whether there is any Client Socket available to connect to this listner. I want to display a message if there is no Client Socket to connect and cancel send/receive of data.
I am using TCPListner using ASP.net C# for the web application. The Client socket is windows VB6 application using Winsock control.
Socketing programme, ASP.Net C#, VS2008
Thanks you for the reply ,
see below my sample code for communicate with server
clsCommunication.cs, this class file imports into .aspx page. I tried using Console.WriteLine(), but cann’t shown this message.
I wanted to display alert box , so that user understands there is no connection OR display on status bar or highlight a frame/box etc.
public void Communicationcation()
{
byte[] bytes = new byte[1024];
string strSiteID = "SiteID";
socket.Start();
if (!socket.Pending())
{
Console.WriteLine("Sorry, no connection requests have arrived");
}
else
{
client = socket.AcceptSocket();
if (client.Connected == true)
{
decimal dSiteID = decimal.Parse(GetSiteSetting(ref strSiteID).ToString());
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
WriteLog(string.Format("Connected with {0} at port {1}", clientep.Address, clientep.Port));
string strWelcome = STARTMESSAGE + "WHOAMI" + " " + dSiteID + " " + dSiteID + FINISHMESSAGE;
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(strWelcome);
int i = client.Send(data, data.Length, SocketFlags.None);
WriteLog(String.Format("Message sent {0} bytes!", i));
//Get reply from the Connected cleint.
i = client.Receive(bytes, bytes.Length, SocketFlags.None);
if (i != -1)
{
WriteLog(string.Format(System.Text.Encoding.UTF8.GetString(bytes)));
}
}
}
}
You can use
TcpListener‘sPendingmethod, see http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.pending.aspx