⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 联通的SGIP发送代码
💻 CS
字号:
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

using JLL.SGIP;

namespace ConsoleApplication1
{
    
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class SgipTest
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Socket test = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            test.Close();
            if (test == null)
            {

            }

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint hostEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8801);
            s.Connect(hostEP);

            try
            {
                Bind bind = new Bind();
                bind.LoginName = "hello";
                bind.LoginPassword = "pwd";
                bind.LoginType = 1;

                Command cmd = Command.CreateCommand(bind);
                s.Send(cmd.GetBytes());

                byte[] obj = new byte[1024];
                s.Receive(obj);
                Bind_Resp bind_resp = (Bind_Resp)Command.ToCommand(obj, 0).Body;
                if (bind_resp.Result == 0)
                {
                    Console.WriteLine("ok, connected to server!");
                    System.Threading.Thread.Sleep(1000);
                }
                
                /*
                Unbind ubind = new Unbind();
                cmd = Command.CreateCommand(ubind);
                s.Send(cmd.GetBytes());
                s.Receive(obj);
                Unbind_Resp ubind_resp = (Unbind_Resp)Command.ToCommand(obj, 0).Body;
                 */
                Console.WriteLine("waiting for server (150 second)...........");
                try
                {
                    byte[] buff = new byte[1024];
                    s.ReceiveTimeout = 150000;
                    int nRead = s.Receive(buff);
                    if (nRead == 0)
                    {
                        Console.WriteLine("server had shutdown!");
                    }
                    else
                    {
                        Console.WriteLine("ok, Server disconnected the socket. received data from server!");
                        try
                        {
                            cmd = Command.ToCommand(buff, 0);
                            Console.WriteLine("the command id:{0}, Length:{1}", cmd.Head.CommandID, cmd.Head.MessageLength);
                            Unbind_Resp r = new Unbind_Resp();
                            cmd = Command.CreateCommand(r);
                            s.Send(cmd.GetBytes());
                        }
                        catch
                        {
                        }
                    }
                }
                catch
                {
                    Console.WriteLine("hadn't receive message from server");
                }
                
                if (s.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] b = new byte[1024];
                    if (0 == s.Receive(b))
                    {
                        Console.WriteLine("server socket is disconnected!");
                    }
                }
                Console.Read();
            }
            finally
            {
                s.Shutdown(SocketShutdown.Both);
                s.Close();

                Console.Read();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -