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)

Re: Why doesn't it throw an exception to connect to an unexisting IP? by Paul

Paul
Wed Apr 23 10:38:39 CDT 2008

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)



RE: Why doesn't it throw an exception to connect to an unexisting IP? by srhartone

srhartone
Wed Apr 23 10:44:02 CDT 2008

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)
>

Re: Why doesn't it throw an exception to connect to an unexisting IP? by Cat

Cat
Wed Apr 23 10:59:27 CDT 2008

"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=A0am, "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. =A0When you pass a string to
> Connect(), Connect will use that as a host name not an IP address. Remembe=
r,
> this is software, not an intelligent person who would recognize an IP
> address from a host name. =A0Use IPAddress.Parse() to build an IPAddress
> object for use with Connect. =A0As for the lack of an exception, I'm not s=
ure
> what's going on there. =A0It's possible that TcpClient doesn't throw the
> exception and simply tries to return status via properties of the object o=
r
> something. =A0You really need to try this on a real device to see what's g=
oing
> on. =A0You should certainly verify the network configuration of your emula=
tor.
> 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
> > =A0 =A0 =A0 =A0Dim client As TcpClient =3D Nothing
> > =A0 =A0 =A0 =A0Try
> > =A0 =A0 =A0 =A0client =3D New TcpClient()
> > =A0 =A0 =A0 =A0client.Connect("my ip address", 7778)
> > =A0 =A0 =A0 =A0Catch ex As Exception
> > =A0 =A0 =A0 =A0MessageBox.Show(ex.Message)
> > =A0 =A0 =A0 =A0End Try
> > =A0 =A0 =A0 =A0MessageBox.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,
> > =A0 =A0 =A0 =A0Dim mySoc As New Socket(AddressFamily.InterNetwork,
> > SocketType.Stream, ProtocolType.Tcp)
> > =A0 =A0 =A0 =A0mySoc.BeginConnect(New IPEndPoint(IPAddress.Parse("my ip
> > address"), 7778),
> > =A0 =A0 =A0 =A0 =A0 =A0 AddressOf OnConnect, mySoc)
> > =A0 =A0End Sub
> > =A0 =A0Sub OnConnect(ByVal result As IAsyncResult)
> > =A0 =A0 =A0 =A0Dim mySoc As Socket =3D CType(result.AsyncState, Socket)
> > =A0 =A0 =A0 =A0mySoc.EndConnect(result)
> > =A0 =A0 =A0 =A0System.Diagnostics.Debug.WriteLine(mySoc.Connected)
>
> > System.Diagnostics.Debug.WriteLine(mySoc.RemoteEndPoint.ToString())
> > =A0 =A0End 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
> > =A0 =A0 =A0 =A0Dim myIP As IPAddress =3D Nothing
> > =A0 =A0 =A0 =A0For Each i As IPAddress In
> > Dns.GetHostAddresses(Dns.GetHostName())
> > =A0 =A0 =A0 =A0 =A0 =A0If i.AddressFamily =3D AddressFamily.InterNetwork=
Then
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0myIP =3D i
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Exit For
> > =A0 =A0 =A0 =A0 =A0 =A0End If
> > =A0 =A0 =A0 =A0Next
> > =A0 =A0 =A0 =A0If myIP Is Nothing Then
> > =A0 =A0 =A0 =A0 =A0 =A0MessageBox.Show("Can't get the IP4 address of thi=
s
> > computer.")
> > =A0 =A0 =A0 =A0End If
> > =A0 =A0 =A0 =A0MyListener =3D New TcpListener(myIP, 7778)
> > =A0 =A0 =A0 =A0MyListener.Start()
> > =A0 =A0 =A0 =A0MyListener.BeginAcceptTcpClient(AddressOf OnClient, MyLis=
tener)


Re: Why doesn't it throw an exception to connect to an unexisting IP? by Cat

Cat
Wed Apr 23 11:22:45 CDT 2008

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=A0am, 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 alwa=
ys
> 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 fai=
l
> when you try to send data if you are not really connected anyway. TCP is g=
ood
> for this. Also if your connection gets terminated mid-flight.
> --
> Simon Hart
> Visual Developer - Device Application Development MVPhttp://simonrhart.blo=
gspot.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
> > =A0 =A0 =A0 =A0 Dim client As TcpClient =3D Nothing
> > =A0 =A0 =A0 =A0 Try
> > =A0 =A0 =A0 =A0 =A0 =A0client =3D New TcpClient()
> > =A0 =A0 =A0 =A0 =A0 =A0client.Connect("my ip address", 7778)
> > =A0 =A0 =A0 =A0 Catch ex As Exception
> > =A0 =A0 =A0 =A0 =A0 =A0MessageBox.Show(ex.Message)
> > =A0 =A0 =A0 =A0 End Try
> > =A0 =A0 =A0 =A0 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,
> > =A0 =A0 =A0 =A0 Dim mySoc As New Socket(AddressFamily.InterNetwork,
> > SocketType.Stream, ProtocolType.Tcp)
> > =A0 =A0 =A0 =A0 mySoc.BeginConnect(New IPEndPoint(IPAddress.Parse("my ip=

> > address"), 7778),
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0AddressOf OnConnect, mySoc)
> > =A0 =A0 End Sub
> > =A0 =A0 Sub OnConnect(ByVal result As IAsyncResult)
> > =A0 =A0 =A0 =A0 Dim mySoc As Socket =3D CType(result.AsyncState, Socket)=

> > =A0 =A0 =A0 =A0 mySoc.EndConnect(result)
> > =A0 =A0 =A0 =A0 System.Diagnostics.Debug.WriteLine(mySoc.Connected)
>
> > System.Diagnostics.Debug.WriteLine(mySoc.RemoteEndPoint.ToString())
> > =A0 =A0 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
> > =A0 =A0 =A0 =A0 Dim myIP As IPAddress =3D Nothing
> > =A0 =A0 =A0 =A0 For Each i As IPAddress In
> > Dns.GetHostAddresses(Dns.GetHostName())
> > =A0 =A0 =A0 =A0 =A0 =A0 If i.AddressFamily =3D AddressFamily.InterNetwor=
k Then
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 myIP =3D i
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Exit For
> > =A0 =A0 =A0 =A0 =A0 =A0 End If
> > =A0 =A0 =A0 =A0 Next
> > =A0 =A0 =A0 =A0 If myIP Is Nothing Then
> > =A0 =A0 =A0 =A0 =A0 =A0 MessageBox.Show("Can't get the IP4 address of th=
is
> > computer.")
> > =A0 =A0 =A0 =A0 End If
> > =A0 =A0 =A0 =A0 MyListener =3D New TcpListener(myIP, 7778)
> > =A0 =A0 =A0 =A0 MyListener.Start()
> > =A0 =A0 =A0 =A0 MyListener.BeginAcceptTcpClient(AddressOf OnClient, MyLi=
stener)


Re: Why doesn't it throw an exception to connect to an unexisting by srhartone

srhartone
Wed Apr 23 17:15:01 CDT 2008

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)
>
>