例如,QQ默认指定4000端口进行发送数据流,如何指定这个发送端口?
如果无法指定,请问如何获取当前所使用的发送的端口?
我使用的是UdpClient
如果无法指定,请问如何获取当前所使用的发送的端口?
我使用的是UdpClient
解决方案 »
- 请教一个关于程序运行速度的问题。
- 在记事本里面打字这么难啊
- 智能客户端发布问题
- 执行Process.Start("cmd.exe")可以正常执行,但换成Process.Start("c:\\mm\\aa.exe")就没有任何反应??
- xeno 2004 和 xeno 2005哪个更好用????
- 163邮箱注册页面中的验证链接地址是怎么得到的?
- C#中!如何用SOCKET提取客户端POST过来的文件字节?
- 新手问题,来帮帮我.
- 大家一起做侃爷!来者有分!!!
- 在C#中如何使用WIN API?
- 三楼楼长的OA系统我在还原数据库备份的时候出错了.没法还原.大家帮我想想办法.
- 刚才一句"preview=true"给了10分,有点心痛哟(我结贴是不是太快?),这回5分,还是菜问题,在线等
http://www.liva.com.cn/web/articles/2004/11/27/1101543149187.shtml
比如:
自己发送端口:1111 接收端口:2222
对方发送端口:2222 接收端口:1111
这样便可以实现通信了
例子
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;namespace Pinbor.ChatApp
{
class Chat
{
private static IPAddress remoteIPAddress;
private static int remotePort;
private static int localPort; [STAThread]
static void Main(string[] args)
{
try
{
//Get neccessary data for connection
Console.WriteLine("Enter Local Port");
localPort = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter Remote Port");
remotePort = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Enter Remote IP address");
remoteIPAddress = IPAddress.Parse(Console.ReadLine()); Thread tRec = new Thread(new ThreadStart(Receiver));
tRec.Start(); while(true)
{
Send(Console.ReadLine());
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
} private static void Send(string datagram)
{
//Create UdpClint
UdpClient sender = new UdpClient(); //Create IPEndPoint with details of remote host
IPEndPoint endPoint = new IPEndPoint(remoteIPAddress,remotePort); try
{
//Convert data byte array
byte[] bytes = Encoding.ASCII.GetBytes(datagram); //Send data
sender.Send(bytes,bytes.Length,endPoint);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
//Close connection
sender.Close();
}
} public static void Receiver()
{
//Create a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient(localPort); //IPEndPoint with remote host information
IPEndPoint RemoteIpEndPoint = null; try
{
Console.WriteLine("----------******** Ready for chat ********----------"); while(true)
{
//Wait for datagram
byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); //Convert and display data
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("-" + returnData.ToString());
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
http://www.liva.com.cn/web/articles/2004/11/27/1101543149187.shtml
UdpClient udpClient = new UdpClient();
try{
udpClient.Connect("www.contoso.com", 11000);
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
udpClient.Send(sendBytes, sendBytes.Length);
UdpClient udpClientB = new UdpClient();
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000); IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes); Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString()); udpClient.Close();
udpClientB.Close();
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
对方发送端口:2222 接收端口:1111
你的代码如何能够指定发送端口
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9000);
server = new UdpClient (ipep);
server.Send(data, intLength, "127.0.0.1", 9050);serverprivate UdpClient server;private IPEndPoint ipep = null;
private IPEndPoint sender = null;ipep = new IPEndPoint(IPAddress.Any, 9050);
sender = new IPEndPoint(IPAddress.Any, 9050);server = new UdpClient(ipep);data = server.Receive(ref sender);
也就是说,两端都设置为server,然后在Send的时候指定目的IP和Port就可以了