• 检验端口是否被调用


     占用了端口,还得监测。(这个通信更简单)

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;

    namespace ConsoleApplication1
    {
    class UsePort
    {
    private int port;

    public int Port
    {
    get { return port; }
    set { port = value; }
    }
    string ip;

    public string Ip
    {
    get { return ip; }
    set { ip = value; }
    }

    protected void SendMessage( string message)
    {
    TcpListener tListenser
    = null;
    IPEndPoint ipPort
    = new IPEndPoint(IPAddress.Parse(ip),port);
    try
    {
    tListenser
    = new TcpListener(ipPort);
    tListenser.Start();
    while (true)
    {
    Console.WriteLine(
    "the port: " + Port.ToString() + " in " + Ip + " is used....");
    Thread.Sleep(
    1000);
    }

    }
    catch (Exception)
    {
    }
    }

    protected static void ReceiveMessage()
    {

    try
    {
    TcpClient client
    = new TcpClient("127.0.0.1", 5000);
    Console.WriteLine(
    "connection is OK");

    }
    catch(Exception)
    {
    Console.WriteLine(
    "connection is Failed");

    }

    }

    public static void Run()
    {
    UsePort up
    = new UsePort { Ip = "127.0.0.1", Port = 5000 };
    new Thread(ReceiveMessage).Start();

    up.SendMessage(
    "pppppppppp");


    }

    }
    }

    结果:

  • 相关阅读:
    5道趣味Python热身题【新手必学】
    操作系统特征
    二叉树的中序遍历
    英语一图画作文模板
    函数
    双阶乘与华里士公式
    因式分解
    【】连通图——详细解释
    【】this指针——c++中的特殊指针
    咱们程序员好用的云笔记
  • 原文地址:https://www.cnblogs.com/jimson/p/socket1.html
Copyright © 2020-2023  润新知