//Assign the any IP of the machine and listen on port number 1000
                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport);                serverSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Dgram, ProtocolType.Udp);其中LOCALPORT是10000
我查过无冲突端口之后
clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), clientSocket);然后接受方接到包 然后查端口 发现每次clientSocket的发送端口都在变 怎么不是一直都是10000?
我的接收方是用MFC写的 GETPEER你懂的,就算我写的有错  我用第三方软件也是显示 clientSocket的发送端口一直变 他不是一开始就是绑定了10000了么,那么怎么做才让clientSocket的发送端口不随机,
因为我下位机接收到端口按原包返回就OK了 这样最简洁最方便

解决方案 »

  1.   

    怎么回事啊 CSDN的人呢 那些MVP呢
      

  2.   


    udp 的客户端如果指定本地端口,是不是需要bind下呢, tcp 的应该bind 下的貌似.
      

  3.   

    完整代码 我是绑定的···
    //Assign the any IP of the machine and listen on port number 1000
                    IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport);                serverSocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Dgram, ProtocolType.Udp);                                //Bind this address to the server
                    serverSocket.Bind(ipEndPoint);                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                    //The epSender identifies the incoming clients
                    EndPoint epSender = (EndPoint)ipeSender;                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
                        SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), null);
      

  4.   

    貌似有点矛盾啊.你的描述里面服务端的端口绑定了1000这个端口,你绑定了这个没问题,可是你的客户端的端口呢,跟这个没关系的啊.也没看到你客户端的代码,也没有说明你客户端的代码绑定了某某端口啊.所以你查peer的端口自然系统自动分配成可用的任意端口了啊.没看到你对此进行处理啊,为啥你还去进行检查呢.有点奇怪.
      

  5.   


    客户端没问题 我用第三方测试过 可以原包返回 再有 这个是UDP,不分服务客户端
      

  6.   

    需要绑定的,你没有绑定,仅仅只是初始化了一个endpoint而已,和绑定是两回事。
      

  7.   


    clientSocketserverSocket没研究过UDP的,但是我看到你的代码里面有2个Socket, 你的serverSocket确实后来说明是bind了, 但是看你的需求你是想"让clientSocket的发送端口不随机", 所以你serverSocket怎么bind也决定不了clientSocket的端口不随机吧.或者 贴更多的代码来看看.
      

  8.   

    这个 serversocket bind不bind应该没关系啊 
    我的client和第三方都能发包给他 
    我现在问题是client的包发出去后 接收方无论是第三方还是我的程序发现他发的每个包的端口都在变 但是我的client又是bind了的 这种情况在MFC里面就没碰过 另外MFC的UDP是不容许BIND的。是不是c#的socket 发包规则就是这样 bind只是监听那个端口 但是发送端口是随机的?
      

  9.   


    是你的client没bind,然后你又想client是固定的端口吧.哪里也没看到你的client 的bind代码啊.如果udp不允许bind,那你还干嘛非要客户端是固定的端口呢.
      

  10.   

    五楼这里的 也只是说明你的服务器端的Socket 进行了绑定啊 serverSocket
      //Bind this address to the server
      serverSocket.Bind(ipEndPoint);可是你的clientSocket呢?你的代码里面有个clientSocket, 我指的是那个socket你没绑定的代码啊.
    麻烦仔细看看给你的回复好吧.
      

  11.   

    Socket serverSocket;
            byte[] byteData = new byte[1024];
            public SocketClass() { 
            }               /// <summary>
            /// 端口监听
            /// </summary>
            /// <param name="localport">本机端口</param>
            public void Listen(int localport) {
                try
                {
                    //Assign the any IP of the machine and listen on port number 1000
                    IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport);                serverSocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Dgram, ProtocolType.Udp);                                //Bind this address to the server
                    serverSocket.Bind(ipEndPoint);                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                    //The epSender identifies the incoming clients
                    EndPoint epSender = (EndPoint)ipeSender;                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
                        SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), null);            }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }            
            }        public delegate void Receive(byte[] byteData, int length);
            public event Receive receive;
            private void OnReceive(IAsyncResult ar)
            {
                try
                {
                    IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint epSender = (EndPoint)ipeSender;
                    
                    int length = serverSocket.EndReceiveFrom(ar, ref epSender);
                                    receive(byteData, length);                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                                new AsyncCallback(OnReceive), epSender);            }
                catch (Exception )
                {
                    
                }              }        /// <summary>
            /// 发送
            /// </summary>
            /// <param name="clientip">下位机IP</param>
            /// <param name="clientport">下位机端口</param>
            /// <param name="message"></param>
            public void SendTo(string clientip, int clientport, byte[] byteData)
            {            try
                {
                    Socket clientSocket; //The main client socket
                    IPEndPoint epServer;   //The EndPoint of the server
                    IPAddress ipAddress = IPAddress.Parse(clientip);                clientSocket = new Socket(AddressFamily.InterNetwork,
                            SocketType.Dgram, ProtocolType.Udp);                epServer = new IPEndPoint(ipAddress, clientport);                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), clientSocket);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString() );
                }      
            }        private void OnSend(IAsyncResult ar)
            {        }
      

  12.   

    我明白了 在发送的地方 建立了 一个随意的clientsocket 并不绑定端口 所以发送出去是随意的端口OK I KNOW IT NOW!
      

  13.   


    在你的SendTo的函数里面.你的clientSocket没bind啊.你给我贴了代码想表达啥意思呢.你自己的客户端与服务器端.你的服务器端确实bind了一个端口了.你现在的需求是想在服务器端能看到客户端的端口是固定的.可是你的客户端没看到有bind.我现在迷惑了.你到底想要什么.为什么你想要,你又不bind. 为什么你想要,你又不bind. 为什么你想要,你又不bind.如果按照你之前的另外一种说法,说客户端不能bind,那么你干嘛还强求.(因为我没搞过udp,没验证你这个说法)反正 我迷糊了.
      

  14.   


    我是把clientsocket多余的放在里面了 上面的长代码是在一个类里面的 都是上位机的程序···
    我把clientsocket改成我已经绑定的那个SOCKET就可以了
      

  15.   


    不过他的socket也可以这样用 可以随意变端口发送 下次试试用在别的地方 用双socket我后面给了你12分