Why doesn't it throw an exception to connect to an unexisting IP?

C

Cat

This has been really troubling me, at least for few days. I tried to
solve it myself but I finally gave up and am asking for some advice.

What I'm trying to do is rather simple. To communicate between PDA and
PC via good old TCP socket. Since I dont' have a PocketPC, I used the
emulator that came with VS.NET

First, I tried like
Dim client As TcpClient = Nothing
Try
client = New TcpClient()
client.Connect("my ip address", 7778)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MessageBox.Show(client.Client.Connected)

The problem was, even though I'd typed a wrong ip, the message box
always showed "True". The connection error only occurred when the
internet connection itself was unavailable.

Second,
Dim mySoc As New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
mySoc.BeginConnect(New IPEndPoint(IPAddress.Parse("my ip
address"), 7778),
AddressOf OnConnect, mySoc)
End Sub
Sub OnConnect(ByVal result As IAsyncResult)
Dim mySoc As Socket = CType(result.AsyncState, Socket)
mySoc.EndConnect(result)
System.Diagnostics.Debug.WriteLine(mySoc.Connected)

System.Diagnostics.Debug.WriteLine(mySoc.RemoteEndPoint.ToString())
End Sub

This was the same, it always printed "True" even thouth the IP address
was wrong. I've tested this on VS.NET 2008 (WM 5.0 Emulator + Mobile
Device Center) and VS.NET 2005 (PPC 2003 emulator + ActiveSync), but
the results were the same.

All I want is simple text-based message exchanging. But I'm stuck at
the very basic stage like checking if the message was sent and
received. Is there any sample code that I can refer to? Please give me
some advice. Thank you a lot.

PS ------- The server side code was like
Dim myIP As IPAddress = Nothing
For Each i As IPAddress In
Dns.GetHostAddresses(Dns.GetHostName())
If i.AddressFamily = AddressFamily.InterNetwork Then
myIP = i
Exit For
End If
Next
If myIP Is Nothing Then
MessageBox.Show("Can't get the IP4 address of this
computer.")
End If
MyListener = New TcpListener(myIP, 7778)
MyListener.Start()
MyListener.BeginAcceptTcpClient(AddressOf OnClient, MyListener)
 
P

Paul G. Tobey [eMVP]

There are several things wrong that I see. When you pass a string to
Connect(), Connect will use that as a host name not an IP address. Remember,
this is software, not an intelligent person who would recognize an IP
address from a host name. Use IPAddress.Parse() to build an IPAddress
object for use with Connect. As for the lack of an exception, I'm not sure
what's going on there. It's possible that TcpClient doesn't throw the
exception and simply tries to return status via properties of the object or
something. You really need to try this on a real device to see what's going
on. You should certainly verify the network configuration of your emulator.
There are three configurations: none, outgoing only, and virtual switch.
You want the last, although outgoing should probably work.

Paul T.
 
S

Simon Hart [MVP]

I wouldn't reply on the Connected property, you need some handshaking for a
true robust solution. So you're server side would in this case pass back a
message to signal success or failure. I never use async operations, I always
create a worker thread to do this for me, this way it gives you complete
control over threading pattern.

Also avoid TcpClient. Try changing the protocol to IP. The comms would fail
when you try to send data if you are not really connected anyway. TCP is good
for this. Also if your connection gets terminated mid-flight.
 
C

Cat

"my ip address" is not the actual code, it was just a place holder
because I didn't want to include my real IP address when I was
posting. In the real code I used my IP address like "123.123.123.123".
I'm sorry for causing misunderstanding.
 
C

Cat

Thanks. I wonder if there is a sample chat application between WM and
PC. Just a simple text-exchanging sample. I downloaded a sample
signature application from Microsoft, but it was written for CF 1.0,
and didn't work correctly (outputting lots of first change of ..
exception messages)
 
S

Simon Hart [MVP]

Well you know how to send data right, the server side would need to look
something like this:

Socket client = tcpListener.Accept();

//Now we have someone connected, lets get the data calling Receive.

//Here you would normally hand this off to a separate thread because you are
//blocking incomming connections.
client.Send(Encoding.ASCII.GetBytes("handshaking");

Of course the client would need to call Receive on the same socket to get
the "handshaking" message.

I need to write a blog about this stuff, as it's not that clear cut how you
do things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top