当前位置: 首页>>代码示例>>C#>>正文


C# Socket.Disconnect方法代码示例

本文整理汇总了C#中System.Net.Sockets.Socket.Disconnect方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.Disconnect方法的具体用法?C# Socket.Disconnect怎么用?C# Socket.Disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.Socket的用法示例。


在下文中一共展示了Socket.Disconnect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: btnkq_Click

 private void btnkq_Click(object sender, EventArgs e)
 {
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     IPEndPoint ie = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
     s.Connect(ie);
     byte[] data = new byte[1024];
     int k = s.Receive(data);
     MessageBox.Show("welcome",Encoding.ASCII.GetString(data,0,k));
     while (true)
     {
         String strA = txta.Text.ToString();
         String strB = txtb.Text.ToString();
         String str = strA +  strB;
         byte[] dl = new byte[1024];
         dl = Encoding.ASCII.GetBytes(str);
         s.Send(dl, dl.Length, SocketFlags.None);
         if (str.ToUpper().Equals("QUIT"))
             break;
         dl = new byte[1024];
         int k1 = s.Receive(dl);
         String KQ = Encoding.ASCII.GetString(dl,0,k1);
         txtkq.Text = KQ.ToString();
     }
     s.Disconnect(true);
     s.Close();
 }
开发者ID:pthdnq,项目名称:csharp-project-nduang,代码行数:26,代码来源:Form1.cs

示例2: Main

 static void Main(string[] args)
 {
     Socket s=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
     IPEndPoint ie=new IPEndPoint(IPAddress.Parse("127.0.0.1"),9000);
     s.Connect(ie);
     Console.WriteLine("Connected to Server.....");
     byte[] data=new byte[1024];
     int k=s.Receive(data);
     Console.WriteLine("Loi chao tu Server:{0}",Encoding.ASCII.GetString(data,0,k));
     while(true)
     {
         Console.WriteLine("Moi nhap du lieu can tinh");
         string st=Console.ReadLine();
         byte[] dl=new byte[1024];
         dl=Encoding.ASCII.GetBytes(st);
         s.Send(dl,dl.Length,SocketFlags.None);
         if(st.ToUpper().Equals("QUIT"))
             break;
         dl=new byte[1024];
         int k1=s.Receive(dl);
         Console.WriteLine("Ket qua tinh tong tu server tra ve:{0}", Encoding.ASCII.GetString(dl, 0, k1));
     }
     s.Disconnect(true);
     s.Close();
 }
开发者ID:pthdnq,项目名称:csharp-project-nduang,代码行数:25,代码来源:Program.cs

示例3: button1_Click

        private void button1_Click(object sender, EventArgs ev)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            s.Connect("192.168.16.170", 10000);

            byte[] buf;

            buf = new byte[] { 0x00,0x06,0x10,0x12,0x00,0x00 };
            
            s.Send(buf);

            byte[] rec=new byte[128];
            int r = 0;

            s.ReceiveTimeout = 1000;

            try
            {
                r = s.Receive(rec);
                Debug.WriteLine(FUNC.BytesToString(rec, 0, r));

                r = s.Receive(rec);
                Debug.WriteLine(FUNC.BytesToString(rec, 0, r));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

            s.Disconnect(false);
        }
开发者ID:puyd,项目名称:AndonSys.Test,代码行数:32,代码来源:fmMain.cs

示例4: CheckEndpoint

        private static bool CheckEndpoint(IPEndPoint solrEndpoint)
        {
            var valid = false;
            using (var s = new Socket(solrEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
            {
                try
                {
                    s.Connect(solrEndpoint);
                    if (s.Connected)
                    {
                        valid = true;
                        s.Disconnect(true);
                    }
                    else
                    {
                        valid = false;
                    }
                }
                catch
                {
                    valid = false;
                }
            }

            return valid;
        }
开发者ID:enekazo,项目名称:Windows-Azure-Solr,代码行数:26,代码来源:Util.cs

示例5: Scan

        public static IEnumerable<int> Scan(string ip, int startPort, int endPort)
        {
            List<int> openPorts = new List<int>();

            for (int port = startPort; port <= endPort; port++)
            {
                Debug.WriteLine(string.Format("Scanning port {0}", port));
                Socket scanSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

                try
                {
                    scanSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
                    scanSocket.Disconnect(false);
                    openPorts.Add(port);
                    //scanSocket.BeginConnect(new IPEndPoint(ip, port), ScanCallBack, new ArrayList() { scanSocket, port });

                }
                catch(Exception ex)
                {
                    //bury exception since it means we could not connect to the port
                }

            }

            return openPorts;
        }
开发者ID:Ranthalion,项目名称:PortScan,代码行数:26,代码来源:PortScanner.cs

示例6: TestMethod1

        public void TestMethod1()
        {
            Uri uri = new Uri("http://bionic-university.com/");
            string message = string.Format(
                "GET {0} HTTP/1.1\r\nHost: {1}\r\nConnection: close\r\nAccept: text/html\r\ntUser-Agent: MyBrowser\r\n\r\n", uri.PathAndQuery, uri.Host);
            string output = string.Empty;
            IPHostEntry IpHost = Dns.GetHostEntry(uri.Host);
            IPAddress[] ips = IpHost.AddressList;
            IPEndPoint ipe = new IPEndPoint(ips[0], uri.Port);
            using (Socket socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect(ipe);
                byte[] bytesSent = Encoding.ASCII.GetBytes(message);
                byte[] received = new byte[1024];
                socket.Send(bytesSent);
                int bytes = 0;
                do
                {
                    bytes = socket.Receive(received, received.Length, 0);
                    output += Encoding.ASCII.GetString(received);
                } while (bytes > 0);
                socket.Disconnect(false);

            }
        }
开发者ID:yehorchankov,项目名称:socket-browser,代码行数:25,代码来源:UnitTest1.cs

示例7: Disconnect

 public void Disconnect(Socket s)
 {
     if (s.Connected)
     {
         s.Shutdown(SocketShutdown.Both);
         s.Disconnect(true);
         s.Close();
     }
 }
开发者ID:CarlosX,项目名称:TestProjects,代码行数:9,代码来源:Networking.cs

示例8: StartListening

        public static void StartListening()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // The DNS name of the computer
            // running the listener is "host.contoso.com".
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11337);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            socket = listener;

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);
                running = true;

                while (running == true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    ServerLog(null, new Logging.LogEventArgs("Waiting for a connection..."));
                    listener.BeginAccept(
                        new AsyncCallback(AcceptCallback),
                        listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }
                listener.Disconnect(true);

            }
            catch (Exception e)
            {
                ServerLog(null, new Logging.LogEventArgs(e.ToString()));
            }

            ServerLog(null, new Logging.LogEventArgs("\nPress ENTER to continue..."));
            Console.Read();

        }
开发者ID:nawns,项目名称:lundgren,代码行数:51,代码来源:SocketServer.cs

示例9: CloseSocket

 protected void CloseSocket(Socket socket)
 {
     try
     {
         socket.Close();
         socket.Disconnect(false);
     }
     catch (ObjectDisposedException)
     {
     }
     catch (SocketException)
     {
     }
 }
开发者ID:ApInvent,项目名称:SyncPaintBoard,代码行数:14,代码来源:TransportBase.cs

示例10: Connect_Click

        private void Connect_Click(object sender, EventArgs e)
        {
            if (ConnectToRobot.Checked)
            {

                Socket m_CommandSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    IPAddress AddressToUse = null;
                    if (!IPAddress.TryParse(HostName.Text,out AddressToUse))
                    {

                        foreach (IPAddress Address in Dns.GetHostEntry(HostName.Text).AddressList)
                            if (Address.AddressFamily == AddressFamily.InterNetwork)
                                AddressToUse = Address;
                    }

                    m_CommandSocket.ReceiveTimeout = 1000;
                    m_CommandSocket.SendTimeout = 1000;

                    m_CommandSocket.Connect(new IPEndPoint(AddressToUse, 3000));

                    byte[] buf = new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 };

                    m_CommandSocket.Send(buf);

                    m_CommandSocket.Receive(buf);

                    m_CommandSocket.Disconnect(false);

                }
                catch
                {
                    MessageBox.Show("Cannot connect to specified host");
                    DialogResult = DialogResult.Retry;
                    return;
                }

                RegistryKey SettingsKey = Registry.CurrentUser.CreateSubKey("Software\\Nasa\\NasaBot");
                SettingsKey.SetValue("Host", HostName.Text);

            }//ConnectToRobot end

            //            else if (NavigationPlanning.Checked)
            //            {
            //                m_ControlWindow = new Control(m_Robot);
            //               m_ControlWindow.Show();
            //            }
            Close();
        }
开发者ID:anareboucas,项目名称:nanook,代码行数:50,代码来源:ConnectionDialog.cs

示例11: ProcessConnection

        private async void ProcessConnection(Socket Client)
        {
            NetworkStream ClientStream = new NetworkStream(Client);

            if (await this.Handshaker.HandshakeConnection(ClientStream))
            {
                this.FinalizeConnection(Client, ClientStream);
            }
            else
            {
                //find propper way to disconnect
                Client.Disconnect(false);
            }
        }
开发者ID:Vengarioth,项目名称:WebSocketsImplementation,代码行数:14,代码来源:Server.cs

示例12: Disconnect

 public void Disconnect(Socket s)
 {
     try
     {
         if (s != null && s.Connected)
         {
             s.Disconnect(true);
         }
         //s.Shutdown(SocketShutdown.Both);
     }
     catch (Exception ex)
     {
         Log.Exception("[Client.Disconnect] ", ex);
     }
 }
开发者ID:ManhHaHY,项目名称:ClowenEmulationOpenSourceProject,代码行数:15,代码来源:SRClient.cs

示例13: SendCommand

 public void SendCommand(string command)
 {
     try
     {
         var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         socket.Connect(_EndPoint);
         socket.Send(Encoding.UTF8.GetBytes(command));
         socket.Disconnect(true);
         socket.Close();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         throw;
     }
 }
开发者ID:jbritz,项目名称:duinofly.NET,代码行数:16,代码来源:NetworkCommander.cs

示例14: GetResponse

 public byte[] GetResponse()
 {
     IPAddress serverIp = IPAddress.Parse(NetOperator.serverIP);
     IPEndPoint iep = new IPEndPoint(serverIp, NetOperator.serverPort);
     Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket.Connect(iep);
     //byte[] byteMessage;
     //byteMessage = Encoding.ASCII.GetBytes(content);
     socket.Send(content);
     byte[] temp = new byte[1024];
     int read = socket.Receive(temp, 0, 1024, SocketFlags.None);
     byte[] result=new byte[read];
        Array.Copy(temp, 0, result, 0, read);
     socket.Shutdown(SocketShutdown.Both);
     socket.Disconnect(false);
     socket.Close();
     return temp;
 }
开发者ID:edwinv710,项目名称:TalkingHead,代码行数:18,代码来源:NetOperator.cs

示例15: SocketConnectTo

        /// <summary>
        /// 测试socket连接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="err"></param>
        /// <returns></returns>
        public static bool SocketConnectTo(string ip, string port, out string err)
        {
            err = "";
            try
            {
                Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                IPEndPoint p = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
                sck.Connect(p);
                sck.Disconnect(false);
                sck.Close();
                return true;
            }
            catch (Exception ex)
            {
                err = string.Format("连接{0}:{1}失败,失败原因:", ip, port) + ex.Message;
                return false;
            }
        }
开发者ID:howbigbobo,项目名称:DailyCode,代码行数:26,代码来源:Network.cs


注:本文中的System.Net.Sockets.Socket.Disconnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。