473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TcpClient read/write timeouts do not timeout

I have a TcpClient. I set the read/write timeouts at 1 minute (in
milliseconds). I get a NetworkStream from it and confirm the timeouts
still exist. I do a NetworkStream.W rite() and then a
NetworkStream.R ead(). Sometimes it sits and waits -- on the Write()
or the Read() -- for 15 minutes before I get fed up and close the
app.....

I am not connecting to a TcpListener. I am connecting to a Socket
with ProtocolType.Tc p, which is acceptable. These read/writes
normally work, but when I get 3 or 4 TcpClients all connecting at the
same time, connection problems start. I don't understand why some of
the connections fail. And, when they do fail, why don't the timeouts
work?

I believe I have a bug in the server, but still, the timeouts should
timeout.
Jul 21 '08 #1
10 6176
On Mon, 21 Jul 2008 08:11:48 -0700, Zytan <zy**********@g mail.comwrote:
[...]
I believe I have a bug in the server, but still, the timeouts should
timeout.
Hard to say without seeing an actual concise-but-complete code example.
That said, your description isn't all that clear either. Are you having
read/write problems or connection problems? One paragraph says the
former, while another says the latter.

Beware of using the TCP timeout: once the timeout occurs, the socket is no
longer usable. Depending on your intent, you may be better off
implementing timeout logic yourself elsewhere.

Pete
Jul 21 '08 #2
(It appears my reply isn't appearing, so I'll write again:)
Hard to say without seeing an actual concise-but-complete code example.
I use the code from this example:
http://msdn.microsoft.com/en-us/libr...tcpclient.aspx
(except I implement timeouts.)

Are you having
read/write problems or connection problems?
It connects fine. The server sees the connection. The client gets
stuck in the stream.Write() call, and the server never sees any data
from it. (This occurs 1% o the time. The other 99%, everything works
fine.)

Beware of using the TCP timeout: once the timeout occurs, the socket is no
longer usable.
Ok.

Depending on your intent, you may be better off
implementing timeout logic yourself elsewhere.
The implementation is: 1. connect, 2. send data, 3. get reply, 4.
disconnect, so TcpClient seemed like the answer, since it was
synchronous, just as I wanted it to be, to avoid the hassle of
asynchronous code.

I even TURNED THE SERVER OFF once the stream.Write() got hung up, to
see if that would end the stream.Write() call... and it doesn't.
Write() should end because there's NO server and because the timeout
is 1 minute, and it's been 20 minutes now. Even if my server code is
a mess of bugs, this makes no sense.
Thanks, Pete,

Zytan
Jul 21 '08 #3
I guess I've conclusively shown that either .NET is fried, or
something else is causing this issue (i.e. me). And I think I may
have found it.... so, hold up... before you waste your time reading my
replies........

Zytan
Jul 21 '08 #4
I guess I've conclusively shown that either .NET is fried, or
something else is causing this issue (i.e. me). And I think I may
have found it.... so, hold up... before you waste your time reading my
replies........
I am SO STUPID...

My log files that supposedly were pinning the issue down to the
stream.Write() line, which was supposedly hanging up, actually pinned
it down to two functions... the Write() call AND an encryption
function made my yours truly, which was entering an INFINITE LOOP...
yeah, really.

So my first mistake was not testing my encryption function.
My second mistake was that the log files didn't pin down the EXACT
line of code of the error. How I did this, I don't know.

Sorry for wasting your time.

Zytan
Jul 21 '08 #5
On Mon, 21 Jul 2008 10:31:42 -0700, Zytan <zy**********@g mail.comwrote:
[...]
My log files that supposedly were pinning the issue down to the
stream.Write() line, which was supposedly hanging up, actually pinned
it down to two functions... the Write() call AND an encryption
function made my yours truly, which was entering an INFINITE LOOP...
yeah, really.
I'm glad you were able to find the issue. Not to belabor the point,
but...had you posted a concise-but-complete code sample that demonstrated
the problem, the above may well have been noted by someone else (possibly
even me :) ), leading to a quicker solution.

In other words, this is exactly why it's so important to post actual,
real-world code. Without the code, no one can say what's wrong, nor even
confirm that _your_ code is correct.

Pete
Jul 21 '08 #6
I'm glad you were able to find the issue. Not to belabor the point,
but...had you posted a concise-but-complete code sample that demonstrated
the problem, the above may well have been noted by someone else (possibly
even me :) ), leading to a quicker solution.
No, belabor away, you're completely right.

I posted in the hopes of wondering if someone could uncover any
gotchas about TcpClient. I was just being stupid. As soon as it
didn't return after the timeout, I should have known that something
else was causing the delay.

And yes, if I posted even a small snippet of code, you would have
asked me WTF is that encryption function doing, and I would have said
"aha"...

Zytan
Jul 21 '08 #7
......an encryption
function made my yours truly, which was entering an INFINITE LOOP...
The bug was operator precedence, I had something like:
x << 1 + 1
assuming it would do:
(x << 1) + 1
but in fact, it does:
x << (1 + 1)

VC++ warns about this, because I do this ALL the time, since I assume
<< and >are multiplication and division in my head, which comes
first. C# does not give any warning.

Zytan
Jul 21 '08 #8
On Mon, 21 Jul 2008 11:19:17 -0700, Zytan <zy**********@g mail.comwrote:
>......an encryption
function made my yours truly, which was entering an INFINITE LOOP...

The bug was operator precedence, I had something like:
x << 1 + 1
assuming it would do:
(x << 1) + 1
but in fact, it does:
x << (1 + 1)

VC++ warns about this, because I do this ALL the time, since I assume
<< and >are multiplication and division in my head, which comes
first. C# does not give any warning.
For what it's worth, if "in your head" you are using the shift operators
as division and multiplication, you should use division and multiplication
operators instead.

Being careful about operator precedence is obviously important in any
case, but in this particular situation, using actual multiplication or
division operators may well have avoided the bug, as well as made the code
more clear as to the intent.

Either way, it's the mixing of math and bit-twiddling that was your
problem. The "+ 1" (as opposed to "| 1") suggests this is more about math
than bit-twiddling, which would mean it's a multiplication disguised as a
shift operation. But even if you really should have been using << with |
(i.e. it's bit-twiddling) you wouldn't have had the bug had you used the
correct operators together, since the << operator _does_ have higher
precedence than the | operator.

Of course, never any harm in providing explicit precedence via
parentheses. But I still think it's better to use math operators for math
and bit-twiddling operators for bit-twiddling. :)

Pete
Jul 21 '08 #9
For what it's worth, if "in your head" you are using the shift operators
as division and multiplication, you should use division and multiplication
operators instead.
Right.
Being careful about operator precedence is obviously important in any
case, but in this particular situation, using actual multiplication or
division operators may well have avoided the bug, as well as made the code
more clear as to the intent.
Yup.
Either way, it's the mixing of math and bit-twiddling that was your
problem. The "+ 1" (as opposed to "| 1") suggests this is more about math
than bit-twiddling, which would mean it's a multiplication disguised as a
shift operation. But even if you really should have been using << with |
(i.e. it's bit-twiddling) you wouldn't have had the bug had you used the
correct operators together, since the << operator _does_ have higher
precedence than the | operator.
Ah.

I was doing bit-twiddling, and not math. So, I should be using <<
with |, just like you say.

I know I'm not the only one who got hit with mixing << and +, though,
since VC++ has a warning about it. But, this doesn't negate all the
great points you've made.
Of course, never any harm in providing explicit precedence via
parentheses.
Right, I find myself doing this a lot. Why didn't I this time?
Probably tired and pressured for time.
But I still think it's better to use math operators for math
and bit-twiddling operators for bit-twiddling. :)
Of course. I had never before thought about +1 and |1 as being any
different when the 1st bit is known to be cleared... but it makes
perfect sense to think about it in the way you mention....

Great tips! I'm glad I reported the bug now.

Zytan
Jul 21 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3049
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok and arrives at the server but from after that i only a couple of KB from the start of the file which cases the picture to display only the very top (of the screen). All the pictures are saved at the client side before sent to the server and they...
18
4893
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
2
6351
by: Eric Cathell | last post by:
I have an application that I am using to send documents to printers over a network. If the printer is powered off, or in configuration mode, or the client bridge is down, the tcpclient just hangs....I need a way to make it time out and continue to the loop....any know a way i can do that? say if it doesnt connect in 10 seconds to timeout and continue the loop? Eric
2
8726
by: Lisa Pearlson | last post by:
Hi, I am wanting to write an internet 'server' program. For ease, I want to use PHP-CLI with XINET super server. XINET communicates with the 'server' program via STDIN/STDOUT. I'm not sure how to communicate with the client this way though.. I can open stdin/out as a file, and use read and write operations, as if it were a regular file.. however, how can I set read timeouts on file read operations?
5
4801
by: Nobody | last post by:
Hi all, I try to write a small client that can handle some TCP communication message. I was wondering how I could use the TcpClient class to manage it. At the first time, I don't want to play with the async methods. My main issue is related about receiving informations back from the server. How can I be sure that the server has finished sending the response? Should I set a
4
11785
by: rowan | last post by:
I'm writing a driver in Python for an old fashioned piece of serial equipment. Currently I'm using the USPP serial module. From what I can see all the serial modules seem to set the timeout when you open a serial port. This is not what I want to do. I need to change the timeout each time I do a "read" on the serial port, depending on which part of the protocol I've got to. Sometimes a return character is expected within half a second,...
0
1434
by: zhangke007 | last post by:
Hello, everyone, Currently, I have an simple serial communication application using the serialnet.dll tool from Franson company. What this application does is to read the data through the com port in my windows ce 5 based wearable computer. The data is sent from a PIC microcontroller through the RS232 protocol. The scenario is like whenever you press a button, my PIC will detect the trigger and then send a single data through RS232...
4
20464
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am wondering if I am using TCPClient class in C#, how to setup timeout value? Timeout I mean, when connects to server for the 1st time, and during <timeoutinterval, if no server response is received, the GetStream method will return will return other than wait forever. thanks in advance,
2
4532
by: Zytan | last post by:
I just had the problem occur again, with NetworkStream.Write() doing its thing with a timeout... and it just sits and waits and waits and waits... it never times outs. So, I shut the server down just to see if THAT will make it at least end the function call and continue (since it's a synchronous call, so the program is delayed until it returns), and even THAT doesn't make the call end. It just sits and waits... Even IF I have a bug...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.