Advertisement
Guest User

Untitled

a guest
Jan 11th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using Microsoft.SPOT;
  8. using Microsoft.SPOT.Hardware;
  9. using Microsoft.SPOT.Net.NetworkInformation;
  10. using SecretLabs.NETMF.Hardware;
  11. using SecretLabs.NETMF.Hardware.Netduino;
  12.  
  13. namespace wiznetTeste
  14. {
  15. public class Program
  16. {
  17. public static void Main()
  18. {
  19. try
  20. {
  21. SecretLabs.NETMF.Net.Wiznet5100 wiznet = new SecretLabs.NETMF.Net.Wiznet5100(SPI.SPI_module.SPI1, Pins.GPIO_PIN_D10);
  22. NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
  23. networkInterface.PhysicalAddress = new byte[] { 0x5C, 0x86, 0x4A, 0x00, 0x00, 0x00 };
  24. networkInterface.EnableStaticIP("192.168.0.59", "255.255.255.0", "192.168.0.1");
  25.  
  26. string proxy = "192.168.0.100";
  27.  
  28. try
  29. {
  30. for (int i = 1; i <= 3; i++)GetWebPage(proxy, "321", 8035);
  31.  
  32. }
  33. catch (SocketException se)
  34. {
  35. Debug.Print("SocketException when connecting to " + proxy + ".");
  36. Debug.Print(
  37. "If your network uses IPSec, you may need enable the port manually");
  38. Debug.Print("Socket Error Code: " + se.ErrorCode.ToString());
  39.  
  40. Debug.Print(se.ToString());
  41. }
  42. }
  43. catch (Exception exc)
  44. {
  45. string err = exc.Message;
  46. }
  47. }
  48.  
  49.  
  50. private static void GetWebPage(String Proxy, String URL, Int32 port)
  51. {
  52. string server = Proxy;
  53. using (Socket serverSocket = ConnectSocket(server, port))
  54. {
  55. String request = URL;
  56. Byte[] bytesToSend = Encoding.UTF8.GetBytes(request);
  57. serverSocket.Send(bytesToSend, bytesToSend.Length, 0);
  58. }
  59. }
  60.  
  61.  
  62. private static Socket ConnectSocket(String server, Int32 port)
  63. {
  64.  
  65. IPHostEntry hostEntry = Dns.GetHostEntry(server);
  66. Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
  67. socket.Connect(new IPEndPoint(hostEntry.AddressList[0], port));
  68. return socket;
  69. }
  70.  
  71. private static String GetHostFromURL(string URL)
  72. {
  73. int start = URL.IndexOf("://");
  74. int end = start >= 0 ? URL.IndexOf('/', start + 3) : URL.IndexOf('/');
  75. if (start >= 0)
  76. {
  77. start += 3;
  78.  
  79. if (end >= 0)
  80. return URL.Substring(start, end - start);
  81.  
  82. else
  83. return URL.Substring(start);
  84. }
  85. if (end >= 0)
  86. return URL.Substring(0, end + 1);
  87. return URL;
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement