Hello,
I'm trying to establish connection to the internet using the following code, but not sure that this code is good.
I want to keep my connection for hours (it's a voip program), but the tcp sockets in my application are dropped every one hour or two. Although there is a disconnection, the device still has IP and still shows that it's connected to internet in the status bar on the top. I tried on lots of HTC devices (HD2, Pro2, Pro, etc...)
Can somebody tell if there is something wrong in the code?
Thanks,
Emil.
Code:
void Connect()
{
CONNMGR_CONNECTIONINFO ConnectionInfo;
ZeroMemory (&ConnectionInfo, sizeof (ConnectionInfo));
ConnectionInfo.cbSize = sizeof (ConnectionInfo);
ConnectionInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ConnectionInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
ConnectionInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ConnectionInfo.bExclusive = TRUE;
ConnectionInfo.guidDestNet = GetNetworkForURL (L"http://www.msn.com");
return ConnMgrEstablishConnectionSync (&ConnectionInfo, &m_hConnection, 30000, dwStatus);
}
void Disconnect()
{
HRESULT hr = ConnMgrReleaseConnection(m_hConnection, TRUE);
m_hConnection = NULL;
}
GUID ConnectionsManager::GetNetworkForURL (LPCTSTR url)
{
DWORD dwIndex = 0;
GUID rv;
if (!SUCCEEDED (ConnMgrMapURL (url, &rv, &dwIndex)))
rv = GUID_NULL;
return rv;
}
Related
Hello,
I would like to know if IntelliDialer supports of being called from an external application with a phone number in parameter?
If yes, what is its command line or whatever?
Thanks a lot,
This is a brilliant software,
Fabien.
auto-reply: RTFM
Well, I was looking for it... I spent time but probably not using the correct reference book...
What I wanted is finally working great; in MFC it could look like the foolowing:
BOOL CPhoneHelper::MakePhoneCall(CString strPhoneNumber, CString strCalledName)
{
WCHAR pszDestAddress[TAPIMAXDESTADDRESSSIZE];
WCHAR pszCalledParty[TAPIMAXCALLEDPARTYSIZE];
PHONEMAKECALLINFO pmci;
wcsncpy(pszDestAddress, strPhoneNumber, TAPIMAXDESTADDRESSSIZE-1);
wcsncpy(pszCalledParty, strCalledName, TAPIMAXCALLEDPARTYSIZE-1);
pmci.cbSize = sizeof(PHONEMAKECALLINFO);
pmci.dwFlags = PMCF_DEFAULT;
pmci.pszDestAddress = pszDestAddress;
pmci.pszAppName = NULL;
pmci.pszCalledParty = pszCalledParty;
pmci.pszComment = NULL;
if (PhoneMakeCall(&pmci) == 0)
return TRUE;
else
return FALSE;
}
Does someone know this? I want to unregister from the GSM network, but not enter the flightmode (I still need SIM access, and it should be fast). The following code works fine on a S100 (WM2003), but the 9100 (WM5) refuses to unregister. Registering works fine on both devices.
Code:
GsmControl::GsmControl()
{
errorMsg = TEXT("No ERROR/SUCCESS message to display.");
hLineApp = NULL;
hLine = NULL;
LineInitializeExParams.dwTotalSize = sizeof(LineInitializeExParams);
LineInitializeExParams.dwNeededSize = 0;
LineInitializeExParams.dwUsedSize = 0;
LineInitializeExParams.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
LineInitializeExParams.Handles.hEvent = NULL;
LineInitializeExParams.dwCompletionKey = 0;
dwLineAPIVersion = TAPI_CURRENT_VERSION;
GsmInitialize();
}
HRESULT GsmControl::GsmInitialize()
// Initializes TAPI interface
{
res = lineInitializeEx(&hLineApp, NULL, NULL, NULL, &dwNumDevs, &dwLineAPIVersion, &LineInitializeExParams);
res = lineOpen(hLineApp, 0, &hLine, dwLineAPIVersion, 0, 0, LINECALLPRIVILEGE_MONITOR, 0, NULL);
return res;
}
HRESULT GsmControl::GsmLineUnregister()
{
res = lineUnregister(hLine);
// res = lineSetEquipmentState(hLine, LINEEQUIPSTATE_NOTXRX); /* Edit: This is commented out, as I don't want any kind of flightmode behaviour */
return res;
}
HRESULT GsmControl::GsmLineRegister()
{
// lineSetEquipmentState(hLine, LINEEQUIPSTATE_FULL); /* Edit: This is commented out, as I don't want any kind of flightmode behaviour */
res = lineRegister(hLine, LINEREGMODE_AUTOMATIC, NULL, 0);
if ( res != 0 ) //FAILED(res) )
{
errorMsg = TEXT("ERROR GsmLineRegister - lineRegister\n");
} else {
errorMsg = TEXT("SUCCESS GsmLineRegister - lineRegister");
}
return res;
}
Whats the return value of the lineUnregister() call?
Sorry, I have a problem getting the value. It's larger than zero.
Shouldn't the following code open a message window that says "GsmLineUnregister returned X"?
Code:
LPTSTR lpText = new WCHAR[1024];
swprintf(lpText, TEXT("GsmLineUnregister returned $d"), gsmCtrl->GsmLineUnregister());
MessageBox(NULL, lpText, TEXT("Deva Daemon"), MB_ICONINFORMATION);
Edit: I'm making too many mistakes lately. I also commented out lineSetEquipmentState, as that's how I want it to work but it doesn't (on a 9100)
OK, I've got it now. lineUnregister returns '11'.
Edit: It's not stable, it seems to return quite all kinds of values larger than ten. Each value is larger than its predecessor. After a reboot, I got a 12, and then again increasing numbers (19, 25, 29, 31). It doesn't seem to make any sense.
(Will I ever manage to write a post with no edits this week?)
OK I am writing my first Mobile 6 App, based on compact framework 2, obviously finding some differences from Framwork vs compact when dealing with reading and writting simple values to xml;
Here is what I want to do: write two textbox values to xml as follows:
<settings>
<hostip>192.168.0.14</hostip>
<hostport>8011</hostport>
</settings>
So I got the above accomplished using the following code:
--------------------
string hstip = "";
string hstport = "";
hstip = this.HostIP.Text.ToString();
hstport = this.HostPort.Text.ToString();
XmlWriterSettings xml_settings = new XmlWriterSettings();
xml_settings.Indent = true;
xml_settings.OmitXmlDeclaration = false;
xml_settings.Encoding = Encoding.UTF8;
xml_settings.ConformanceLevel = ConformanceLevel.Auto;
using (XmlWriter xml = XmlTextWriter.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.WriteStartElement("settings");
//xml.WriteStartAttribute("hostip", "");
xml.WriteElementString("hostip", hstip);
//xml.WriteString(hstip);
//xml.WriteEndAttribute();
xml.WriteElementString("hostport", hstport);
//xml.WriteStartAttribute("hostport", "");
//xml.WriteString(hstport);
//xml.WriteEndAttribute();
xml.WriteEndElement();
//ds.WriteXml(xml);
xml.Close();
}
-----------end of write code------------
Now the part I am stumped on as I have tried all varietions to get the data back into the textboxes, I either get and exception or blank values in the textboxes.
Here is the code:
------------------
private void SrvSettings_Load(object sender, EventArgs e)
{
XmlReaderSettings xml_settings = new XmlReaderSettings();
xml_settings.IgnoreComments = true;
xml_settings.IgnoreProcessingInstructions = true;
xml_settings.IgnoreWhitespace = true;
xml_settings.CloseInput = true;
try
{
using (XmlReader xml = XmlTextReader.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.ReadStartElement("settings");
xml.Read();
xml.ReadElementString(,,)
//xml.ReadToFollowing("hostip");
this.HostIP.Text = xml.GetAttribute("hostip","");
//this.HostIP.Text = xml.GetAttribute("hostip");
//xml.ReadToFollowing("hostport");
this.HostPort.Text = xml.GetAttribute("hostport","");
// Close it out----------------
xml.ReadEndElement();
xml.Close();
//ds.ReadXml(xml);
// }
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(
ex.ToString(), "Error Loading IP settings");
}
}
------------end of read code
obviously I get an exception with this:
"System.xml.xmlexception:
"Text" is an invalid XmlNoteType. Line 2, posistion 11 at
System.xml.xmlReader.ReadEndElement()"
-----
I suspect I need to use ReadElementString, but I have tried and no workie...
Somebody have an example/class for both reading and writing per my requirements above, I would really appreciate the help..
Use XmlDocument
Try using the XmlDocument instead. It's alot easier to use for simple xml reading/writing.
Well Partially working now - I get the Hostport populated but for some reason hostip is blank even though it appears it falls through it in the case statement.. wierd..
DataSet ds = new DataSet();
XmlReaderSettings xml_settings = new XmlReaderSettings();
xml_settings.IgnoreComments = true;
xml_settings.IgnoreProcessingInstructions = true;
xml_settings.IgnoreWhitespace = true;
xml_settings.CloseInput = true;
try
{
using (XmlReader xml = XmlTextReader.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.MoveToContent();
xml.ReadStartElement("settings");
while (xml.Read())
{
//xml.Read();
switch (xml.Name)
{
case "hostip":
HostIP.Text = xml.ReadString(); <--I get Blank text
break;
case "hostport":
HostPort.Text = xml.ReadString(); <--POpulated OK
break;
default:
break;
}
// Close it out----------------
//xml.ReadEndElement();
}
xml.Close();
//ds.ReadXml(xml);
// }
}
}
Solved, Had to remove the line: xml.ReadStartElement("settings");
Thanks..
Just out of curiosity, what will your app do ?
I'm writing a WPF Windows App that monitors Server Services, IPs, Websites for up or down status; buiding a client server component to it, so the end goal will be to use the mobile app to connect to the host and download status on said servers being monitored and populate them in a listview whith thier up or done status.
Simple app but still in the works.
if you can make it customizable (to set up parametres, adreses) it wil be useful for many website administrators
a cool little app
Hey guys,
I have spent at least 24 hours trying to figure this out by myself and still havn't been able to...so I broke down and came here.
This code works...it does not (9/10s sure here...) run asyn and receives the same number of bytes as the picture should have
Code:
class ClientThread implements Runnable {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void run() {
while(true){
try {
int bytesRead;
int totalBytes = 0;
byte[] pic = new byte[1024];
byte[] inputPic_bytes = new byte[5000000];
DataInputStream dis;
DataOutputStream dos;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
dos.writeBytes("snapshot" + '\n');
int pic_size = Integer.parseInt(dis.readLine());
Log.e("TCP", "Pic size: " + pic_size);
while (totalBytes < pic_size){
bytesRead = dis.read(pic);
System.arraycopy(pic, 0, inputPic_bytes, totalBytes, bytesRead);
totalBytes += bytesRead;
pic[0] = (byte) (pic[0]&0xFF);
Log.e("TCP", "Read bytes: " + bytesRead + " Total bytes: " + totalBytes + " Bytes: " + pic[0] + " " + pic[1] + " " + pic[2]);
}
this code runs async and receives ~5k more bytes than the picture size which does not even seem possible to me (read_mode = 2 gets the pic)
Code:
protected Boolean doInBackground(Void... params) { //This runs on a different thread
boolean result = false;
try {
//create a new socket instance
SocketAddress sockaddr = new InetSocketAddress("192.168.0.5", 50007);
nsocket = new Socket();
nsocket.connect(sockaddr, 5000);//connect and set a 10 second connection timeout
if (nsocket.isConnected()) {//when connected
nis = new DataInputStream(nsocket.getInputStream());
nos = nsocket.getOutputStream();//and output stream from the socket
Log.e("Buffer Size", "Size: " + nsocket.getReceiveBufferSize());
networktask.SendDataToNetwork("sensors");
while(true){//while connected
if(read_mode == 0){
String getPicLength = nis.readLine();//read the lines coming from the socket
byte[] array_getPicLength = getPicLength.getBytes();
publishProgress(array_getPicLength);//update the publishProgress
Log.e("While_Count_0", "Count");
read_mode = 0;
}
else if(read_mode == 1){
String getPicLength = nis.readLine();//read the lines coming from the socket
byte[] array_getPicLength = getPicLength.getBytes();
publishProgress(array_getPicLength);//update the publishProgress
Log.e("While_Count_1", "Count");
read_mode = 2;
}
else if(read_mode == 2){
nis.read(pic);
publishProgress(pic);//update the publishProgress
}
}
}
Not sure if I have included enough info here...but this is seriously troubling me as I used to have it working.
What is so different about async that it is causing to receive so many extra bytes of "picture" data?
Thanks all...this is really bothering me.
I hope someone can help me.
I'm having trouble getting an Apple WeatherKit REST request working. I'm trying to follow all of the requirements listed on https://developer.apple.com/documen...equest_authentication_for_weatherkit_rest_api, but I still get a 403 response code -- and I can't locate any Java samples to follow. I've been sweating this for weeks and haven't gotten anywhere.
Here's what I have:
URL url = new URL ("https://weatherkit.apple.com/api/v1/weather/en/40.75/-73.00?dataSets=currentWeather&timezone=Europe/London");
PrivateKey pkey = getPrivateKey ("AuthKey_CXXXXXXXXX.p8");
String jwts = getJwtsString (pkey);
HttpURLConnection conn = getConnection (url, jwts);
conn.connect();
int responsecode = conn.getResponseCode();
Just in case I've got something wrong, here are the three functions I'm calling. First, the one that creates the PrivateKey from the p8 file (I'm pretty sure this one is OK):
private PrivateKey getPrivateKey (String filename) {
PrivateKey privateKey = null;
try {
AssetManager assetManager = this.getAssets();
InputStream stream = assetManager.open(filename);
byte[] bytes = new byte[stream.available()];
int bytesRead = stream.read(bytes);
byte[] pkcs8EncodedKey = new byte[0];
pkcs8EncodedKey = Base64.getDecoder().decode(bytes);
KeyFactory factory = KeyFactory.getInstance("EC");
privateKey = factory.generatePrivate(new PKCS8EncodedKeySpec(pkcs8EncodedKey));
} catch (Exception e) { }
return privateKey;
}
Next, creating the JWTS string:
private String getJwtsString(PrivateKey privateKey) {
long t1 = System.currentTimeMillis();
return Jwts.builder()
.setHeaderParam ("alg", "ES256")
.setHeaderParam ("kid", "CXXXXXXXXX")
.setHeaderParam ("id", "QXXXXXXXXX.com.mydomain.myproject")
.claim ("iss", "Q5DC438ZG4")
.claim ("iat", t1)
.claim ("exp", t1 + 60000)
.claim ("sub", "com.mydomain.myproject") // obviously, replaced with my project info
.signWith (privateKey, SignatureAlgorithm.ES256)
.compact ();
}
And finally, creating the connection:
private HttpURLConnection getConnection(URL url, String jws) {
HttpURLConnection conn = (HttpURLConnection) Objects.requireNonNull(url).openConnection();
conn.setRequestMethod ("GET")
conn.setRequestProperty ("Authorization", "Bearer "+ jws);
conn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty ("Content-Language", "en-US");
conn.setUseCaches (false);
conn.setDoInput (true);
conn.setDoOutput (true);
conn.setConnectTimeout (30000); //set timeout to 30 seconds
return conn;
}
Obviously, this is the first time I've tried creating an authenticated http call, so I wouldn't be at all surprised if this is wrong.
Can anybody supply some sample code that I could follow?
Thanks in advance...