المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Not able to receive data using tcp(urgent!)



C# Programming
04-29-2009, 10:17 AM
Hello Friends,

I need your help regarding the socket programming . Let say i am in the server system . Client has send a xml file using TCP to a specific port . My problem when i read data using NetworkStream then then it has been stopped there not able to find it out where is the problem , even it does not through any error .

But when i send and receive data to the same system ( mean to say the server and client is same ) then its working fine . I am able to receive data . But when server and client is different then the problem occurs . I am puuting the sample code . Please look into the code and help me if you people have any idea .


TcpClient c = new TcpClient("Test", 1027);
NetworkStream ns = c.GetStream(); // get stream
byte[] buf = new byte[8024]; // create byte array to receive data

ns.Read(buf, 0, 8020); // read data from stream into byte array
// read data from stream into byte array
string st = System.Text.Encoding.ASCII.GetString(buf); // convert byte array to string

// create a writer and open the file
TextWriter tw = new StreamWriter("D:\\test.txt");

// write a line of text to the file
tw.WriteLine(st.ToString());

// close the stream
tw.Close();
c.Close();

Console.WriteLine(st);