有一个所有的插座来到电脑 [英] got a all the socket's come to the pc

查看:70
本文介绍了有一个所有的插座来到电脑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void tcp_fun()

{

File_Location =D:\\project\\test\\tcp.txt;

TcpListener server = null;

try

{

//在端口4555上设置TcpListener。

********************************************* *********************************

********* *************************************

***** *****************************************

* *********************************************



i不会使端口= 80

和IPAddress = 255.255.255.255

来获取所有的http套接字

这样做是对的吗?

和int我得到的例外

---------------- -----------------------





SocketException:{ 0} System.Net.Sockets.SocketException(0x80004005):需求ested地址在其上下文中无效

在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)

在System.Net.Sockets.Socket。绑定(EndPoint localEP)

在System.Net.Sockets.TcpListener.Start(Int32 backlog)

在System.Net.Sockets.TcpListener.Start()

at WindowsFormsApplication1.app.tcp_fun()在D:\ project@ \\ WindowsFormsApplication1 \ WindowsFormsApplication1 \Form1.cs:第60行



< br $> b $ b

----------------------------------- ---

*************************************** *******

*********************************** ***********

******************************* ***************

Int32 port = 4545;

IPAddress localAddr = IPAddress.Parse(127.0.0.255);



// TcpListener server = ne w TcpListener(port);

server = new TcpListener(localAddr,port);

****************** ****************************

************** ********************************

********** ************************************

****** ************************************************** **********************

//开始侦听客户请求。

server.Start( );



//读取数据的缓冲区

Byte [] bytes = new Byte [256];

String data = null;



//输入监听循环。

while(true)

{

using(System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location,true) )

{

file.WriteLine(等待连接...);

}

//执行阻止调用以接受请求。

//你也可以在这里用户server.AcceptSocket()。

TcpClient client = server.AcceptTcpClient();

//控制台。 WriteLine(已连接!);



使用(System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location,true))

{

file.WriteLine(等待连接...);

}

data = null; < br $>


//获取用于读写的流对象

NetworkStream stream = client.GetStream();



int i;



//循环接收客户端发送的所有数据。

while((i = stream.Read(bytes,0,bytes.Length))!= 0)

{

//翻译数据字节为ASCII字符串。

data = System.Text.Encoding.ASCII.GetString(bytes,0,i);



// Console.WriteLine(Received:{0},data);



using(System.IO.StreamWriter file = new System.IO.StreamWriter(@ File_Location,true))

{

file.WriteLine(收到:{0}+数据);

}

//处理客户端发送的数据。

data = data.ToUpper();



byte [] msg = System.Text.Encoding.ASCII.GetBytes(data);



//发回回复。

stream.Write(msg,0,msg.Length);

// Console.WriteLine(发送:{0},数据);



using(System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location,true))

{

file.WriteLine(已发送: {0}+数据;



}

}



//关闭和结束连接

client.Close();

}

}

catch(SocketException e )

{

// Console.WriteLine(SocketException:{0},e);







使用(System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location,true))

{

file.WriteLine(S ocketException:{0}+ e);



}

}

终于

{

//停止收听新客户。

// server.Stop();

}





// Console.WriteLine(\\\
Hit enter to continue ...);

//控制台。阅读();

}

private void tcp_fun()
{
File_Location = "D:\\project\\test\\tcp.txt";
TcpListener server = null;
try
{
// Set the TcpListener on port 4555.
******************************************************************************
**********************************************
**********************************************
**********************************************

i wont to make the port =80
and the IPAddress =255.255.255.255
to get all the http sockets
is it right to do so?
and int the exception i got
---------------------------------------


SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at WindowsFormsApplication1.app.tcp_fun() in D:\project\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 60



--------------------------------------
**********************************************
**********************************************
**********************************************
Int32 port = 4545;
IPAddress localAddr = IPAddress.Parse("127.0.0.255");

// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
**********************************************
**********************************************
**********************************************
******************************************************************************
// Start listening for client requests.
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.
while (true)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location, true))
{
file.WriteLine("Waiting for a connection... ");
}
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
//Console.WriteLine("Connected!");

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location, true))
{
file.WriteLine("Waiting for a connection... ");
}
data = null;

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();

int i;

// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

// Console.WriteLine("Received: {0}", data);

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location, true))
{
file.WriteLine("Received: {0}" + data);
}
// Process the data sent by the client.
data = data.ToUpper();

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response.
stream.Write(msg, 0, msg.Length);
// Console.WriteLine("Sent: {0}", data);

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location, true))
{
file.WriteLine("Sent: {0}" + data);

}
}

// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
// Console.WriteLine("SocketException: {0}", e);



using (System.IO.StreamWriter file = new System.IO.StreamWriter(@File_Location, true))
{
file.WriteLine("SocketException: {0}" + e);

}
}
finally
{
// Stop listening for new clients.
// server.Stop();
}


// Console.WriteLine("\nHit enter to continue...");
// Console.Read();
}

推荐答案

您要求的是保留的无效地址。子网的广播地址为255,但仅限于最后一个八位字节。含义192.168.0.0/24的广播地址通常为192.168.0.255。但这仅适用于默认配置和按惯例。它可以被网络配置覆盖,但是如果静态定义或者由DHCP服务器传递,本地机器应该知道它。 (所有这些都是IPv4或课程...... IPv6完全不同。)我之前没有检查过在.net中获取广播地址,但它可能暴露在网络命名空间的某处。



但是,关于获取所有端口的问题让我不确定你是否掌握了端口的概念。绑定到广播地址上的端口80只会使您在广播IP上向端口80广播消息。它没有神奇地启用某种混杂模式,它将为您提供发送到广播地址上任何端口的所有数据包。实际上,我不确定你是否可以,在第7层(应用程序级别)实际上将数据包发送到其他端口,而不是你的应用程序必然...那种方式会破坏端口的目的。



我猜你想创建一个生产者/消费者设置,其中一台(或多台)机器广播消息,一个或多个听众选择那些。您可以通过绑定到正确的IP地址并选择要使用的端口来实现,但您应该选择10000以上的端口并远离众所周知的端口。



如果你在.Net中查找多播,你可能会找到一些有用的信息。它允许客户端查找服务器然后订阅数据流。
What you are asking for is a reserved, invalid address. The broadcast address for a subnet is 255 but only in the last octet. Meaning the broadcast address for 192.168.0.0/24 is usually 192.168.0.255. But that is only for default configurations and by convention. It can be overridden by the network configuration but the local machine should know about it if either statically defined or it is handed down by a DHCP server. (All of this is IPv4 or course... IPv6 is TOTALLY different.) I haven't checked to get the broadcast address in .net before but it is probably exposed somewhere in the network namespace.

However, your question about getting all the ports make me not sure you grasp the concept of ports. Binding to port 80 on the broadcast address will only get you messages broadcast to port 80 on the broadcast IP. It doesn't magically enable some sort of promiscuous mode that will give you all of the packets sent to any port on the broadcast address. Indeed, I'm not sure you can, at layer 7 (the app level) actually get packets sent to other ports than your application has bound to... that sort of defeats the purpose of ports.

I'm guessing you want to create a producer/consumer setup where one (or more) machines broadcast messages and one or more listeners pick those up. You can do that by binding to the proper IP address and selecting a port to use but you should probably pick a port above 10000 and stay away from the well known ports.

You could probably find some useful information if you look up multicasting in .Net. It allows clients to find servers then subscribe to a data stream.


这篇关于有一个所有的插座来到电脑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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