Hi,
We have an asynchronous server socket program, which works fine BUT, when due to some circumstances, I receive null data or blank data, the server goes to infinite loop
Here is the code - namespace SocketServer
-
{
-
-
public class StateObject //class created to clear the duplicate records
-
{
-
// Client socket.
-
public Socket workSocket = null;
-
// Size of receive buffer.
-
public const int BufferSize = 5000;
-
// Receive buffer.
-
public byte[] buffer = new byte[BufferSize];
-
// Received data string.
-
public string sb;
-
-
}
-
-
class Program
-
{
-
-
-
public AsyncCallback pfnWorkerCallBack;
-
private Socket m_mainSocket;
-
-
public int port = 7030;
-
public byte[] m_byBuff = new byte[5000];
-
public int flag2 = 0;
-
-
public byte[] inValue = new byte[] { 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 };
-
public byte[] outvalue = new byte[10];
-
-
private string m_strConnection;
-
public Program()
-
{
-
this.m_strConnection = ConfigurationSettings.AppSettings["Connection"];
-
this.strDateFormat = null;
-
this.strLocation = null;
-
StartListen();
-
-
-
}
-
public void StartListen()
-
{
-
-
-
try
-
{
-
NameValueCollection collection1 = ConfigurationSettings.AppSettings;
-
m_mainSocket = new Socket(AddressFamily.InterNetwork,
-
SocketType.Stream,
-
ProtocolType.Tcp);
-
IPAddress ip;
-
-
string ipstr = "198.158.0.20";
-
-
ip = IPAddress.Parse(ipstr);
-
-
IPEndPoint ipLocal = new IPEndPoint(ip, 7030);
-
m_mainSocket.Bind(ipLocal);
-
m_mainSocket.Listen(100);
-
-
// Create the call back for any client connections...
-
-
-
while (true)
-
{
-
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
-
Thread.Sleep(1000);
-
}
-
-
}
-
catch (Exception e1)
-
{
-
Console.WriteLine(e1.Message.ToString());
-
WriteErrorLogMessage(e1.Message);
-
-
-
}
-
}
-
-
-
public void OnClientConnect(IAsyncResult asyn)
-
{
-
try
-
{
-
Socket msocket;
-
msocket = m_mainSocket.EndAccept(asyn);
-
msocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);
-
msocket.IOControl(IOControlCode.KeepAliveValues, inValue, outvalue);
-
WaitForData(msocket);
-
}
-
-
catch (Exception e1)
-
{
-
Console.WriteLine(e1.Message.ToString());
-
WriteErrorLogMessage(e1.Message);
-
}
-
-
}
-
-
-
public void WaitForData(System.Net.Sockets.Socket soc)
-
{
-
try
-
{
-
if (pfnWorkerCallBack == null)
-
{
-
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
-
}
-
-
StateObject state = new StateObject();
-
state.workSocket = soc;
-
soc.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, pfnWorkerCallBack, state);
-
-
}
-
catch (Exception e1)
-
{
-
Console.WriteLine(e1.Message.ToString());
-
WriteErrorLogMessage(e1.Message);
-
}
-
-
}
-
public void OnDataReceived(IAsyncResult asyn)
-
{
-
Console.WriteLine("data received");
-
WriteLogMessage("data received");
-
bool socketClosed = false;
-
try
-
{
-
StateObject stateobject = (StateObject)asyn.AsyncState;
-
Socket sock = stateobject.workSocket;//assgning the app socket to new socket
-
-
int nBytesRec = sock.EndReceive(asyn);
-
-
if (nBytesRec > 0)
-
{
-
string sReceived = System.Text.Encoding.ASCII.GetString(stateobject.buffer, 0, StateObject.BufferSize);
-
-
sReceived = sReceived.TrimEnd('\0');
-
WriteLogMessage(sReceived);
-
if (sReceived != null && sReceived != " " && sReceived != "")
-
{
-
Console.WriteLine(sReceived);
-
}
-
-
else
-
{
-
sock.Disconnect(false);
-
sock.Close();
-
stateobject = null;
-
Console.WriteLine("Inside infinite loop");
-
socketClosed = true;
-
}
-
if (!socketClosed)
-
{
-
byte[] Buffer = new byte[StateObject.BufferSize];
-
-
stateobject.buffer = Buffer;
-
-
sock.BeginReceive(stateobject.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(OnDataReceived), stateobject);
-
}
-
}
-
-
catch (SocketException e1)
-
{
-
Console.WriteLine(e1.Message.ToString());
-
WriteErrorLogMessage(e1.Message);
-
}
-
-
-
catch (Exception e1)
-
{
-
Console.WriteLine("Inside infinite loop");
-
WriteErrorLogMessage(e1.Message);
-
}
-
}
There is an underlined code, where we want to disconnect those socket connections which does not carry any value. When we did that what happens is, whenever the socket reconnects it does not connect, nor does any other live socket connects.
Please let me know where are we going wrong
Thanks in advance
Regards
cmrhema
0 1289 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Corne Oosthuizen |
last post by:
I'm writing a Telnet Server application using
Asynchronous sockets.
I spawn a listener thread to handel incomming connections
and create a separate client socket for each new
connection.
I...
|
by: Darren Thomas |
last post by:
Dear all,
I have written a TCP server as a windows service that accepts connections using the System.Net.Sockets.Socket class. I use the various asynchronous methods in the socket class. I'm...
|
by: Colin |
last post by:
I'm writing a little console socket server but I'm having some
difficulty. Can I ask your advice - where is the best place to get
some help on that topic? It would be nice if some people who knew...
|
by: taskswap |
last post by:
I have a legacy application written in C that I'm trying to convert to
C#. It processes a very large amount of data from many clients
(actually, upstream servers - this is a mux) simultaneously.
...
|
by: Macca |
last post by:
I am writing an application that uses asynchronous sockets to get data over
ethernet from embedded devices, up to 30 concurrent devices.(These devices
are written in C).
My application...
|
by: Macca |
last post by:
My app has an asynchronous socket server. It will have 20 clients connected
to the server. Each client sends data every 500 millisecondsThe Connections
once established will not be closed unless...
|
by: Macca |
last post by:
Hi,
I am writing an asychronous socket server to handle 20+ simulataneous
connections.
I have used the example in MSDN as a base. The code is shown at end of
question.
Each connection has a...
|
by: Engineerik |
last post by:
I am trying to create a socket server which will listen for connections from
multiple clients and call subroutines in a Fortran DLL and pass the results
back to the client. The asynchronous socket...
|
by: Pat B |
last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol
using C#. I have implemented it using BeginReceive and EndReceive
calls so as not to block when waiting for data from the...
|
by: keksy |
last post by:
Hi every1,
I am writing a small client/server application and in it I want to
send an image asynchronous from the client to the server through a TCP
socket. I found an example code on the MSDN...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |