TcpClient

A

Adam Goetz

I'm sure I'm close, just not quite there.

I have a eVC app and a CF.NET app, both are test apps at various stages of
completeness. Both are trying to connect to the same server from the same
handheld unit, though at different times of course.

So, what simple thing I have left out/gotten wrong?

eVC code that works:
.....
CString IP = "192.168.0.81";

destination_sin.sin_family = PF_INET;
destination_sin.sin_port = htons (4567);
LPTSTR tempIP;
char outIP[20];
tempIP = IP.GetBuffer(20);
for (int i = 0; i < IP.GetLength(); i++)
outIP = (char) tempIP;
outIP = 0;// terminate the string

destination_sin.sin_addr.s_addr = (DWORD) inet_addr(outIP);

if (connect (ClientSocket,
(struct sockaddr *) &destination_sin,
sizeof (destination_sin)) == SOCKET_ERROR)
{
wsprintf (szError, TEXT("Connecting socket failed. Error: %d"),
WSAGetLastError ());
MessageBox (szError);
closesocket (ClientSocket);
SetCursor(hOldCursor);
return;
};
.....

CF.NET code that returns "The requested address is not valid in its context"
.....
TcpClient client = new TcpClient();
try
{
client = new TcpClient(new IPEndPoint(IPAddress.Parse("192.168.0.81"),
4567));
}
catch (Exception e )
{
MessageBox.Show(e.Message);
}
.....
 
P

Paul G. Tobey [eMVP]

You don't want to make your endpoint have that address (that's the
constructor you've chosen -- make the socket have this IP address and port
number *on my end*), you want to connect to it. You need to read up on what
that constructor does vs. what the Connect() method does.

I think that you want to use the empty constructor and then use code similar
to what you have there with Connect, or you want to use the constructor
which takes a string and an int (the IP address string and the port number
to connect to).

Paul T.
 

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