Reputation: 3147
I am writing at NTRIP client on WM6. Basically I am getting data from a server using sockets by first sending a configuration. But I am unable to get it working over a GPRS connection on the same device.
I send this message.
Get / HTTP/1.0
User-Agent: NTRIP client
Accept: */*
Connection: close
To this server.
Hostname: mamba.gps.caltech.edu
Port: 2101
I make the connection by doing this
string message = "GET / HTTP/1.0\r\nUser-Agent: NTRIP client\r\nAccept: */*\r\nConnection: close\r\n\r\n"
IPAddress ipAddress = Dns.GetHostEntry(hostname).AddressList[0];
_NTRIPCaster = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_NTRIPCaster.Connect(new IPEndPoint(ipAddress, Convert.ToInt32(port)));
_NTRIPCaster.Send(Encoding.ASCII.GetBytes(message));
for (int i = 0; i < 50; i++) //Wait for upto 5 seconds for a response
{
Thread.Sleep(100);
if (_NTRIPCaster.Available > 0)
{
Byte[] inBytes = new byte[_NTRIPCaster.Available];
_NTRIPCaster.Receive(inBytes);
sourceTable += Encoding.ASCII.GetString(inBytes, 0, inBytes.Length);
//Check if all of the Source table has been recieved
if (sourceTable.Contains("ENDSOURCETABLE"))
{
sourceTableRecieved = true;
break;
}
}
}
This all works fine if I have a Wi-Fi connection, or the device is docked to a PC and active sync is sharing the PCs internet connection.
If I cut off the internet on the PC, and disable the Wi-Fi then its unable to resolve the hostname to an IP address. Doesn't even get to the socket connections. Basically it is not using the modem in the device and making use of a GPRS connection. This happens whether the GPRS is connected or not.
Since I am on WM6, I have looked at the connection manager API - http://msdn.microsoft.com/en-us/library/aa458120 .
But after following a few other posts I have been able to find on stackoverflow and other forums I have been unable to get it to work. Does anyone know how I can make a GPRS connection and start sending data to a server.
Upvotes: 1
Views: 1574
Reputation: 3147
After alot of experimenting I got it to work.
Used the ConnectionManager, in SDF from OpenNetCF
ConnectionManager connectionManager = new ConnectionManager();
connectionManager.Connect(false);
Thread.Sleep(50); //Give it time to make a connection
Next, I used the TCP/IP method of connection. Honestly, I am not sure how this differs to sockets with a TCP protocol, as from what I can tell a TCPClient object, has a property called Client which itself is a socket. Stripped down sample of code below.
using (NetworkStream ns = _client.GetStream())
using (MemoryStream ms = new MemoryStream())
{
ns.Write(messageBytes, 0, messageBytes.Length);
for (int i = 0; i < 50; i++)
{
Thread.Sleep(20);
byte[] buffer = new byte[16 * 1024];
int bytes;
while ((bytes = ns.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytes);
}
byte[] data = ms.ToArray();
response += Encoding.ASCII.GetString(data, 0, data.Length);
}
I am now getting data sent and received as expected.
Upvotes: 1
Reputation: 10855
If you use the higher level network objects like the HttpRequest then the connection manager API is automatically invoked by the .NET Framework. Is there a reason you are using low-level sockets?
Upvotes: 1