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.
014.
namespace
Wifi_Control
015.
{
016.
public
partial
class
Form1 : Form
017.
{
018.
private
Socket client;
019.
private
Thread receiver;
020.
private
byte
[] data =
new
byte
[1024];
021.
private
byte
[] data1 =
new
byte
[1024];
022.
public
Form1()
023.
{
024.
InitializeComponent();
025.
}
026.
private
void
connected(IAsyncResult iar)
027.
{
028.
try
029.
{
030.
client.EndConnect(iar);
031.
032.
lblsta.Text =
"Connected"
;
033.
receiver =
new
Thread(
new
ThreadStart(ReceiveData));
034.
receiver.Start();
035.
036.
}
037.
038.
catch
039.
{
040.
MessageBox.Show(
"ไม่สามารถเชื่อต่อได้ โปรดตรวจสอบข้อมูล"
);
041.
042.
}
043.
044.
}
045.
046.
private
void
ReceiveData()
047.
{
048.
int
recv;
049.
string
strData;
050.
051.
052.
while
(client.Connected)
053.
{
054.
055.
try
056.
{
057.
recv = client.Receive(data);
058.
strData = Encoding.Default.GetString(data, 0, recv);
059.
string
[] getdata = strData.Split(
','
);
060.
textBox1.Text = getdata[0];
061.
textBox2.Text = getdata[1];
062.
063.
064.
065.
066.
067.
068.
}
069.
catch
(SocketException ex)
070.
{
071.
MessageBox.Show(ex.Message,
"Client"
);
072.
}
073.
}
074.
075.
}
076.
077.
private
void
BtnCon_Click(
object
sender, EventArgs e)
078.
{
079.
try
080.
{
081.
client =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
082.
IPEndPoint iep =
new
IPEndPoint(IPAddress.Parse(TxtIP.Text),
int
.Parse(TxtPort.Text));
083.
client.BeginConnect(iep,
new
AsyncCallback(connected), client);
084.
085.
}
086.
catch
087.
{
088.
MessageBox.Show(
"เชื่อมต่อเซิฟเวอร์ไม่สำเร็จ โปรดตรวจความถูกต้อง"
);
089.
}
090.
}
091.
092.
private
void
label2_Click(
object
sender, EventArgs e)
093.
{
094.
095.
}
096.
private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)
097.
{
098.
reg[
"port"
] = TxtPort.Text;
099.
reg[
"url"
] = TxtIP.Text;
100.
try
101.
{
102.
BtnCon_Click(sender, e);
103.
}
104.
catch
105.
{
106.
}
107.
}
108.
string
port =
""
;
109.
string
url =
""
;
110.
MyReg reg =
new
MyReg(
"ArduinoAll"
);
111.
private
void
Form1_Load(
object
sender, EventArgs e)
112.
{
113.
114.
timer1.Interval = 1000;
115.
timer1.Enabled =
true
;
116.
117.
118.
119.
try
120.
{
121.
port = reg[
"port"
].ToString();
122.
url = reg[
"url"
].ToString();
123.
124.
TxtIP.Text = url;
125.
TxtPort.Text = port;
126.
}
127.
catch
128.
{
129.
reg[
"port"
] =
""
;
130.
reg[
"url"
] =
""
;
131.
}
132.
133.
134.
}
135.
136.
private
void
BtnDis_Click(
object
sender, EventArgs e)
137.
{
138.
if
(client.Connected)
139.
{
140.
receiver.Abort(client);
141.
lblsta.Text =
"Disconnected"
;
142.
client.Close(5);
143.
}
144.
else
145.
{
146.
MessageBox.Show(
"ไม่มีการเชื่อมต่อ"
);
147.
}
148.
}
149.
private
void
SendData(IAsyncResult iar)
150.
{
151.
try
152.
{
153.
Socket remote = (Socket)iar.AsyncState;
154.
int
sent = remote.EndSend(iar);
155.
}
156.
catch
(SocketException ex)
157.
{
158.
MessageBox.Show(ex.Message);
159.
}
160.
}
161.
private
void
button1_Click(
object
sender, EventArgs e)
162.
{
163.
InputTxt.Text =
"ON"
;
164.
BtnSend_Click(sender, e);
165.
}
166.
167.
private
void
BtnSend_Click(
object
sender, EventArgs e)
168.
{
169.
if
(client.Connected)
170.
{
171.
byte
[] input = Encoding.Default.GetBytes(InputTxt.Text);
172.
client.BeginSend(input, 0, input.Length, 0,
new
AsyncCallback(SendData), client);
173.
richTextBox1.AppendText(
"ส่ง: "
+ InputTxt.Text +
"\n"
);
174.
}
175.
else
176.
{
177.
178.
MessageBox.Show(
"ไม่สามารถเชื่อมต่อเซิฟเวอร์ได้ หรือ ไม่มีการเชื่อมต่อ"
);
179.
}
180.
}
181.
182.
183.
184.
185.
186.
private
void
timer1_Tick(
object
sender, EventArgs e)
187.
{
188.
189.
190.
191.
}
192.
193.
194.
}
195.
}