We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

planetcall on 02/27/08


Tagged

print c fun humor


Versions (?)


Fun with C# and HP Laserjet


Published in: C# 


URL: http://www.odetocode.com/Humor/68.aspx

I have not tested it yet but the author claims to make your printer show some random messages.


  1. namespace hphack
  2. {
  3. using System;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7.  
  8. public class PrnHack
  9. {
  10. public static int Main(string[] args)
  11. {
  12. if(!ParseArgs(args))
  13. {
  14. return -1;
  15. }
  16.  
  17. Console.WriteLine("\nHP Display Hack");
  18. Console.WriteLine("Host: {0}", args[0]);
  19. Console.WriteLine("Message: {0}\n", message);
  20.  
  21. IPEndPoint ipEndPoint;
  22. ipEndPoint = new IPEndPoint( Dns.Resolve(args[0]).AddressList[0], PJL_PORT);
  23.  
  24. Console.WriteLine("Host is {0}", ipEndPoint.ToString());
  25.  
  26. Socket socket;
  27. socket = new Socket(
  28. AddressFamily.InterNetwork,
  29. SocketType.Stream,
  30. ProtocolType.Tcp
  31. );
  32.  
  33. socket.Connect(ipEndPoint);
  34.  
  35. byte [] sendData;
  36. string sendString;
  37.  
  38. sendString = String.Format(
  39. "\x1B%-12345X@PJL RDYMSG DISPLAY = \"{0}\"
  40. \x1B%-12345X
  41. ",
  42. message
  43. );
  44.  
  45. sendData = Encoding.ASCII.GetBytes(sendString);
  46.  
  47. int result;
  48. result = socket.Send(sendData, sendData.Length, 0);
  49.  
  50. if(result == 0)
  51. {
  52. Console.WriteLine("Could not send on socket");
  53. }
  54.  
  55. socket.Close();
  56.  
  57. Console.WriteLine("Finished\n\n");
  58. return 0;
  59. }
  60.  
  61.  
  62.  
  63. protected static bool ParseArgs(string[] args)
  64. {
  65. if(args.Length != 2)
  66. {
  67. Console.WriteLine(
  68. "HP Display Hack: " +
  69. "hphack printername \"message\" "
  70. );
  71. return false;
  72. }
  73.  
  74. if(args[1].Length > 16)
  75. {
  76. Console.WriteLine("Message must be <= 16 characters");
  77. return false;
  78. }
  79.  
  80. if(args[1].CompareTo("random") == 0)
  81. {
  82. message = GetRandomMessage();
  83. }
  84. else
  85. {
  86. message = args[1];
  87. }
  88.  
  89. return true;
  90. }
  91.  
  92.  
  93. public static string GetRandomMessage()
  94. {
  95. string [] Messages = {
  96. "BUZZ OFF",
  97. "TOUCH ME",
  98. "STEP AWAY",
  99. "SET TO STUN",
  100. "SCORE = 3413",
  101. "PAT EATS MICE",
  102. "FEED ME",
  103. "GO AWAY",
  104. "NEED MORE SPACE",
  105. "POUR ME A DRINK",
  106. "IN DISTRESS",
  107. "NICE SHIRT",
  108. "GO AWAY",
  109. "NO PRINT FOR YOU",
  110. "RADIATION LEAK",
  111. "HANDS UP",
  112. "PRESS MY BUTTON",
  113. "TAKE ME HOME",
  114. "LOOKS LIKE RAIN",
  115. "HELLO WORLD",
  116. "NICE HAIR",
  117. "NEED A MINT?",
  118. "BE GENTLE",
  119. "BE KIND",
  120. "INSERT DISK",
  121. "BUY ME LUNCH",
  122. "DONT STOP",
  123. "COME CLOSER",
  124. "TAKE A BREAK",
  125. "INSERT QUARTER",
  126. "BLACK SABBATH"
  127. };
  128.  
  129.  
  130. Random r = new Random();
  131. return Messages[r.Next() % Messages.Length];
  132. }
  133.  
  134. protected const int PJL_PORT = 9100;
  135. protected static string message = "NO MESSAGE";
  136.  
  137. }
  138. }

Report this snippet 

You need to login to post a comment.