PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
Why doesn't it throw an exception to connect to an unexisting IP?
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
Why doesn't it throw an exception to connect to an unexisting IP?
![]() |
Why doesn't it throw an exception to connect to an unexisting IP? |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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) |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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. "Cat" <typingcat@gmail.com> wrote in message news:6c56c727-8402-423a-8d80-5b1694c3d1d7@k10g2000prm.googlegroups.com... > 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) |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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. -- Simon Hart Visual Developer - Device Application Development MVP http://simonrhart.blogspot.com "Cat" wrote: > 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) > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
"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. On Apr 24, 12:38*am, "Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT com> wrote: > 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. > > "Cat" <typing...@gmail.com> wrote in message > > news:6c56c727-8402-423a-8d80-5b1694c3d1d7@k10g2000prm.googlegroups.com... > > > 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.InterNetworkThen > > * * * * * * * *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) |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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) On Apr 24, 12:44*am, Simon Hart [MVP] <srhart...@yahoo.com> wrote: > 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. > -- > Simon Hart > Visual Developer - Device Application Development MVPhttp://simonrhart.blogspot.com > > "Cat" wrote: > > 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) |
|
|
|
#6 |
|
Guest
Posts: n/a
|
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. -- Simon Hart Visual Developer - Device Application Development MVP http://simonrhart.blogspot.com "Cat" wrote: > 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) > > On Apr 24, 12:44 am, Simon Hart [MVP] <srhart...@yahoo.com> wrote: > > 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. > > -- > > Simon Hart > > Visual Developer - Device Application Development MVPhttp://simonrhart.blogspot.com > > > > "Cat" wrote: > > > 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) > > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

