Rev. A 4-11 Programming Samples
UB-E02 Technical Reference Guide
4.7.8 Programming Sample
4.7.8.1 For Windows Console
/* UDP3289 programming sample for win32
* HOW TO BUILD
* cc wudp.cpp wsock32.lib
*/
#include <stdio.h>
#include <winsock.h>
#define MAXBUF 512
char buf[MAXBUF];
int main(int argc, char* argv[])
{
WSADATA data;
SOCKET sock;
struct sockaddr_in addr;
int i, len, fromlen;
if (argc != 2) {
printf("usage: wudp <IP address>\n");
exit(1);
}
/* initialize windows socket */
WSAStartup(0x0101, &data);
/* Create socket */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
fprintf(stderr, "Error socket(): %d\n", WSAGetLastError());
exit(1);
}
/* initialize the parameter */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(3289);
addr.sin_addr.s_addr = inet_addr(argv[1]);
/* make a packet (PRINTER STATUS) */
buf[0] = 'E';
buf[1] = 'P';
buf[2] = 'S';
buf[3] = 'O';
buf[4] = 'N';
buf[5] = 'Q'; // PacketType(Q)
buf[6] = 0x03; // DeviceType(3)
buf[7] = 0x00; // DeviceNumber(0)
buf[8] = 0x00; // Function(0010h)
buf[9] = 0x10;
buf[10] = 0x00; // Result