test.cs

来源:互联网 发布:企业海关出口数据查询 编辑:程序博客网 时间:2024/05/19 07:42

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Ports;

using System.Net;
using System.Net.Sockets;

using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.Win32;

 

namespace ConsoleApplication2
{
    class Program
    {
        [DllImport("kernel32.dll")]
        static extern uint GetTickCount();

        [DllImport("user32")]
        static extern void MessageBeep(int type);

        static void Main(string[] args)
        {


            byte[] btarry = new byte[10];
            btarry[0] = 0x02;
            string tempstr = new ASCIIEncoding().GetString(btarry, 0, 1);
            Console.WriteLine(tempstr);

 

            //string ComputerName = Dns.GetHostName();
            //IPHostEntry myHost = new IPHostEntry();
            //myHost = Dns.Resolve(ComputerName);
            //for (int i =0; i < myHost.AddressList.Length; i++)
            //{
            //    Console.WriteLine("{0}", myHost.AddressList[i].ToString());
            //}  
   

            try
            {
                //int port = 3355;
                //string host = "192.168.0.23";

                string host = args[0];
                int port = Convert.ToInt32(args[1]);

                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                c.Connect(ipe);


                string sendStr = string.Empty;
                bool b = true;

                ulong t = GetTickCount();
                ulong t1 = t;
                ulong d = 10 * 1000;
                while (true)
                {
                    if (b)
                    {
                        sendStr = tempstr + "10      0     0";
                    }
                    else
                    {
                        sendStr = tempstr + "10  58960     0";                       
                    }


                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    c.Send(bs, bs.Length, 0);
                    Console.WriteLine(sendStr);
                    byte[] recvBytes = new byte[1024];
                    System.Threading.Thread.Sleep(1000);

                    t1 = GetTickCount();

                    if (t1-t >d)
                    {
                        t = t1;
                        b = !b;
                    }
                }

                c.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }

            Console.ReadLine();
        }
    }  
}