Advertisement
Guest User

TCPClient

a guest
Apr 24th, 2012
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.IO;
  8. using System.Threading;
  9.  
  10. namespace TCPClient
  11. {
  12.     class TClient
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             String IP;
  17.             String Port;
  18.             String Option;
  19.  
  20.             Console.Write("Please give IP:      ");
  21.             IP = Console.ReadLine();
  22.  
  23.             Console.Write("Please give Port:    ");
  24.             Port = Console.ReadLine();
  25.             Console.WriteLine("-------------------------------");
  26.            
  27.             // Clear Console
  28.             Console.Clear();
  29.            
  30.             Hereweare:
  31.             Console.WriteLine("\n1) <Chat Mode>  - You just send messages to server");
  32.             Console.WriteLine("2) <Flood Mode> - You spam server with packets");
  33.             Console.Write("\nPlease select Mode: ");
  34.             Option = Console.ReadLine();
  35.             Console.WriteLine("-------------------------------");
  36.  
  37.             if (Option == "1")
  38.                 CMode(IP, Convert.ToInt32(Port));
  39.  
  40.             else if (Option == "2")
  41.                 Flood(IP, Convert.ToInt32(Port));
  42.  
  43.             else
  44.             {
  45.                 Console.WriteLine("Wrong Selection, Please use 1 or 2");
  46.                 Console.WriteLine("Console will be clear in 3 seconds");
  47.                 Thread.Sleep(3000);
  48.  
  49.                 // Clear Console from old messags
  50.                 Console.Clear();
  51.  
  52.                 // Go upp for repeat
  53.                 goto Hereweare;
  54.             }
  55.         }
  56.  
  57.         private static Socket socket;
  58.         private static IPEndPoint Host;
  59.         private static System.Net.Sockets.TcpClient Client;
  60.  
  61.         public static void CMode(string IP, int Port)
  62.         {
  63.             Console.Clear();
  64.  
  65.             // Create Socket
  66.             socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  67.             Host = new IPEndPoint(IPAddress.Parse(IP), Port);
  68.             Client = new TcpClient();
  69.  
  70.             try
  71.             {
  72.                 // TCPClient connect to server...
  73.                 Console.WriteLine("Connecting...");
  74.                 Client.Connect(IPAddress.Parse(IP), Port);
  75.             }
  76.             catch (Exception)
  77.             { Console.WriteLine("Connection To " + IP + " in Port " + Port + " has failed!"); return; }
  78.  
  79.             Console.WriteLine("Write your message.");
  80.             Console.WriteLine("---------------------------------");
  81.  
  82.             try
  83.             {
  84.                 while (Client.Connected)
  85.                 {
  86.                     // Read Message
  87.                     String Message = Console.ReadLine();
  88.                     Stream Stre = Client.GetStream();
  89.  
  90.                     // Get Time
  91.                     String Date = DateTime.Now.ToString("HH:mm:ss: ");
  92.  
  93.                     ASCIIEncoding ASCII = new ASCIIEncoding();
  94.                     Byte[] Buff = ASCII.GetBytes(Date + ": " + Message);
  95.  
  96.                     // Kernel - Sending packets to TARGET
  97.                     Stre.Write(Buff, 0, Buff.Length);
  98.                 }
  99.             }
  100.             catch (Exception)
  101.             { Console.WriteLine("Client Disconected"); }
  102.            
  103.             //Close TCP
  104.             Client.Close();  
  105.        
  106.         }
  107.  
  108.         public static void Flood(string IP, int Port)
  109.         {
  110.             Console.Clear();
  111.  
  112.             // Create Socket
  113.             socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  114.             Host = new IPEndPoint(IPAddress.Parse(IP), Port);
  115.             Client = new TcpClient();
  116.  
  117.             // TCPClient connect to server...
  118.             Console.WriteLine("Connecting...");
  119.  
  120.             try
  121.             {
  122.                 // Socket connect : Flood
  123.                 socket.Connect(Host);
  124.             }
  125.             catch (Exception)
  126.             { Console.WriteLine("Socket connection to {0} at Port {1} has failed!",IP,Port); }
  127.  
  128.             Console.WriteLine("Write your message.");
  129.             Console.WriteLine("---------------------------------");
  130.  
  131.             String Message = new String('*', 10);
  132.  
  133.             try
  134.             {
  135.                 while (Client.Connected)
  136.                 {
  137.                     Stream Stre = Client.GetStream();
  138.                  
  139.                     ASCIIEncoding AEncod = new ASCIIEncoding();
  140.                     Byte[] Buff = AEncod.GetBytes(Message);
  141.  
  142.                     // Kernel, send message to server.
  143.                     socket.Send(Buff);
  144.                 }
  145.             }
  146.             catch (Exception)
  147.             { Console.WriteLine("Socket Disconected"); }
  148.  
  149.             // Close Socket & TCPCliet
  150.             socket.Close();
  151.             Client.Close();
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement