小弟今天写了段和下位机的通信代码。
在除错处理时遇到了件怪事儿。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.IO;namespace Monitor1
{
class Program
{
static void Main(string[] args)
{
//demo();
do
{
demo();
}
while (true); }
private static void demo()
{
DateTime time;
TcpClient client = new TcpClient();
client.ReceiveTimeout = 1000;
client.SendTimeout = 1000;
//Console.WriteLine("请选择连接方式:" + "\n" + "Input 1: 10.0.0.1; 2: 172.16.65.115 3:输入ip地址");
//string s = Console.ReadLine();
bool input = true;
while (input)
{
Console.WriteLine("请选择连接方式:" + "\n" + "Input 1: 10.0.0.1; 2: 172.16.65.115 3:输入ip地址");
string s = Console.ReadLine();
if (s == "0")
{ }
if (s == "1")
{
client.Connect("10.0.0.1", 502);
input = false;
}
else if (s == "2")
{
client.Connect("172.16.65.115", 502);
input = false;
}
else if (s == "3")
{
Console.WriteLine("请输入目标主机地址:");
string str = Console.ReadLine();
Regex ip = new Regex(@"^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$");
if (ip.IsMatch(str))
{
client.Connect(str, 502);
input = false;
}
else
Console.WriteLine("请输入正确的主机地址!");
}
// else
// Console.WriteLine("请选择连接方式:" + "\n" + "Input 1: 10.0.0.1; 2: 10.0.0.2 3:输入ip地址");
} Console.WriteLine("Connect:{0}-->{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
Byte[] rcv = new Byte[256]; if (client.Connected)
{
NetworkStream stream = client.GetStream();
byte[] snd = { 0, 0, 0, 0, 0, 6, 1, 3, 0, 0, 0, 10 };
Console.WriteLine("请输入通信的从机地址!");
snd[6] = byte.Parse(Console.ReadLine());
string str;
str = "";
for (int i = 0; i < snd.Length; i++)
{
str += snd[i].ToString() + ".";
}
Console.WriteLine("send:{0}", str);
time = DateTime.Now;
try
{
stream.Write(snd, 0, 12);
Console.WriteLine("共耗时:{0}", (DateTime.Now - time).TotalMilliseconds.ToString());
}
catch (IOException ex) //*******@#¥#¥% 触发这个异常,程序会挂掉
{
Console.WriteLine("共耗时:{0}", (DateTime.Now - time).TotalMilliseconds.ToString());
Console.WriteLine(ex.Message);
}
if (stream.CanRead)
{
time = DateTime.Now;
try
{
stream.Read(rcv, 0, 256);
}
catch (IOException ex) //************@#¥# 触发这个异常,程序会顺序执行,并进入下一次循环
{
Console.WriteLine("共耗时:{0}", (DateTime.Now - time).TotalMilliseconds.ToString());
Console.WriteLine(ex.Message);
}
Console.WriteLine("共耗时:{0}",(DateTime.Now-time).TotalMilliseconds.ToString());
str = "";
for (byte i = 0; i < 29; i++)
{
str += rcv[i].ToString() + ".";
}
Console.WriteLine("received:{0}", str); int[] getBack = new int[10];
for (int i = 9, j = 0; i < 29; i = i + 2)
{
getBack[j] = rcv[i] * 256 + rcv[i + 1];
j++;
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0}", getBack[i]);
}
client.Close();
Console.Read();
}
}
}
}
}
在除错处理时遇到了件怪事儿。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.IO;namespace Monitor1
{
class Program
{
static void Main(string[] args)
{
//demo();
do
{
demo();
}
while (true); }
private static void demo()
{
DateTime time;
TcpClient client = new TcpClient();
client.ReceiveTimeout = 1000;
client.SendTimeout = 1000;
//Console.WriteLine("请选择连接方式:" + "\n" + "Input 1: 10.0.0.1; 2: 172.16.65.115 3:输入ip地址");
//string s = Console.ReadLine();
bool input = true;
while (input)
{
Console.WriteLine("请选择连接方式:" + "\n" + "Input 1: 10.0.0.1; 2: 172.16.65.115 3:输入ip地址");
string s = Console.ReadLine();
if (s == "0")
{ }
if (s == "1")
{
client.Connect("10.0.0.1", 502);
input = false;
}
else if (s == "2")
{
client.Connect("172.16.65.115", 502);
input = false;
}
else if (s == "3")
{
Console.WriteLine("请输入目标主机地址:");
string str = Console.ReadLine();
Regex ip = new Regex(@"^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$");
if (ip.IsMatch(str))
{
client.Connect(str, 502);
input = false;
}
else
Console.WriteLine("请输入正确的主机地址!");
}
// else
// Console.WriteLine("请选择连接方式:" + "\n" + "Input 1: 10.0.0.1; 2: 10.0.0.2 3:输入ip地址");
} Console.WriteLine("Connect:{0}-->{1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint);
Byte[] rcv = new Byte[256]; if (client.Connected)
{
NetworkStream stream = client.GetStream();
byte[] snd = { 0, 0, 0, 0, 0, 6, 1, 3, 0, 0, 0, 10 };
Console.WriteLine("请输入通信的从机地址!");
snd[6] = byte.Parse(Console.ReadLine());
string str;
str = "";
for (int i = 0; i < snd.Length; i++)
{
str += snd[i].ToString() + ".";
}
Console.WriteLine("send:{0}", str);
time = DateTime.Now;
try
{
stream.Write(snd, 0, 12);
Console.WriteLine("共耗时:{0}", (DateTime.Now - time).TotalMilliseconds.ToString());
}
catch (IOException ex) //*******@#¥#¥% 触发这个异常,程序会挂掉
{
Console.WriteLine("共耗时:{0}", (DateTime.Now - time).TotalMilliseconds.ToString());
Console.WriteLine(ex.Message);
}
if (stream.CanRead)
{
time = DateTime.Now;
try
{
stream.Read(rcv, 0, 256);
}
catch (IOException ex) //************@#¥# 触发这个异常,程序会顺序执行,并进入下一次循环
{
Console.WriteLine("共耗时:{0}", (DateTime.Now - time).TotalMilliseconds.ToString());
Console.WriteLine(ex.Message);
}
Console.WriteLine("共耗时:{0}",(DateTime.Now-time).TotalMilliseconds.ToString());
str = "";
for (byte i = 0; i < 29; i++)
{
str += rcv[i].ToString() + ".";
}
Console.WriteLine("received:{0}", str); int[] getBack = new int[10];
for (int i = 9, j = 0; i < 29; i = i + 2)
{
getBack[j] = rcv[i] * 256 + rcv[i + 1];
j++;
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0}", getBack[i]);
}
client.Close();
Console.Read();
}
}
}
}
}
解决方案 »
- 关于Process is terminated due to StackOverflowException.的问题
- WinForm里如何给文本框添加软键盘
- Winform中的listview控件问题?
- 用httpwebrequest请求图片,response一会返回test/html一会返回图片流,怎么处理,没分,高手指教?
- C#连接Oracle数据库,连接字符串怎么写?
- 再问一个正则表达式的问题
- C# 求正则表达式
- C# Socket编程 局域网与外网客户端和服务器端反序列化问题
- 急事!!!!!!!!!!!!!!!!!!!!!!!!!!!!!找人做软件!有银子的哦!
- winform 批量插入sqllite 速度慢
- 求救!GridView 里如何设置一列显示数据库里的二进制图片!一天了还没搞定!
- C#泛型的问题
PS:各位大大有知道哪里提供免费相册,并提供外链的不。给介绍一个啊。
好上图。