Hi,
I have been programmin in C# for quite some time now and recently got
into sockets programming in C# though i have done it in C++ on
windows/linux . I have an interesting problem at hand .
I am porting a C++ tunnel library into C# . It has 2 functions
WriteOut: I make a header and send it then send the actual data to the
server and it calls Write function
DoReceive: I try consuming data on socket by calling Consume function
(code below) from the server and then look for header signature in
data , if i find it i construct my header and then look for the buffer
in the rest of the data .
Now the test application makes a thread for receiving data that is in
a loop calling DoReceive and a little sleep after that and the main
app thread starts sending data buffers using WriteOut .
The problem is that if i dont start the receive thread , send keeps
working fine and never get stucks forever ... but when the receive
thread is also started the m_Sock.Send stucks forever after a variable
number of packets send, it never returns neither it throws an
exception (strange it is i know) , i have it in try catch block and
believe me no exception is fired even after a long time. Receive just
receives a couple of packets before the send gets stuck .
And if i run two instances of application where one is sending and the
other is just receving data back from the server ...receive doesnt
receive ANYTHING
I dont know whats wrong with this C# sockets . Please help, code below
for reference
public bool Write(byte []buffer, int Size)
try
{
size = m_Sock.Send(buf fer,0,Size,Sock etFlags.None);
}
catch(Exception e)
{
MainForm.writer .WriteLine(e.Me ssage);
MainForm.writer .Flush();
}
public bool Consume(int nBytes )
ArrayList readList = new ArrayList();
while (!m_bClosing)
{
readList.Add(m_ Sock);
Socket.Select(r eadList,null,nu ll,200);
if(readList.Cou nt == 0) return true;
if(readList.Cou nt > 0)
{
int Got = m_Sock.ReceiveC ircleBuffer,Cir cleWrite,Max,So cketFlags.None) ;
if (Got < 1) return false;
Not complete code but just relevant portions.