C#-UDP连接在本地但不全局 [英] C# -- UDP connection works on local but not global

查看:71
本文介绍了C#-UDP连接在本地但不全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我编写了一个程序,该程序提供类似于mIRC的界面/文本聊天(使用tcp套接字)以及音频聊天(使用UDP套接字).

该程序是典型的客户端-服务器设置.

事实是,TCP数据包在我的本地网络中以及从全球网络中的客户端发送和接收都很好.

但是当涉及UDP连接时,只有本地数据包传输有效O_O


它大约有1000行,因此我将在相关的UDP套接字摘要中进行复制:)


Hello,

I''ve written a program that provides mIRC-like interface/textchat(using tcp socket) and also with audio-chat(using a UDP socket).

The program is the typical client-server setup.

The thing is, TCP packets are sent and received just fine both in my local network and from clients in the global network.

But when it comes to the UDP connection, only the local packet transfer works O_O


Its about 1000 lines, so I''ll copy in the relevant UDP socket snippets :)


this.audioSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);





IPEndPoint clientEP = new IPEndPoint(IPAddress.Any,this.clientPort); 
this.audioSocket.Bind(clientEP);  
this.serverEP = new IPEndPoint(IPAddress.Parse(this.textbox_connect.Text),serverPort); // the IP is correct 
                             


this.audioSocket.BeginConnect(this.serverEP, new AsyncCallback(audioConnectCallback), this.audioSocket);
                                connDone.WaitOne();
                                this.textbox_display.Text = ("sending sh*t to: " + serverEP.ToString());
StateObject state_audio = new StateObject();
                                state_audio.setWorkSocket(this.audioSocket);
                                state_audio.getWorkSocket().BeginReceive(state_audio.getBuffer(), 0, state_audio.getBufferSize(), 0, new AsyncCallback(audioReceive),state_audio);









private void audioReceive(IAsyncResult asyn)
        {
           
            int readBytes = 0;
            try
            {
                StateObject state = (StateObject)asyn.AsyncState;
                readBytes = state.getWorkSocket().EndReceive(asyn);
                this.waveProvider.AddSamples(state.getBuffer(), 0, readBytes);
               
                this.audioSocket.BeginReceive(state.getBuffer(), 0, state.getBufferSize(), SocketFlags.None, new AsyncCallback(audioReceive), state);
            }
            catch (Exception ez)
            {
                MessageBox.Show("audioReceive: " + this.textbox_nick.Text + "        " +ez.ToString());
            }
            
        }






不知道还有什么要说的,或者这段代码说不出什么:(我是否有明显的遗漏之处,这会使它在本地网络上工作,但不在外部网络上工作?

它真的很奇怪,因为在运行时没有异常或任何东西抛出(在任何一侧),没有东西只是从外部客户端接收/发送到外部的.


如果有任何差距,请告诉我!

干杯






Not sure what else to say, or if this code says anything at all :( is there anything obvious that I am missing, that would make this work on a local network but not outside it?

Its really wierd because there is no exceptions or anything thrown(on any side) when running, nothing just gets received/sent from/to the client outside.


If there is any gaps just let me know!

Cheers

推荐答案

也许您尝试连接的全局地址位于路由器/防火墙后面?
Perhaps the global address your trying to connect to is behind a router/firewall?


可能是防火墙.暂时将您的应用更改为在两端使用端口53. UDP 53通常用于DNS查询,并且可以通过大多数防火墙. gl
Like the others said, probably a firewall. Temporarily change your app to use port 53 on both ends. UDP 53 is normally used for DNS queries and allowed through most firewalls. gl


是的,可能是路由器防火墙出现的问题


有时也可能是防病毒块..可能是antivius可能会阻止您正在使用的UPD端口..
Yes.., may be the problem with your router firewall
OR

Some times may be the Antivirus blocks also.., may be the antivius may blocks the UPD port which is you are using..


这篇关于C#-UDP连接在本地但不全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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