I have some problems with TCP client on net Compact-Framework 3.5. My server application is written in C++ plus QT library and it's working on desktop computer. One of my clients applications is written in java on android platform and works fine. Also another desktop client written in C++ + QT works fine. I think that server application should be ok an there must be some problem with NET client.
Now I'm trying to write client on Windows Mobile (5.0 - 6.5) platform, using NET CF 3.5.
Problem is that TCP client disconnects with out any reason after one sequence:
client -> server - sends request
server -> client - sends replay
I' have checked communication using Wireshark and below are results:
client -> server [Push]
server -> client [Ack], [Push]
client -> server [Ack], [Fin]
so disconnection is initialized from client side.
I spouse that probably my socket is initialized in wrong way. I'm new in C# and NET stuff so maybe someone more experienced can help me.
Below is a parat of code which opens connection:
public void Open (ConnectionInfo info)
{
if (m_socket != null)
{
m_socket.Close();
}
try
{
m_info = info;
IPAddress ip = IPAddress.Parse((string)m_info.host);
TcpClient tcp = new TcpClient();
tcp.Connect(ip, (int)m_info.port);
m_socket = tcp.Client;
if(!m_socket.Connected)
throw new Ex.SocketEx();
readLength = 0;
}
catch (Exception e)
{
throw new Ex.SocketEx();
}
}
Writes data into socket:
public void Write ( Buffor data)
{
if (m_socket == null)
throw new Ex.SocketEx();
int startTickCount = Environment.TickCount;
int sent = 0;
int offset = 0;
int timeout = 1000;
int size = data.Size();
byte[] buffer = data.ToArray();
do
{
if (Environment.TickCount > startTickCount + timeout)
throw new Ex.SocketEx();
try
{
sent += m_socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
}
catch (SocketException e)
{
Debug.WriteLine(e.ToString());
Thread.Sleep(50);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
throw new Ex.SocketEx();
}
} while (sent < size);
}
and reads data from socket
public Buffor Read()
{
if (m_socket == null)
throw new Ex.SocketEx();
Buffor buffor = new Buffor();
try
{
if (readLength == 0)
{
if (m_socket.Available >= 2)
{
byte[] pick = new byte[2];
m_socket.Receive(pick, 2, SocketFlags.None);
readLength = (((int)pick[0]) << 8) + ((int)pick[1]) - 2;
}
}
if ((readLength != 0) & (m_socket.Available >= readLength))
{
byte[] buff = new byte[readLength];
m_socket.Receive(buff, readLength, SocketFlags.None);
buffor = new Buffor(buff);
readLength = 0;
}
}
catch (Exception e)
{
throw new Ex.SocketEx();
}
return buffor;
}
"Connection" is running in a separate thread than User Interface. Threads is checking periodically if new message is ready to read, if so reads the message, handles it and gets back to waiting for new one. From the other side if UI wants to send something, message is putted at the end of the send buffer. Connection thread in each iteration is checking if there is something in send buffer, if so sends it.
In short whay it looks lake this:
void run()
{
while (!m_quit)
{
if(m_socket.Avaliable >= 2)
doRead();
if(m_send_buff.Length != 0)
doSend();
Thread.Sleep(50);
}
}