using System;//引入核心命名空间,是一切类的基础
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;//语言集成查询(LINQ)是.net3.5以后推出的API,主要作用是对集合进行查询
using System.Text;
using System.Windows.Forms;
using System.Net;//引入网络操作命名空间
using System.Net.Sockets;//引入网络套接字操作命名空间
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;
namespace WifiCar
{
public partial class Form1 : Form
{
static bool Stop_Working = false;
static bool Constructions_EN = false;
static int Up_EN = 0;
static int Back_EN = 0;
static int Left_EN = 0;
static int Right_EN = 0;
static int Stop_EN = 0;
static string null_v = string.Empty;
static string data_buffer = string.Empty;
public Form1()
{
InitializeComponent();
}
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString
(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString
(string section, string key, string def, byte[] retVal, int size, string filePath);
static string FileName = Application.StartupPath + "\\Config.ini";
public string ReadIni(string Section, string Ident, string Default)
{
Byte[] Buffer = new Byte[65535];
//必须设定0(系统默认的代码页) 的编码方式,否则无法支持中文
int bufLen = GetPrivateProfileString
(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
string s = Encoding.GetEncoding(0).GetString(Buffer);
s = s.Substring(0, bufLen);
return s.Trim();
}
string CameraIp = @"http://192.168.1.1:8080/?action=snapshot"; // 摄像头IP地址
string ControlIp = "192.168.11.1"; //服务器IP地址
string Port = "2001"; //服务器端口号
string CMD_Forward = "", CMD_Backward = "", CMD_TurnLeft = "",
CMD_TurnRight = "", CMD_Stop = "", CMD_EngineUp = "",
CMD_EngineDown = "", CMD_EngineLeft = "", CMD_EngineRight = "",
CMD_OnLight = "", CMD_OffLight = "";
private void VideoButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
thread = new System.Threading.Thread(ThreadProc);
thread.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//获取窗体中所有picture控件,然后存入一个picture集合中
pictureBox1.ImageLocation = CameraIp;// "http://192.168.1.1:8080/?action=snapshot";
}
/*
private static Encoding encode = Encoding.Default;
/// <summary>
/// 监听请求
/// </summary>
/// <param name="port"></param>
void start_listen(string p)
{
IPAddress ips = IPAddress.Parse(ControlIp.ToString());//("192.168.2.1");
IPEndPoint ipe = new IPEndPoint(ips, Convert.ToInt32(Port.ToString()));
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket
// c.Connect(ipe);
// Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// c.Bind(new IPEndPoint(IPAddress.Any, Convert.ToInt32(Port.ToString())));
// c.Listen(100);
int port_listen = Convert.ToInt32(Port.ToString()); // Console.WriteLine("Listen " + port + " ...");
c.Connect(ipe);
byte[] bs = Encoding.ASCII.GetBytes(p);
c.Send(bs, bs.Length, 0);//发送测试信息
// Socket acceptSocket = c.Accept();
/*
if (Re_EN == 1)
{
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(new IPEndPoint(ips, port_listen));
listenSocket.Listen(100);
Socket acceptSocket = listenSocket.Accept();
string receiveData = Receive(acceptSocket, 5000); //5 seconds timeout.
data_buffer = receiveData;
GetData(data_buffer);
DestroySocket(acceptSocket); //import
// Console.WriteLine("Receive:" + receiveData);
}
*/
/*
if (Up_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Up_EN = 0;
}
if (Back_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Back_EN = 0;
}
if (Left_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Left_EN = 0;
}
if (Right_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Right_EN = 0;
}
if (Stop_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Stop_EN = 0;
}
*/
// DestroySocket(acceptSocket); //import
// DestroySocket(c); //import
// }
/*
/// <summary>
/// 销毁Socket对象
/// </summary>
/// <param name="socket"></param>
private static void DestroySocket(Socket socket)
{
if (socket.Connected)
{
socket.Shutdown(SocketShutdown.Both);
}
socket.Close();
}
*/
private NetworkStream networkStream;
private StreamReader streamReader;
private StreamWriter streamWriter;
private TcpClient myclient;
private Thread recvThread; // 接收信息线程
private Thread sendThread; // 发送信息线程
private Thread serverThread;// 服务线程
private Thread st;
private void Start_Button_Click_1(object sender, EventArgs e)
{
Stop_Button.Enabled = true;
//null_v = System.DateTime.Now.ToString("u");//MAC地址一样,时间更新
//Year = System.DateTime.Now.Year;
//;
//Car_Control_Start.Enabled = true;
serverThread = new Thread(new ThreadStart(Connection));
serverThread.Start();
Start_Button.Enabled = true;
}
private void Connection()
{
/*
try
{
IPAddress ipAddress = IPAddress.Parse(txtIP.Text);
Int32 port = Int32.Parse(txtPort.Text);
IPEndPoint ipe = new IPEndPoint(ipAddress, port);
myclient = new TcpClient();
myclient.Connect(ipe);
}
catch
{
MessageBox.Show("没有连接到服务器!");
return;
}
*/
if (Stop_Working == false)
{
try
{
IPAddress ipAddress = IPAddress.Parse(ControlIp.ToString());
Int32 port = 2001;//Int32.Parse(txtPort.Text);
IPEndPoint ipe = new IPEndPoint(ipAddress, port);
myclient = new TcpClient();
myclient.Connect(ipe);
}
catch
{
MessageBox.Show("没有连接到服务器!");
return;
}
networkStream = myclient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
// 创建接收信息线程,并启动
recvThread = new Thread(new ThreadStart(RecvData));
recvThread.Start();
}
}
// 接收数据
private void RecvData()
{
string s = streamReader.ReadLine();
// 如果没接到服务器退出的消息,则继续接收信息
while (s != null && !s.Equals("exit"))
{
s = streamReader.ReadLine();
SetText(s);
}
ReleaseResouce();
}
/*
private void bu
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;//语言集成查询(LINQ)是.net3.5以后推出的API,主要作用是对集合进行查询
using System.Text;
using System.Windows.Forms;
using System.Net;//引入网络操作命名空间
using System.Net.Sockets;//引入网络套接字操作命名空间
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;
namespace WifiCar
{
public partial class Form1 : Form
{
static bool Stop_Working = false;
static bool Constructions_EN = false;
static int Up_EN = 0;
static int Back_EN = 0;
static int Left_EN = 0;
static int Right_EN = 0;
static int Stop_EN = 0;
static string null_v = string.Empty;
static string data_buffer = string.Empty;
public Form1()
{
InitializeComponent();
}
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString
(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString
(string section, string key, string def, byte[] retVal, int size, string filePath);
static string FileName = Application.StartupPath + "\\Config.ini";
public string ReadIni(string Section, string Ident, string Default)
{
Byte[] Buffer = new Byte[65535];
//必须设定0(系统默认的代码页) 的编码方式,否则无法支持中文
int bufLen = GetPrivateProfileString
(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
string s = Encoding.GetEncoding(0).GetString(Buffer);
s = s.Substring(0, bufLen);
return s.Trim();
}
string CameraIp = @"http://192.168.1.1:8080/?action=snapshot"; // 摄像头IP地址
string ControlIp = "192.168.11.1"; //服务器IP地址
string Port = "2001"; //服务器端口号
string CMD_Forward = "", CMD_Backward = "", CMD_TurnLeft = "",
CMD_TurnRight = "", CMD_Stop = "", CMD_EngineUp = "",
CMD_EngineDown = "", CMD_EngineLeft = "", CMD_EngineRight = "",
CMD_OnLight = "", CMD_OffLight = "";
private void VideoButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
thread = new System.Threading.Thread(ThreadProc);
thread.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//获取窗体中所有picture控件,然后存入一个picture集合中
pictureBox1.ImageLocation = CameraIp;// "http://192.168.1.1:8080/?action=snapshot";
}
/*
private static Encoding encode = Encoding.Default;
/// <summary>
/// 监听请求
/// </summary>
/// <param name="port"></param>
void start_listen(string p)
{
IPAddress ips = IPAddress.Parse(ControlIp.ToString());//("192.168.2.1");
IPEndPoint ipe = new IPEndPoint(ips, Convert.ToInt32(Port.ToString()));
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket
// c.Connect(ipe);
// Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// c.Bind(new IPEndPoint(IPAddress.Any, Convert.ToInt32(Port.ToString())));
// c.Listen(100);
int port_listen = Convert.ToInt32(Port.ToString()); // Console.WriteLine("Listen " + port + " ...");
c.Connect(ipe);
byte[] bs = Encoding.ASCII.GetBytes(p);
c.Send(bs, bs.Length, 0);//发送测试信息
// Socket acceptSocket = c.Accept();
/*
if (Re_EN == 1)
{
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(new IPEndPoint(ips, port_listen));
listenSocket.Listen(100);
Socket acceptSocket = listenSocket.Accept();
string receiveData = Receive(acceptSocket, 5000); //5 seconds timeout.
data_buffer = receiveData;
GetData(data_buffer);
DestroySocket(acceptSocket); //import
// Console.WriteLine("Receive:" + receiveData);
}
*/
/*
if (Up_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Up_EN = 0;
}
if (Back_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Back_EN = 0;
}
if (Left_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Left_EN = 0;
}
if (Right_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Right_EN = 0;
}
if (Stop_EN == 1)
{
c.Send(bs, bs.Length, 0);//发送测试信息
Stop_EN = 0;
}
*/
// DestroySocket(acceptSocket); //import
// DestroySocket(c); //import
// }
/*
/// <summary>
/// 销毁Socket对象
/// </summary>
/// <param name="socket"></param>
private static void DestroySocket(Socket socket)
{
if (socket.Connected)
{
socket.Shutdown(SocketShutdown.Both);
}
socket.Close();
}
*/
private NetworkStream networkStream;
private StreamReader streamReader;
private StreamWriter streamWriter;
private TcpClient myclient;
private Thread recvThread; // 接收信息线程
private Thread sendThread; // 发送信息线程
private Thread serverThread;// 服务线程
private Thread st;
private void Start_Button_Click_1(object sender, EventArgs e)
{
Stop_Button.Enabled = true;
//null_v = System.DateTime.Now.ToString("u");//MAC地址一样,时间更新
//Year = System.DateTime.Now.Year;
//;
//Car_Control_Start.Enabled = true;
serverThread = new Thread(new ThreadStart(Connection));
serverThread.Start();
Start_Button.Enabled = true;
}
private void Connection()
{
/*
try
{
IPAddress ipAddress = IPAddress.Parse(txtIP.Text);
Int32 port = Int32.Parse(txtPort.Text);
IPEndPoint ipe = new IPEndPoint(ipAddress, port);
myclient = new TcpClient();
myclient.Connect(ipe);
}
catch
{
MessageBox.Show("没有连接到服务器!");
return;
}
*/
if (Stop_Working == false)
{
try
{
IPAddress ipAddress = IPAddress.Parse(ControlIp.ToString());
Int32 port = 2001;//Int32.Parse(txtPort.Text);
IPEndPoint ipe = new IPEndPoint(ipAddress, port);
myclient = new TcpClient();
myclient.Connect(ipe);
}
catch
{
MessageBox.Show("没有连接到服务器!");
return;
}
networkStream = myclient.GetStream();
streamReader = new StreamReader(networkStream);
streamWriter = new StreamWriter(networkStream);
// 创建接收信息线程,并启动
recvThread = new Thread(new ThreadStart(RecvData));
recvThread.Start();
}
}
// 接收数据
private void RecvData()
{
string s = streamReader.ReadLine();
// 如果没接到服务器退出的消息,则继续接收信息
while (s != null && !s.Equals("exit"))
{
s = streamReader.ReadLine();
SetText(s);
}
ReleaseResouce();
}
/*
private void bu