Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace GateOpener
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string target = "192.168.1.1";
  17.             int port = 16;
  18.             string message = "titok";
  19.  
  20.  
  21.             Connect(target, port, message);
  22.         }
  23.  
  24.         static void Connect(String server, Int32 port, String message)
  25.         {
  26.             try
  27.             {
  28.                 // Create a TcpClient.
  29.                 // Note, for this client to work you need to have a TcpServer
  30.                 // connected to the same address as specified by the server, port
  31.                 // combination.
  32.                 TcpClient client = new TcpClient(server, port);
  33.  
  34.                 // Translate the passed message into ASCII and store it as a Byte array.
  35.                 Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  36.  
  37.                 // Get a client stream for reading and writing.
  38.                 //  Stream stream = client.GetStream();
  39.  
  40.                 NetworkStream stream = client.GetStream();
  41.  
  42.                 // Send the message to the connected TcpServer.
  43.                 stream.Write(data, 0, data.Length);
  44.  
  45.                 Console.WriteLine("Sent: {0}", message);
  46.  
  47.                 // Receive the TcpServer.response.
  48.  
  49.                 // Buffer to store the response bytes.
  50.                 data = new Byte[256];
  51.  
  52.                 // String to store the response ASCII representation.
  53.                 String responseData = String.Empty;
  54.  
  55.                 // Read the first batch of the TcpServer response bytes.
  56.                 Int32 bytes = stream.Read(data, 0, data.Length);
  57.                 responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  58.                 Console.WriteLine("Received: {0}", responseData);
  59.  
  60.                 // Close everything.
  61.                 stream.Close();
  62.                 client.Close();
  63.             }
  64.             catch (ArgumentNullException e)
  65.             {
  66.                 Console.WriteLine("ArgumentNullException: {0}", e);
  67.             }
  68.             catch (SocketException e)
  69.             {
  70.                 Console.WriteLine("SocketException: {0}", e);
  71.             }
  72.  
  73.             Console.WriteLine("\n Press Enter to continue...");
  74.             Console.Read();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement