This Problem is Killing Me :@  
Author Message
RizwanSharp





PostPosted: .NET Framework Networking and Communication, This Problem is Killing Me :@ Top

Hello,

I have a very strabge problem and dont know what to do. I wrote very simple code to varify the behavior of problem in some big project.

I run this Server Code in a Console Application on my PC:

class Program

{

private static TcpListener listener;

private static int port;

static void Main(string[] args)

{

port = 7000;

listener = new TcpListener(IPAddress.Any, port);

listener.Start();

Console.WriteLine("Server Started");

TcpClient client = listener.AcceptTcpClient();

Console.WriteLine("Client Connected");

NetworkStream ns = client.GetStream();

byte[] data = Encoding.ASCII.GetBytes("Hello");

ns.Write(data, 0, data.Length);

ns.Flush();

Console.WriteLine("Data Sent");

ns.Close();

client.Close();

listener.Stop();

Console.ReadLine();

}

}

And I Run this Client Code from My Pocket PC in Console Application:

class Program

{

private static int port;

private static TcpClient client;

static void Main(string[] args)

{

port = 7000;

client = new TcpClient();

Console.WriteLine("Connecting");

client.Connect(IPAddress.Parse("210.2.164.223"), port);

Console.WriteLine("Connected");

NetworkStream ns = client.GetStream();

byte[] data = new byte[50];

Console.WriteLine("Reading Message");

int readBytes = ns.Read(data, 0, data.Length); // Reutrns 0 

string message = Encoding.ASCII.GetString(data, 0, readBytes);

Console.WriteLine(message);

ns.Close();

client.Close();

Console.ReadLine();

}

}

Server Output:

Server Started

Client Connected

Data Sent

Client Output:

Connecting

Connected

Reading Message

// Here Blank Massage is Received

Note:

If I use the same client code on PC in Console Application, All works fine and Client Receives data!

If I use the same client code on Pocket PC through GPRS Connection then also it works fine!

But when I use the same client code on Pocket PC which is connected to My PC through Cradle I dound the above mentioned output with 0 length data received.

Conclusion:

All worked fine a few days back before I Installed my Windows. It seems like some problem with IP Addresses!

My PC Gets 2 IP Addresses one from DSL Internet Modem and One from the Cradle of Pocket PC when Device is on the Cradle; Before Windows Installation When I used to Query for IP Addresses of machine It gave me DSL Modems IP Address at index 0 and Cradle's connection IP Address at 1 index but after Windows installation I get the inverse, First Cradle's Connection IP and second DSl  Modem's IP though Dns.GetHostAddresses(Environment.MachineName).AddressList;

I wasted 8 hours to find all this and then it came to my mind to write a small application to check from where problem is arisen but got the conclusion that my code is fine, There is some other conflict which is sucking my mind badly.

Its not feasible for me to waste much time to make an update to application then connect th GPRS then disconnect it to update it and then connect again to test and so on.

In past I just used Cradles Connection, All I needed to do was to put my device on cradle in morning and do all the Update, Test and so on, And then take it off from the Cradle. But now It S****!

Can Anybody tell me how to fix this problem

I'll be really thankful to you to solve this issue!

Best Regards,

 

 




.NET Development27  
 
 
IsshouFuuraibou





PostPosted: .NET Framework Networking and Communication, This Problem is Killing Me :@ Top

Sorry that I don't have much experience in using Pocket PC.


One thing that I notice is a lack-luster handshaking over a single port used for both transmitting and receiving. This becomes problematic when the vastly asynchronous procedures, which is what happens when running client/server software. If you are running on the PC with only one core then you are forcing some synchronization by that nature of context switching when shifting to an IO operation. I would suggest some more syncronization, more than what read gives you by blocking for.


Alright, after having worked on this for quite some time, I noticed something inside NetworkStream.Read method documentation.


"If the remote host shuts down the connection, and all available data has been received, the Read
method completes immediately and return zero bytes."

byte[] data = Encoding.ASCII.GetBytes("Hello");

ns.Write(data, 0, data.Length);

ns.Flush();

Console.WriteLine("Data Sent");

ns.Close();

client.Close();

listener.Stop();

Let me point out that, you don't wait at all after sending your message. If you reach the ns.Close() before the PocketPC has a chance to complete reading the reading method will return 0.

Add in another handshake after sent from the client to the server (PocketPC to the PC) that says "Data Received" wait and watch for that before closing the stream and client. Of if the TcpClient supports it, watch for a client disconnected flag/event before closing your stream.

Ideally what you might want to do is start communication with one port, have the server initiate another connection down the the client with the client expecting it and manage communication with one port being "in" and the other "out" and make sure to send control messages between client/server so that you don't get ahead of yourself when running the programs.

Control codes are very important to client/server communication. You really should never start communicating real data until you've gone through handshaking to ensure that both the client and server are in a known ready state. Then a handshaking at the end to ensure that client and server are in a known state where you can close the network stream.

Hope this helps you out with this issue that you are dealing with.



 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, This Problem is Killing Me :@ Top

Hi IsshouFuuraibou,

Thanks for the reply, As I mentioned that this is really a test code written by me when I was having the same problem with the original code. The original code has asynchrnous call throught out and I'm not disconnected it untill i get 0 length data from the NetworkStream so the problem is not there. As i mentioned that all things were running fine just before I installed windows, even now all things are working fine If i connect from GPRS internet from my Device to connect to my Desktop PC.

Your point is valid that this may be closing the conneciton write after sending the data to the client which has not be received at Client so it get 0 length data but putting a delay even doesnot solve my problem. There is some other issue which is not coming in front. And now I've continued development using GPRS connection from my Pocket PC not from its cradle and all things are going fine. But the real problem remains un resolved.

Anyway, thanks a lot for your concern and help!

Best Regards,

Rizwan



 
 
IsshouFuuraibou





PostPosted: .NET Framework Networking and Communication, This Problem is Killing Me :@ Top

Rizwan,

Even though it is test code it pays to be more careful that you can't accidentally cause the same end result (receiving 0 length data) by a second means when trying to address it as a bug. Could end up going in circles, fixing the original bug but introducing a new one and not realize it. I've done that before.

The only other thing I saw when searching through the documentation is to bind your TCP listener to only the one localaddr that you are expecting to get the connection on. I don't know if you've tried this yet or not. It would be worth a shot.

Other than that last thing there isn't anything else I can suggest without actually being able to set up hardware to test it on. I just don't have a PocketPC to do any real testing for this. Hope you get more help than just me, it is weird that you had a working program then it stopped when something got updated. It is possible that the CRL code makes certain assumptions that were true before the update then not true after the update.

Best Regards,

Isshou


 
 
Unzip





PostPosted: .NET Framework Networking and Communication, This Problem is Killing Me :@ Top

Rizwan,

I finally found a solution to my problem (http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=889661&SiteID=1&mode=1), I hope this will work for you too... I you have questions you can mail me at rubenthys_at_gmail.com

Greetings,

Ruben


 
 
RizwanSharp





PostPosted: .NET Framework Networking and Communication, This Problem is Killing Me :@ Top

Hello IsshouFuuraibou,

Thanks a lot for your reply! Really appreicate it!

Rizwan,

Even though it is test code it pays to be more careful that you can't accidentally cause the same end result (receiving 0 length data) by a second means when trying to address it as a bug. Could end up going in circles, fixing the original bug but introducing a new one and not realize it. I've done that before.

As i mentioned in my first post that all was working fine before I installed windows, I got the same code, Same device but only with new installation of windows, I dont know what's wrong with OS settings. I'm not balming either .Net nor my code, the problem is on the other end, with IP Addresses or etc... I dont know how to explain it really


The only other thing I saw when searching through the documentation is to bind your TCP listener to only the one localaddr that you are expecting to get the connection on. I don't know if you've tried this yet or not. It would be worth a shot.

I had done that thing before I posted my quesiton of forums, I have tried specifiying only one IP Address that was in use by my Machine.


Other than that last thing there isn't anything else I can suggest without actually being able to set up hardware to test it on. I just don't have a PocketPC to do any real testing for this. Hope you get more help than just me, it is weird that you had a working program then it stopped when something got updated. It is possible that the CRL code makes certain assumptions that were true before the update then not true after the update.

Best Regards,

Isshou

You are right, I think it may run on some other computer at this time but not sure...... Lets continue then. I try to get it working on emulator, lets see what happpens.

Best Regards,