Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using System.IO;
  9.  
  10. namespace ClientSocket
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             try
  17.             {
  18.                 TcpClient tcpclnt = new TcpClient();
  19.                 //Console.WriteLine("Connecting.....");
  20.  
  21.                 int PORT = 8888;
  22.                 string IP = "127.0.0.1";
  23.  
  24.                 for (int i = 0; i < args.Length; i++)
  25.                 {
  26.                     string[] arg = args[i].Split(':');
  27.                     string a = arg[0].Replace("\"", "").Trim();
  28.                     string b = arg[1].Replace("\"", "").Trim();
  29.                     switch (a)
  30.                     {
  31.                         case "-i":
  32.                             IP = b;
  33.                             break;
  34.                         case "-p":
  35.                             int.TryParse(b, out PORT);
  36.                             break;
  37.                         default:
  38.                             break;
  39.                     }
  40.                 }
  41.  
  42.                 tcpclnt.Connect(IP, PORT);
  43.                 // use the ipaddress as in the server program
  44.  
  45.                 //Console.WriteLine("Connected");
  46.  
  47.                 String str = "getData";
  48.                 Stream stm = tcpclnt.GetStream();
  49.  
  50.                 ASCIIEncoding asen = new ASCIIEncoding();
  51.                 byte[] ba = asen.GetBytes(str);
  52.                 //Console.WriteLine("Transmitting.....");
  53.  
  54.                 stm.Write(ba, 0, ba.Length);
  55.  
  56.                 byte[] bb = new byte[100];
  57.                 int k = stm.Read(bb, 0, 100);
  58.  
  59.                 for (int i = 0; i < k; i++)
  60.                     Console.Write(Convert.ToChar(bb[i]));
  61.  
  62.                 tcpclnt.Close();
  63.                 Environment.Exit(0);
  64.             }
  65.  
  66.             catch (Exception e)
  67.             {
  68.                 // Console.WriteLine("Error..... " + e.StackTrace);
  69.                 Environment.Exit(1);
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement