小弟今天写了段和下位机的通信代码。
在除错处理时遇到了件怪事儿。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();
                    }
                }
        }
    }
}

解决方案 »

  1.   

    见注释中的  //*******@#¥#¥%   位置不知道怎么上图,求解释,求安慰。
    PS:各位大大有知道哪里提供免费相册,并提供外链的不。给介绍一个啊。
    好上图。
      

  2.   

    论坛左上角上点击“微博”,进入后,左侧菜单中有个“相册”,点击就可以上传图片了。
      

  3.   

    日你妹的天涯,不允许直接外链图片但是只要你先把图片上传到相册,然后在随便回一个帖子,把图片插入,然后退出天涯(一定要退出,或者换一个没登陆天涯的浏览器,),到同一个帖子下面去找自己刚才的回帖,再复制图片地址,就可以外链了。
      

  4.   

    我勒个去,我还到天涯整了半天,曲线迂回上的图