|
-
Mar 26th, 2012, 01:24 PM
#1
Thread Starter
Junior Member
Tcplistener project
Sorry if I am not following the forums rules by any of this, my first post so..
I have been working on a little tcp client/server project and I am stuck with a problem after searching for hours and I have read a lot of threads here on vbforums without finding what Im looking for..
My client and server communicate just fine, the problem I have is this:
The server program user interface allowes one to enter the port number you want the server to listen on. The server program attempts to opens any port that I set, and always goes on to the next step, i.e. gives feedback that it is now listening. This also happens when I attempt to listen on ports that I know for a fact are in use by other application. The code does not give any error when port is already in use by other applications, it goes right on to say "listening for connections".. For instance, I have an irc-server running on 6666, so when I set port 6666 in the listen program, It doesnt give an error, it just pretends everything is fine and says "listening".. If I try to connect, the connection of course goes through to the irc-server because it is using that specific port.
I will try to paste my code here:
Code:
Try
servidor = New TcpListener(ip, thePort)
servidor.Start() ''initializing port listener
Catch ex As SocketException
conversacion.Text &= "[" & TimeOfDay & "] " & "ERROR binding port :" & thePort & vbCrLf
Exit Sub
End Try
I am using try->catch and It does not give any message. So "conversacion" is a text field, where I want the error binding port-message to come, any transfers between the client and server is showing there so I know there is nothing wrong with that. The error DOES show If I am running two instances of the same server program, and one is already using the designated port. I want it to be able to give this error message if another program is using the chosen port.
Any help?
-
Mar 26th, 2012, 04:13 PM
#2
Thread Starter
Junior Member
Re: Tcplistener project
I think I found the solution... I had ip address set to 127.0.0.1 but should use 0.0.0.0, right?
-
Mar 27th, 2012, 03:45 PM
#3
Thread Starter
Junior Member
Re: Tcplistener project
seems to work, but now I discovered another problem...
I can't know when client disconnects, i.e. by normal disconnect 
(I am not talking about unpredicted disconnect, network failure etc that requires ping-pong or other keep-alive messages etc to be certain)
To read the stream I use a timer with interval 1000 and when it ticks,
it goes like this: if read_write.DataAvailable = True Then --> it will read the stream and
if it is not 0 --> it shows text on screen text field
Else (if it is 0) --> say client disconnected
When I was debugging this, I tried removing the "if read_write.DataAvailable" so that every tick would make it read the stream, and I discovered that now it DOES show the "client disconnected" message when client disconnects. However, this makes the server program hang, it simply crashes until I click disconnect in the client program. Then it shows message "client disconnected".
Maybe I am wrong, but this must mean that the "if read_write.DataAvailable" is false when client disconnects, so I should not use this to determine whether or not to read the stream.
Then what could be a better alternative?
Do I need to check "DataAvailable" or some other check before reading the stream?
Without it, the disconnect always is recognized, but the program freezes, without it the
program never freezes, but does not show when client disconnects.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|