简体   繁体   English

如何为客户端应用程序修复端口

[英]how to fix port for client application

I am developing console application in visual studio(c#). 我正在Visual Studio(C#)中开发控制台应用程序。 I need to continuously listen from server's defined IP & port. 我需要不断地从服务器定义的IP和端口监听。 On listening to from that IP & port I will get data of call logs from epabx . 从该IP和端口进行侦听时,我将从epabx获取呼叫日志的数据。 Problem is I am developing client socket will communicate through any free port selected by OS. 问题是我正在开发客户端套接字,可以通过操作系统选择的任何空闲端口进行通信。 PBX only allows to communicate through pre-configured port number. PBX仅允许通过预配置的端口号进行通信。 I don't find any answer about how to configure port for client in advance so that application communicates from that port only. 我没有找到有关如何预先为客户端配置端口的任何答案,以便应用程序仅从该端口进行通信。 Any help will be good. 任何帮助都会很好。

Many showed to change port for web application but i want to configure port for console application. 许多人表示要更改Web应用程序的端口,但我想为控制台应用程序配置端口。 Here is my code sample if it helps in any way: 这是我的代码示例,如果有任何帮助:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ClientSocket
{
    public class Program
    {
        static void Main(string[] args)
        {
            TcpClient clientSocket = new TcpClient();
            clientSocket.Connect(destinationPort, destinationPort);
            //((System.Net.IPEndPoint)(clientSocket.Client.LocalEndPoint)).Port = 3389; I want to fix my port in advance something like this or any other way
            Console.WriteLine(" >> Client Started");

            while (true)
            {
                try
                {
                    NetworkStream serverStream = clientSocket.GetStream();
                    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "$");
                    Console.WriteLine("\nPing to Server:");
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();

                    byte[] inStream = new byte[10025];
                    serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
                    string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                    Console.WriteLine("Reply from server:" + returndata);
                    Thread.Sleep(5000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }
}

Whatever you commented line will be enough to set local end point port number. 无论您用什么注释行,都足以设置本地端点端口号。

IPEndPoint ipLocalEndPoint = new IPEndPoint(localIPAddress, clientPort);
TcpClient clientSocket = new TcpClient(ipLocalEndPoint);
clientSocket.Connect(serverIP, serverPort);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试客户端和服务器套接字应用程序时如何指定有效端口? - How do I specify a valid port when testing a client and server socket application? 如何从ASP.Net Web应用程序读取/写入客户端计算机的串行端口 - How can read/write serial port of client machine from ASP.Net Web application 如何在应用程序中修复分辨率? - how to fix resolution in application? 如何将.NET应用程序移植到mono - how to port .NET application to mono 如何在MVC应用程序中修复OptimisticConcurrencyException - How to fix OptimisticConcurrencyException in an MVC application 如何修复服务器错误'/'应用程序? - How to fix Server Error '/' Application? 安装在客户端计算机上时,如何修复引发异常的应用程序(信息:System.IO.DirectoryNotFoundException堆栈) - How do I fix an application that is throwing Exception (Info: System.IO.DirectoryNotFoundException Stack) when installed in a client machine 如何测试并行端口C#应用程序? - How to test a parallel port C# application? 如何修复“提供程序与Oracle客户端版本不兼容”? - How to fix “The provider is not compatible with the version of Oracle client”? MessageSecurityException,如何解决不同的客户端和服务器时间? - MessageSecurityException, how to fix different client and server time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM