Winphone push function of C language to realize __c language

Source: Internet
Author: User
Tags sprintf uuid

Winphone push function of C language implementation.

Directly on the source code:

WPPUSH.C//In Linux compile: Gcc-o wppush wppush.c-luuid #include <stdio.h> #include <stdlib.h> #include < string.h> #include <stdarg.h> #include <sys/socket.h> #include <netinet/in.h> #include <
	netdb.h> #include <uuid/uuid.h> int htconnect (char *host, int port) {int white_sock;
	struct hostent * site;

	struct sockaddr_in me;
	site = gethostbyname (host);

	if (site==null) return-2;
	White_sock = socket (af_inet,sock_stream,0);

	if (White_sock <0) return-1;
	memset (&me, 0, sizeof (struct sockaddr_in));
	memcpy (&AMP;ME.SIN_ADDR, site-> h_addr_list[0], site-> h_length);
	me.sin_family = af_inet;

	Me.sin_port = htons (port); Return (Connect (white_sock, (struct sockaddr *) &me,sizeof (struct sockaddr)) <0)?
-1:white_sock;
int htsend (int sock, char *fmt, ...)
	{char buf[1024];
	Va_list argptr;
	Va_start (ARGPTR,FMT);
	vsprintf (BUF,FMT,ARGPTR);
	Va_end (ARGPTR);
	printf ("\n%s\n", BUF); return Send (Sock,buf,strLen (BUF), 0);
	} void uuid2string (const uuid_t UU, char* const str) {int i;
	for (i=0;i<16;i++) sprintf (str+i*2, "%02x", Uu[i));
Return
} char * Mystrcat (char *dest, char *fmt, ...)
	{va_list argptr;

	Char buf[1024];
	Va_start (Argptr, FMT);
	vsprintf (buf, FMT, argptr);

	Va_end (ARGPTR);
Return strcat (dest, buf);
	} void Wppush1 (int sock, char* Devicetoken, char* alert, int badge, int type, char* svrip) {int n;
	uuid_t UU;
		Char guid[200];
	Char buf1[512];

	Char buf2[1024];
	Uuid_generate (UU); Uuid2string (UU, GUID);

	Turns the UUID into a string. sprintf (BUF1, "%s%d%s", "<?xml version=\" 1.0\ "encoding=\" "utf-8\"? ><wp:notification xmlns:wp=\ " Wpnotification\ "><wp:Tile><wp:BackgroundImage></wp:BackgroundImage><wp:Count>",

	Badge, "</wp:Count><wp:Title>qq</wp:Title></wp:Tile></wp:Notification>");

	n = strlen (BUF1);
	sprintf (Buf2, "%s%s%s%c%c", "POST", Devicetoken, "http/1.1", 13,10); Mystrcat (Buf2, "%s%c%c", "ContEnt-type:text/xml;
	Charset=utf-8 ", 13,10);
	Mystrcat (Buf2, "%s%s%c%c", "X-messageid:", guid,13,10);
	Mystrcat (Buf2, "%s%c%c", "x-notificationclass:1", 13,10);
	Mystrcat (Buf2, "%s%c%c", "X-windowsphone-target:token", 13,10);
	Mystrcat (Buf2, "%s%s%c%c", "Host:", svrip,13,10);
	Mystrcat (Buf2, "%s%d%c%c", "Content-length:", n,13,10);
	Mystrcat (Buf2, "%s%c%c", "Expect:100-continue", 13,10);
	Mystrcat (Buf2, "%s%c%c%c%c", "connection:keep-alive", 13,10,13,10);

	strcat (Buf2, BUF1);

	printf ("%s\n", buf2);

	Htsend (sock, BUF2);
Return 
	} void Wppush2 (int sock, char* Devicetoken, char* alert, int badge, int type, char* svrip, int identifier) {int n;
	uuid_t UU;
		Char guid[200];
	Char buf1[512];

	Char buf2[1024];
	Uuid_generate (UU); Uuid2string (UU, GUID);
	
		Turns the UUID into a string. if (0 = = type) {sprintf (buf1, "%s%s%s", "<?xml version=\" 1.0\ encoding=\ "utf-8\"? ><wp:notification xmlns:w P=\ "wpnotification\" &GT;&LT;WP:TOAST&GT;&LT;WP:TEXT1&GT;TELEPHONE&LT;/WP:TEXT1&GT;&LT;wp:text2> ", Alert," </wp:Text2></wp:Toast></wp:Notification> "); }else if (3 = = type) {sprintf (buf1, "%s%s%s%d%s", "<?xml" version=\ "1.0\" encoding=\ "utf-8\"? ><wp:notificati On xmlns:wp=\ "wpnotification\" ><wp:toast><wp:text1>group message</wp:text1><wp:text2> ", Alert," </wp:text2><wp:param>/startpage.xaml?groupid=, "identifier," </wp:param></wp:toast
		></wp:Notification> "); }else {sprintf (buf1, "%s%d%s", "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? ><wp:notification xmlns:wp=\" WPNot ") Ification\ "><wp:toast><wp:text1>message</wp:text1><wp:text2>hi, there is new news. </wp:text2><wp:param>/startpage.xaml?umid= ", identifier," </wp:param></wp:toast></wp:
		Notification> ");

		} n = strlen (BUF1);
		sprintf (Buf2, "%s%s%s%c%c", "POST", Devicetoken, "http/1.1", 13, 10); Mystrcat (Buf2, "%s%c%c", "Content-type:text/xml";
		Charset=utf-8 ", 13, 10); MYstrcat (Buf2, "%s%s%c%c", "X-messageid:", guid,13,10);
		Mystrcat (Buf2, "%s%c%c", "X-notificationclass:2", 13,10);
		Mystrcat (Buf2, "%s%c%c", "X-windowsphone-target:toast", 13,10);
		Mystrcat (Buf2, "%s%s%c%c", "Host:", svrip,13,10);
		Mystrcat (Buf2, "%s%d%c%c", "Content-length:", n,13,10);
		Mystrcat (Buf2, "%s%c%c", "Expect:100-continue", 13,10);
		Mystrcat (Buf2, "%s%c%c%c%c", "connection:keep-alive", 13,10,13,10);

		strcat (Buf2, BUF1);

		Htsend (sock, BUF2);
Return
	} void Wppush4 (int sock, char* Devicetoken, char* alert, int badge, int type, char* svrip, int identifier) {int n;
	uuid_t UU;
	time_t timetmp;
	Char guid[200];
		Char buf[100];
	Char buf1[512];

	Char buf2[1024];
	Uuid_generate (UU); Uuid2string (UU, GUID);

	Turns the UUID into a string. 
	Time (&AMP;TIMETMP);

	Ctime_r (&timetmp, buf);
	printf ("%d,%s\n", timetmp, BUF);
	n = strlen (BUF);
	while (' \ n ' = buf[--n]);

	Buf[n+1] = ' the ';
sprintf (BUF1, "%s%c%c", "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>", 13, 10);	Mystrcat (BUF1, "%s%c%c", "<notification xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ "xmlns:xsd=\"
	Http://www.w3.org/2001/xmlschema\ "xmlns=\" wpnotification\ ">", 13, 10);
	Mystrcat (BUF1, "%s%s%s%c%c", "<name xmlns=\" \ ">", Alert, ":</name>", 13, 10);
	Mystrcat (BUF1, "%s%d%s%c%c", "<number xmlns=\" \ ">", identifier, "</Number>", 13, 10);
	Mystrcat (BUF1, "%s%d%s%c%c", "<datetime xmlns=\" \ ">", buf, "</DateTime>", 13, 10);
	Mystrcat (BUF1, "%s%d%s%c%c", "<count xmlns=\" \ ">", Badge, "</Count>", 13, 10);

	Mystrcat (BUF1, "%s%c%c%s", "<sessionid xmlns=\" \ "></SessionId>", "a", "</Notification>");

	n = strlen (BUF1);
	sprintf (Buf2, "%s%s%s%c%c", "POST", Devicetoken, "http/1.1", 13, 10); Mystrcat (Buf2, "%s%c%c", "Content-type:text/xml";
	Charset=utf-8 ", 13, 10);
	Mystrcat (Buf2, "%s%c%c", "X-notificationclass:4", 13,10);
	Mystrcat (Buf2, "%s%c%c", "Host:db3.notify.live.net", 13, 10); Mystrcat (Buf2, "%s%d%c%c", "COntent-length: ", n,13,10);
	Mystrcat (Buf2, "%s%c%c", "Expect:100-continue", 13,10);
	Mystrcat (Buf2, "%s%c%c%c%c", "connection:keep-alive", 13,10,13,10);

	strcat (Buf2, BUF1);

	Htsend (sock, BUF2);
Return
	} void Wppush5 (int sock, char* Devicetoken, char* alert, int badge, int type, char* svrip) {int n;
	uuid_t UU;
	time_t timetmp;
	Char guid[200];
		Char buf[100];
	Char buf1[512];

	Char buf2[1024];
	Uuid_generate (UU); Uuid2string (UU, GUID);

	Turns the UUID into a string. sprintf (BUF1, "%s%d%s", "<?xml version=\" 1.0\ "encoding=\" "utf-8\"? ><wp:notification xmlns:wp=\ " wpnotification\ "version=\" 2.0\ "> <wp:tile template=\" iconictile\ "><wp:widecontent1>message</wp" : Widecontent1><wp:widecontent2>hi, there's new news. </wp:WideContent2><wp:WideContent3> </wp:widecontent3><wp:backgroundimage></wp: Backgroundimage><wp:count> ", badge," </wp:count><wp:backgroundcolor></wp:backgroundcolor ></wp:tile></wp:notificaTion> ");

	n = strlen (BUF1);
	sprintf (Buf2, "%s%s%s%c%c", "POST", Devicetoken, "http/1.1", 13, 10); Mystrcat (Buf2, "%s%c%c", "Content-type:text/xml";
	Charset=utf-8 ", 13, 10);
	Mystrcat (Buf2, "%s%s%c%c", "X-messageid:", guid,13,10);
	Mystrcat (Buf2, "%s%c%c", "x-notificationclass:1", 13,10);
	Mystrcat (Buf2, "%s%c%c", "X-windowsphone-target:token", 13,10);
	Mystrcat (Buf2, "%s%s%c%c", "Host:", Svrip, 13, 10);
	Mystrcat (Buf2, "%s%d%c%c", "Content-length:", N, 13, 10);
	Mystrcat (Buf2, "%s%c%c", "Expect:100-continue", 13, 10);
	Mystrcat (Buf2, "%s%c%c%c%c", "connection:keep-alive", 13,10,13,10);

	strcat (Buf2, BUF1);

	Htsend (sock, BUF2);
Return
	} void Wppush6 (int sock, char* Devicetoken, char* alert, int badge, int type, char* svrip, int identifier) {int n;
	uuid_t UU;
	time_t timetmp;
	Char guid[200];
		Char buf[100];
	Char buf1[512];

	Char buf2[1024];
	Uuid_generate (UU); Uuid2string (UU, GUID);

	Turns the UUID into a string. if (0 = = type) {sprintf (buf1, "%s%s%s", "<?xml Version=\ "1.0\" encoding=\ "utf-8\" ><wp:notification xmlns:wp=\ "wpnotification\" ><wp:Toast><wp :text1>telephone</wp:text1><wp:text2> ", Alert," </wp:text2></wp:toast></wp:

	Notification> "); }else if (1 = = type) {sprintf (buf1, "%s%d%s", "<?xml" version=\ "1.0\" encoding=\ "utf-8\"? ><wp:notification xmln S:wp=\ "wpnotification\" ><wp:toast><wp:text1>message</wp:text1><wp:text2>hi, there are new news. </wp:text2><wp:param>/view/chatview.xaml?type=1&index=0&id= ", identifier," </wp:Param>
	</wp:Toast></wp:Notification> "); }else if (3 = = type) {sprintf (buf1, "%s%d%s%d%s", "<?xml" version=\ "1.0\" encoding=\ "utf-8\"? ><wp:notification Xmlns:wp=\ "wpnotification\" ><wp:toast><wp:text1>group message</wp:text1><wp:text2> ", Alert, "</wp:text2><wp:param>/startpage.xaml?groupid=", identifier, "</wp:param></wp:toast ></wp:notification>");
	}
	else {return;

	} n=strlen (BUF1);
	sprintf (Buf2, "%s%s%s%c%c", "POST", Devicetoken, "http/1.1", 13, 10); Mystrcat (Buf2, "%s%c%c", "Content-type:text/xml";
	Charset=utf-8 ", 13, 10);
	Mystrcat (Buf2, "%s%s%c%c", "X-messageid:", guid,13,10);
	Mystrcat (Buf2, "%s%c%c", "X-notificationclass:2", 13,10);
	Mystrcat (Buf2, "%s%c%c", "X-windowsphone-target:toast", 13,10);
	Mystrcat (Buf2, "%s%s%c%c", "Host:", Svrip, 13, 10);
	Mystrcat (Buf2, "%s%d%c%c", "Content-length:", N, 13, 10);
	Mystrcat (Buf2, "%s%c%c", "Expect:100-continue", 13, 10);
	Mystrcat (Buf2, "%s%c%c%c%c", "connection:keep-alive", 13,10,13,10);

	strcat (Buf2, BUF1);

	Htsend (sock, BUF2);
Return
	} void Wppush7 (int sock, char* Devicetoken, char* alert, int badge, int type, char* svrip, int identifier) {int n;
	uuid_t UU;
	time_t timetmp;
	Char guid[200];
		Char buf[100];
	Char buf1[512];

	Char buf2[1024];
	Uuid_generate (UU); Uuid2string (UU, GUID);

	Turns the UUID into a string. 
	Time (&AMP;TIMETMP); Ctime_r (&timEtmp, BUF);
	printf ("%d,%s\n", timetmp, BUF);
	n = strlen (BUF);
	while (' \ n ' = buf[--n]);

	Buf[n+1] = ' the ';
	sprintf (BUF1, "%s%c%c", "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>", 13, 10); Mystrcat (BUF1, "%s%c%c", "<notification xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ "xmlns:xsd=\"
	Http://www.w3.org/2001/xmlschema\ "xmlns=\" wpnotification\ ">", 13, 10);
	Mystrcat (BUF1, "%s%s%s%c%c", "<name xmlns=\" \ ">", Alert, ":</name>", 13, 10);
	Mystrcat (BUF1, "%s%d%s%c%c", "<number xmlns=\" \ ">", identifier, "</Number>", 13, 10);
	Mystrcat (BUF1, "%s%s%s%c%c", "<datetime xmlns=\" \ ">", buf, "</DateTime>", 13, 10);
	Mystrcat (BUF1, "%s%d%s%c%c", "<count xmlns=\" \ ">", Badge, "</Count>", 13, 10);
	Mystrcat (BUF1, "%s%s%s%c%c", "<sessionid xmlns=\" \ ">", Alert, "</SessionId>", 13, 10);

	Mystrcat (BUF1, "%s%c%c%s", "<type xmlns=\" \ ">0</Type>", "a", "</Notification>");

	n = strlen (BUF1); sprintf (BUF2, "%s%s%s%c%c", "POST", Devicetoken, "http/1.1", 13, 10); Mystrcat (Buf2, "%s%c%c", "Content-type:text/xml";
	Charset=utf-8 ", 13, 10);
	Mystrcat (Buf2, "%s%c%c", "X-notificationclass:4", 13,10);
	Mystrcat (Buf2, "%s%c%c", "Host:db3.notify.live.net", 13, 10);
	Mystrcat (Buf2, "%s%d%c%c", "Content-length:", N, 13, 10);
	Mystrcat (Buf2, "%s%c%c", "Expect:100-continue", 13, 10);
	Mystrcat (Buf2, "%s%c%c%c%c", "connection:keep-alive", 13,10,13,10);

	strcat (Buf2, BUF1);

	Htsend (sock, BUF2);
Return
	int main (int argc,char **argv) {int n, socket;

	Char buf[2048]; if (argc<2) {printf ("Please input the hostName.")
		Example:\n%s www.sohu.com\n ", argv[0]);
	Return
	Socket = Htconnect (argv[1],80); if (socket <1) {printf ("Create CONNECCT failed.
		Socket:%d\n ", socket);
	Return

	printf ("Send push request: \ n"); WPPUSH6 (socket, "http://hk1.notify.live.net/throttledthirdparty/01.00/ Aqetf6fm0duxqlg2ijzviohvagaaaaadehadaaquzm52ojjcnuzfouuyqtmzoui0rjqfbkxfr0fdwq "," This is MessagE test. ", 3, 1," hk1.notify.live.net ", 20056);

	printf ("\ n receive the information returned by the push server: \ n \ nthe");
	n = read (socket, buf, sizeof (BUF));
	Buf[n] = ' the ';

	printf ("%s", buf);
Close (socket); }


Implementation results (for example):

#./wppush hk1.notify.live.net
Send Request start
... ...

Receive information returned by the push server:

http/1.1 OK
Cache-control:private
server:microsoft-iis/7.5
x-deviceconnectionstatus:connected
X-notificationstatus:received
X-subscriptionstatus:active
x-messageid:b3bc6d8c-179d-4b4c-9642-c1674a86820b
activityid:18bb002b-d2bc-4254-9acf-c4c596dbd14c
x-server:hk1mpnsm016
x-aspnet-version:4.0.30319
X-powered-by:asp.net
Date:fri, 09:32:39 GMT
content-length:0



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.