Skip to content

Instantly share code, notes, and snippets.

@snaka
Created April 29, 2009 05:22
Show Gist options
  • Save snaka/103597 to your computer and use it in GitHub Desktop.
Save snaka/103597 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <winsock2.h>
void sendAndRecieve(char *message) {
struct sockaddr_in server;
SOCKET sock;
char buf[2048];
// Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
// Configure socket
server.sin_family = AF_INET;
server.sin_port = htons(23053);
server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
// Connect to server
connect(sock, (struct sockaddr *)&server, sizeof(server));
// Send data (Register)
memset(buf, 0, sizeof(buf));
_snprintf(buf, sizeof(buf), message);
send(sock, buf, sizeof(buf), 0);
printf(
"<<%d\n"
"<<%s\n",
sizeof(buf),
buf
);
// Recieve data from server
memset(buf, 0, sizeof(buf));
int n = recv(sock, buf, sizeof(buf), 0);
printf(
">>%d\n"
">>%s\n",
n, buf
);
closesocket(sock);
}
int
main()
{
WSADATA wsaData;
// Initialize winsock2
WSAStartup(MAKEWORD(2,0), &wsaData);
// Register
sendAndRecieve(
"GNTP/1.0 REGISTER NONE\r\n"
"Application-Name: C sample\r\n"
"Notifications-Count: 1\r\n"
"\r\n"
"Notification-Name: Test\r\n"
"Notification-Display-Name: Test\r\n"
"Notification-Enabled: True\r\n"
"Notification-Icon: http://www.hatena.ne.jp/users/snaka72/profile.gif\r\n"
"\r\n"
);
// Notify
sendAndRecieve(
"GNTP/1.0 NOTIFY NONE\r\n"
"Application-Name: C sample\r\n"
"Notification-Name: Test\r\n"
"Notification-Display-Name: Test\r\n"
"Notification-Icon: http://www.hatena.ne.jp/users/snaka72/profile.gif\r\n"
"Notification-Title: Hoge\r\n"
"Notification-Text: This is test notification by C++.\r\n"
"\r\n"
);
// end socket
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment