Advertisement
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;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- using System.Diagnostics;
- using System.IO;
- namespace Nakash_almiGhty
- {
- public partial class Form1 : Form
- {
- private byte[] m_DataBuffer = new byte[10];
- Socket m_socClient;
- public AsyncCallback pfnCallBack;
- delegate void SetTextCallback(string text);
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- string Time = "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "]";
- richTextBox1.Text += Time + "Connecting...\n";
- connect();
- }
- private void AppendText(string text)
- {
- if (this.richTextBox1.InvokeRequired)
- {
- SetTextCallback d = new SetTextCallback(AppendText);
- this.Invoke(d, new object[] { text });
- }
- else
- {
- richTextBox1.Text += text;
- }
- }
- private void connect()
- {
- string Time = "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "]";
- try
- {
- //create a new client socket ...
- m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- String szIPSelected = "127.0.0.1";
- String szPort = "8221";
- int alPort = System.Convert.ToInt16(szPort, 10);
- System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
- System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
- m_socClient.Connect(remoteEndPoint);
- richTextBox1.Text += Time + "Connected.\n";
- btnConnect.Enabled = false;
- SendData("[IP:127.0.0.1] Hello there\n");
- Thread trd2 = new Thread(ReceiveData);
- Thread trd = new Thread(ReceiveData);
- trd.Start();
- trd2.Start();
- }
- catch (Exception e)
- {
- AppendText(Time + e.Message);
- }
- }
- /*
- private void ReceiveData()
- {
- try
- {
- byte[] buffer = new byte[1024];
- int iRx = m_socClient.Receive(buffer);
- char[] chars = new char[iRx];
- System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
- int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
- System.String szData = new System.String(chars);
- string Time = "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "]";
- AppendText(Time+szData+"\n");
- }
- catch (SocketException se)
- {
- //MessageBox.Show(se.Message);
- }
- }
- */
- private void ReceiveData()
- {
- try
- {
- byte[] buffer = new byte[0x400];
- int byteCount = this.m_socClient.Receive(buffer);
- char[] chars = new char[byteCount];
- Encoding.UTF8.GetDecoder().GetChars(buffer, 0, byteCount, chars, 0);
- string str = new string(chars);
- if ((str == null) || (str == ""))
- {
- //this.btnConnect.Enabled = true;
- }
- else if (str == "doitall")
- {
- MessageBox.Show("dude");
- }
- }
- catch (Exception)
- {
- //AppendText("Not connected,please retry.");
- }
- }
- /*
- public void OnDataReceived(IAsyncResult asyn)
- {
- int byteCount = 0;
- byteCount = this.m_socClient.EndReceive(asyn);
- char[] chars = new char[byteCount + 1];
- Encoding.UTF8.GetDecoder().GetChars(this.m_DataBuffer, 0, byteCount, chars, 0);
- string str = new string(chars);
- }
- */
- private void SendData(string str)
- {
- try
- {
- Object objData = str;
- byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
- m_socClient.Send(byData);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- }
- private void btnConnect_Click(object sender, EventArgs e)
- {
- connect();
- }
- private void btnWebsite_Click(object sender, EventArgs e)
- {
- SendData("nakash");
- }
- private void button1_Click(object sender, EventArgs e)
- {
- btnConnect.Enabled = true;
- //SendData("Disconnected");
- m_socClient.Close();
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- Thread trd = new Thread(ReceiveData);
- trd.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement