新手插座相关问题 [英] Newbie Socket Related Question

查看:70
本文介绍了新手插座相关问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到套接字以测试服务是否不再侦听。我不需要检查别人抓住插座。我试图检查 拒绝连接异常 - WSAECONNREFUSED 10061,要是我的比赛,我 想
开始,或NBSP;&重启NBSP;一个特定的服务。这里是我到目前为止(任何指针将不胜因为我觉得我在我的水新手头赞赏):



使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;


命名空间ConsoleApplication1

{

   课程计划

    {

        static void Main(string [] args)

        {


            System.Net.IPAddress ip地址= System.Net.Dns.GetHostEntry(QUOT;本地主机")。AddressList中[0];

            试试
            {

                System.Net.Sockets.TcpListener的TCPListener =新System.Net.Sockets.TcpListener(ip地址,5060);

         &NBSP ;      tcpListener.Start();

            }
            catch(System.Net.Sockets.SocketException ex)

            {

         

            }


        }
    }

}

解决方案

您需要使用  TcpClient的类,以检查是否有任何人正在收听的特定端口。 


 

课程
    {
        static void Main(string [] args)
        {

           试试
            {
                Int32端口= 13000;
                TcpClient client = new TcpClient(" 127.0.0.1",port);
                client.Close();
            }
            catch(ArgumentNullException e)
            {
                Console.WriteLine(" ArgumentNullException:{0}",e);
            }
            catch(SocketException e)
            {
                Console.WriteLine(" SocketException:{0}",e);
            }

            Console.WriteLine(" \ n按Enter键继续......");
            Console.Read();

        }
    }


SocketException.ErrorCode属性将告诉你,如果连接被拒绝。


http://msdn.microsoft.com/en-us/library/system.net.sockets.socketexception.errorcode















I am trying to connect to a socket to test if a service is no longer listening. I don't need to check for someone else grabbing the socket. I am trying to check for a connection refused exception - WSAECONNREFUSED 10061. If I get a match, I want to start, or restart  a specific service. Here's what I have so far (any pointers would be greatly appreciated as I think I am over my novice head in water):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            System.Net.IPAddress ipAddress = System.Net.Dns.GetHostEntry("localhost").AddressList[0];
            try
            {
                System.Net.Sockets.TcpListener tcpListener = new System.Net.Sockets.TcpListener(ipAddress, 5060);
                tcpListener.Start();
            }
            catch (System.Net.Sockets.SocketException ex)
            {
         
            }

        }
    }
}

解决方案

You need to use TcpClient class to check if anybody is listening a particular port. 

 

class Program
    {
        static void Main(string[] args)
        {

            try
            {
                Int32 port = 13000;
                TcpClient client = new TcpClient("127.0.0.1", port);
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }

            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();

        }
    }

SocketException.ErrorCode property will tell you if connection is refused.

http://msdn.microsoft.com/en-us/library/system.net.sockets.socketexception.errorcode







这篇关于新手插座相关问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆