我有个关于多端口监听的问题。我在CSharp上写了这段代码。我可以用这个代码发送两个端口的消息。只起一次作用。如何将此代码更改为多次工作?这里的代码块:
服务器端:
class ListenPorts
{
Socket[] scon;
IPEndPoint[] ipPoints;
internal ListenPorts(IPEndPoint[] ipPoints)
{
this.ipPoints = ipPoints;
scon = new Socket[ipPoints.Length];
}
public void beginListen()
{
for (int i = 0; i < ipPoints.Length; i++)
{
scon[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
scon[i].Bind(ipPoints[i]);
Thread thread = new Thread(threadListen);
thread.Start(scon[i]);
}
}
public void threadListen(object objs)
{
Socket scon = objs as Socket;
byte[] data = new byte[1024];
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
try
{
scon.Listen(100);
Socket newSocket = scon.Accept();
newSocket.ReceiveFrom(data, ref Remote);
// scon.ReceiveFrom(data, ref Remote);
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine(scon.LocalEndPoint.ToString() + "IP {0}: ", Remote.ToString());
}
}main方法用于调用类来侦听端口。
class Program
{
static void Main(string[] args)
{
IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
IPEndPoint ipPoint1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);
IPEndPoint[] ipPoints = new IPEndPoint[2] { ipPoint, ipPoint1 };
ListenPorts lp = new ListenPorts(ipPoints);
Console.WriteLine("Begin Listen");
lp.beginListen();
}}
客户端:
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
//TCP Client
Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
//Set the IP address of the server, and its port.
IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
IPEndPoint ipep2 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);
string welcome = "Hello! ";
try
{
Socket server1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
data = Encoding.ASCII.GetBytes(welcome);
server1.Connect(ipep1);
server1.Send(Encoding.ASCII.GetBytes(welcome));
server1.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Socket server2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
data = Encoding.ASCII.GetBytes(welcome);
server2.Connect(ipep2);
server2.Send(Encoding.ASCII.GetBytes(welcome));
server2.Close();
}}
发布于 2019-12-02 10:36:27
在侦听器代码中需要一个循环。
try
{
while (true)
{
scon.Listen(100);
Socket newSocket = scon.Accept();
var size= newSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine($"Received {size} bytes on socket {scon.LocalEndPoint}");
}
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine(scon.LocalEndPoint.ToString() + "IP {0}: ", Remote.ToString());https://stackoverflow.com/questions/39894280
复制相似问题