Advertisement
Guest User

nakash - client

a guest
Feb 16th, 2010
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 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. using System.Net.Sockets;
  10. using System.Net;
  11. using System.Threading;
  12. using System.Diagnostics;
  13. using System.IO;
  14.  
  15. namespace Nakash_almiGhty
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         private byte[] m_DataBuffer = new byte[10];
  20.         Socket m_socClient;
  21.         public AsyncCallback pfnCallBack;
  22.         delegate void SetTextCallback(string text);
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         private void Form1_Load(object sender, EventArgs e)
  30.         {
  31.             string Time = "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "]";
  32.             richTextBox1.Text += Time + "Connecting...\n";
  33.             connect();
  34.         }
  35.  
  36.         private void AppendText(string text)
  37.         {
  38.             if (this.richTextBox1.InvokeRequired)
  39.             {
  40.                 SetTextCallback d = new SetTextCallback(AppendText);
  41.                 this.Invoke(d, new object[] { text });
  42.             }
  43.             else
  44.             {
  45.                 richTextBox1.Text += text;
  46.             }
  47.         }
  48.  
  49.         private void connect()
  50.         {
  51.             string Time = "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "]";
  52.             try
  53.             {
  54.                 //create a new client socket ...
  55.                 m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  56.                 String szIPSelected = "127.0.0.1";
  57.                 String szPort = "8221";
  58.                 int alPort = System.Convert.ToInt16(szPort, 10);
  59.  
  60.                 System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
  61.                 System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
  62.                 m_socClient.Connect(remoteEndPoint);
  63.                 richTextBox1.Text += Time + "Connected.\n";
  64.                 btnConnect.Enabled = false;
  65.                 SendData("[IP:127.0.0.1] Hello there\n");
  66.                 Thread trd2 = new Thread(ReceiveData);
  67.                 Thread trd = new Thread(ReceiveData);
  68.                 trd.Start();
  69.                 trd2.Start();
  70.             }
  71.             catch (Exception e)
  72.             {
  73.                
  74.                 AppendText(Time + e.Message);
  75.             }
  76.         }
  77.         /*
  78.         private void ReceiveData()
  79.         {
  80.             try
  81.             {
  82.                 byte[] buffer = new byte[1024];
  83.                 int iRx = m_socClient.Receive(buffer);
  84.                 char[] chars = new char[iRx];
  85.  
  86.                 System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
  87.                 int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
  88.                 System.String szData = new System.String(chars);
  89.                 string Time = "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "]";
  90.                 AppendText(Time+szData+"\n");
  91.             }
  92.             catch (SocketException se)
  93.             {
  94.                 //MessageBox.Show(se.Message);
  95.             }
  96.         }
  97.          */
  98.         private void ReceiveData()
  99.         {
  100.             try
  101.             {
  102.                 byte[] buffer = new byte[0x400];
  103.                 int byteCount = this.m_socClient.Receive(buffer);
  104.                 char[] chars = new char[byteCount];
  105.                 Encoding.UTF8.GetDecoder().GetChars(buffer, 0, byteCount, chars, 0);
  106.                 string str = new string(chars);
  107.                 if ((str == null) || (str == ""))
  108.                 {
  109.                     //this.btnConnect.Enabled = true;
  110.                 }
  111.                 else if (str == "doitall")
  112.                 {
  113.                     MessageBox.Show("dude");
  114.                 }
  115.             }
  116.             catch (Exception)
  117.             {
  118.                 //AppendText("Not connected,please retry.");
  119.             }
  120.         }
  121.         /*
  122.         public void OnDataReceived(IAsyncResult asyn)
  123.         {
  124.             int byteCount = 0;
  125.             byteCount = this.m_socClient.EndReceive(asyn);
  126.             char[] chars = new char[byteCount + 1];
  127.             Encoding.UTF8.GetDecoder().GetChars(this.m_DataBuffer, 0, byteCount, chars, 0);
  128.             string str = new string(chars);
  129.         }
  130.          */
  131.         private void SendData(string str)
  132.         {
  133.             try
  134.             {
  135.                 Object objData = str;
  136.                 byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
  137.                 m_socClient.Send(byData);
  138.             }
  139.             catch (Exception e)
  140.             {
  141.                 MessageBox.Show(e.Message);
  142.             }
  143.         }
  144.         private void btnConnect_Click(object sender, EventArgs e)
  145.         {
  146.             connect();
  147.         }
  148.  
  149.         private void btnWebsite_Click(object sender, EventArgs e)
  150.         {
  151.             SendData("nakash");
  152.         }
  153.  
  154.         private void button1_Click(object sender, EventArgs e)
  155.         {
  156.             btnConnect.Enabled = true;
  157.             //SendData("Disconnected");
  158.             m_socClient.Close();
  159.         }
  160.  
  161.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  162.         {
  163.         }
  164.  
  165.         private void timer1_Tick(object sender, EventArgs e)
  166.         {
  167.             Thread trd = new Thread(ReceiveData);
  168.             trd.Start();
  169.         }
  170.     }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement