Socket Recieve problem
-
Hi guys i try to write a simpale server client appliaction. server transmit the file content to client and recieve an acknowledgment which is a "ok" string. i have some problem in my code.
using (NetworkStrem ns = new NetworkStream(client))
{
using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
{
while (sent < file.Length) // sent = 0 initially
{
data = new byte[20];br.Read(data, 0, data.Length); //read 20 byte from file tempData = data; ns.Write(data, 0, data.Length); // sent to client ns.Flush(); ns.Read(data, 0, data.Length); //recieve ack, but its read the previews content sent to the client ack = Encoding.ASCII.GetString(data); while (ack != "ok") { ns.Write(tempData, 0, tempData.Length); ns.Flush(); ns.Read(data, 0, data.Length); ack = Encoding.ASCII.GetString(data); } sent += 20; ack = string.Empty; }
}
what'wrong here i dont know if u know tell me Thanks in advance
-
Hi guys i try to write a simpale server client appliaction. server transmit the file content to client and recieve an acknowledgment which is a "ok" string. i have some problem in my code.
using (NetworkStrem ns = new NetworkStream(client))
{
using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
{
while (sent < file.Length) // sent = 0 initially
{
data = new byte[20];br.Read(data, 0, data.Length); //read 20 byte from file tempData = data; ns.Write(data, 0, data.Length); // sent to client ns.Flush(); ns.Read(data, 0, data.Length); //recieve ack, but its read the previews content sent to the client ack = Encoding.ASCII.GetString(data); while (ack != "ok") { ns.Write(tempData, 0, tempData.Length); ns.Flush(); ns.Read(data, 0, data.Length); ack = Encoding.ASCII.GetString(data); } sent += 20; ack = string.Empty; }
}
what'wrong here i dont know if u know tell me Thanks in advance
What are the problems? Are you getting an error - if so edit your question and post the error.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
-
What are the problems? Are you getting an error - if so edit your question and post the error.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
using (NetworkStrem ns = new NetworkStream(client))
{
using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
{
while (sent < file.Length) // sent = 0 initially
{
data = new byte[20];br.Read(data, 0, data.Length); //read 20 byte from file tempData = data; ns.Write(data, 0, data.Length); // sent to client //\*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\* ns.Flush(); ns.Read(data, 0, data.Length); //Error occures here, this read method read the data send by me in \*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\* and also the ack string ok, thats why the while loop became infinite ack = Encoding.ASCII.GetString(data); while (ack != "ok") { ns.Write(tempData, 0, tempData.Length); ns.Flush(); ns.Read(data, 0, data.Length); ack = Encoding.ASCII.GetString(data); } sent += 20; ack = string.Empty; }
}
-
using (NetworkStrem ns = new NetworkStream(client))
{
using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
{
while (sent < file.Length) // sent = 0 initially
{
data = new byte[20];br.Read(data, 0, data.Length); //read 20 byte from file tempData = data; ns.Write(data, 0, data.Length); // sent to client //\*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\* ns.Flush(); ns.Read(data, 0, data.Length); //Error occures here, this read method read the data send by me in \*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\* and also the ack string ok, thats why the while loop became infinite ack = Encoding.ASCII.GetString(data); while (ack != "ok") { ns.Write(tempData, 0, tempData.Length); ns.Flush(); ns.Read(data, 0, data.Length); ack = Encoding.ASCII.GetString(data); } sent += 20; ack = string.Empty; }
}
ganesh_IT wrote:
br.Read(data, 0, data.Length);
I don't understand your message, however I can tell you a Read method returns an integer, i.e. the number of bytes read. Not using that information is a mistake, as there may be less than data.Length, in which case you are processing too much data later on (as in your write). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi guys i try to write a simpale server client appliaction. server transmit the file content to client and recieve an acknowledgment which is a "ok" string. i have some problem in my code.
using (NetworkStrem ns = new NetworkStream(client))
{
using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
{
while (sent < file.Length) // sent = 0 initially
{
data = new byte[20];br.Read(data, 0, data.Length); //read 20 byte from file tempData = data; ns.Write(data, 0, data.Length); // sent to client ns.Flush(); ns.Read(data, 0, data.Length); //recieve ack, but its read the previews content sent to the client ack = Encoding.ASCII.GetString(data); while (ack != "ok") { ns.Write(tempData, 0, tempData.Length); ns.Flush(); ns.Read(data, 0, data.Length); ack = Encoding.ASCII.GetString(data); } sent += 20; ack = string.Empty; }
}
what'wrong here i dont know if u know tell me Thanks in advance
If you are using TCP/IP you do not need to send acknowledgments. TCP/IP is a guaranteed, in-order protocol and .NET will throw an exception if it fails. If you want to keep the connection active set the keep alive flag. Oh, and, if you are using UDP sending an ack is self defeating.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost