001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.ComponentModel;
004.
using
System.Data;
005.
using
System.Drawing;
006.
using
System.Linq;
007.
using
System.Net;
008.
using
System.Net.Sockets;
009.
using
System.Text;
010.
using
System.Threading;
011.
using
System.Windows.Forms;
012.
using
Utility;
013.
using
Microsoft.Office.Interop.Excel;
014.
015.
namespace
Wifi_Control
016.
{
017.
public
partial
class
Form1 : Form
018.
{
019.
private
Socket client;
020.
private
Thread receiver;
021.
private
byte
[] data =
new
byte
[1024];
022.
private
byte
[] data1 =
new
byte
[1024];
023.
public
string
in_data;
024.
private
string
[] getdata;
025.
private
int
val;
026.
private
DateTime datetime;
027.
028.
public
Form1()
029.
{
030.
InitializeComponent();
031.
}
032.
private
void
connected(IAsyncResult iar)
033.
{
034.
try
035.
{
036.
client.EndConnect(iar);
037.
038.
lblsta.Text =
"Connected"
;
039.
receiver =
new
Thread(
new
ThreadStart(ReceiveData));
040.
receiver.Start();
041.
042.
}
043.
044.
catch
045.
{
046.
MessageBox.Show(
"ไม่สามารถเชื่อมต่อได้ โปรดตรวจสอบข้อมูล"
);
047.
048.
}
049.
050.
}
051.
052.
private
void
ReceiveData()
053.
{
054.
int
recv;
055.
string
strData;
056.
057.
058.
while
(client.Connected)
059.
{
060.
061.
try
062.
{
063.
recv = client.Receive(data);
064.
strData = Encoding.Default.GetString(data, 0, recv);
065.
getdata = strData.Split(
','
);
066.
textBox1.Text = getdata[1];
067.
068.
069.
}
070.
catch
(SocketException ex)
071.
{
072.
MessageBox.Show(ex.Message,
"Client"
);
073.
}
074.
}
075.
076.
}
077.
078.
079.
private
void
BtnCon_Click(
object
sender, EventArgs e)
080.
{
081.
try
082.
{
083.
client =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
084.
IPEndPoint iep =
new
IPEndPoint(IPAddress.Parse(TxtIP.Text),
int
.Parse(TxtPort.Text));
085.
client.BeginConnect(iep,
new
AsyncCallback(connected), client);
086.
timer1.Start();
087.
}
088.
catch
089.
{
090.
MessageBox.Show(
"เชื่อมต่อเซิฟเวอร์ไม่สำเร็จ โปรดตรวจความถูกต้อง"
);
091.
}
092.
}
093.
094.
095.
096.
097.
private
void
SendData(IAsyncResult iar)
098.
{
099.
try
100.
{
101.
Socket remote = (Socket)iar.AsyncState;
102.
int
sent = remote.EndSend(iar);
103.
}
104.
catch
(SocketException ex)
105.
{
106.
MessageBox.Show(ex.Message);
107.
}
108.
}
109.
110.
111.
private
void
LoadData(IAsyncResult iar)
112.
{
113.
try
114.
{
115.
Socket remote = (Socket)iar.AsyncState;
116.
int
sent = remote.EndSend(iar);
117.
118.
}
119.
catch
(SocketException ex)
120.
{
121.
MessageBox.Show(ex.Message);
122.
}
123.
}
124.
125.
126.
private
void
button1_Click(
object
sender, EventArgs e)
127.
{
128.
InputTxt.Text =
"ON"
;
129.
BtnSend_Click(sender, e);
130.
}
131.
132.
private
void
button2_Click(
object
sender, EventArgs e)
133.
{
134.
InputTxt.Text =
"OFF"
;
135.
BtnSend_Click(sender, e);
136.
}
137.
138.
139.
private
void
BtnSend_Click(
object
sender, EventArgs e)
140.
{
141.
if
(client.Connected)
142.
{
143.
byte
[] input = Encoding.Default.GetBytes(InputTxt.Text);
144.
client.BeginSend(input, 0, input.Length, 0,
new
AsyncCallback(SendData), client);
145.
146.
}
147.
else
148.
{
149.
150.
MessageBox.Show(
"ไม่สามารถเชื่อมต่อเซิฟเวอร์ได้ หรือ ไม่มีการเชื่อมต่อ"
);
151.
}
152.
}
153.
154.
private
void
btnload_Click(
object
sender, EventArgs e )
155.
{
156.
if
(client.Connected)
157.
{
158.
byte
[] input = Encoding.Default.GetBytes(txtload.Text);
159.
client.BeginSend(input, 0, input.Length, 0,
new
AsyncCallback(SendLoad), client);
160.
161.
}
162.
else
163.
{
164.
165.
MessageBox.Show(
"ไม่สามารถเชื่อมต่อเซิฟเวอร์ได้"
);
166.
167.
168.
}
169.
}
170.
171.
172.
173.
private
void
timer1_Tick(
object
sender, EventArgs e)
174.
{
175.
txtload.Text =
"LOAD"
;
176.
btnload_Click(sender, e);
177.
178.
}
179.
180.
181.
182.
183.
184.
185.
186.
187.
}
188.
}