如何用vb.net监听端口25(how to listen on port 25 with vb.net)

编程入门 行业动态 更新时间:2024-10-26 05:20:23
如何用vb.net监听端口25(how to listen on port 25 with vb.net)

我基本上是在为邮件服务器上的传入电子邮件编写垃圾邮件过滤器。 我想编写一个VB.NET程序,它可以侦听端口25上的任何传入邮件,然后在其上运行我的脚本,然后将其传递给在不同端口上运行的邮件服务器。

我需要做什么才能让我的程序只是等待消息进入端口25然后对它作出反应?

谢谢。

I'm basically trying to write a spam filter for incoming email on a mail server. I'd like to write a VB.NET program that can listen for any incoming mail on port 25 and then run my script on it and then pass it to the mail server running on a different port.

What do I need to do to have my program just sit and wait for a message to come in on port 25 and then react to it?

Thanks.

最满意答案

这里,作为一个例子,我是一段时间以前在VB.NET中修改过的套接字监听服务的一部分。 基本上,套接字在服务启动时侦听端口25上的流量,接受连接,然后将该连接分配给新线程,发送响应,然后关闭TCP连接。

Dim serverSocket As New TcpListener(IPAddress.Any, "25") Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList(0) Dim ipLocalEndPoint As New System.Net.IPEndPoint(IPAddress, 25) Protected Overrides Sub OnStart(ByVal args() As String) Dim listenThread As New Thread(New ThreadStart(AddressOf ListenForClients)) listenThread.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. End Sub Private Sub ListenForClients() serverSocket = New TcpListener(ipLocalEndPoint) serverSocket.Start() While True Dim client As TcpClient = Me.serverSocket.AcceptTcpClient Dim clientThread As New Thread(New ParameterizedThreadStart(AddressOf HandleClientComm)) clientThread.Start(client) End While End Sub Private Sub HandleClientComm(ByVal client As Object) Dim tcpClient As TcpClient = DirectCast(client, TcpClient) Dim clientStream As NetworkStream = tcpClient.GetStream Dim message As Byte() = New Byte(4095) {} Dim bytesRead As Integer While True If (bytesRead = 0) Then Exit While End If Dim encoder As New asciiencoding() Dim serverResponse As String = "Response to send" 'Response to send back to the testing client Dim sendBytes As [Byte]() = encoding.ascii.getbytes(serverResponse) clientStream.Write(sendBytes, 0, sendBytes.Length) End While tcpClient.Close() End Sub

Here, as an example, is part of a socket listening service I modified from a tutorial a while ago in VB.NET. Basically, a socket listens for traffic on port 25 when the service is started, accepts a connection and then assigns that connection to a new thread, sends a response, and then closes the TCP connection.

Dim serverSocket As New TcpListener(IPAddress.Any, "25") Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList(0) Dim ipLocalEndPoint As New System.Net.IPEndPoint(IPAddress, 25) Protected Overrides Sub OnStart(ByVal args() As String) Dim listenThread As New Thread(New ThreadStart(AddressOf ListenForClients)) listenThread.Start() End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. End Sub Private Sub ListenForClients() serverSocket = New TcpListener(ipLocalEndPoint) serverSocket.Start() While True Dim client As TcpClient = Me.serverSocket.AcceptTcpClient Dim clientThread As New Thread(New ParameterizedThreadStart(AddressOf HandleClientComm)) clientThread.Start(client) End While End Sub Private Sub HandleClientComm(ByVal client As Object) Dim tcpClient As TcpClient = DirectCast(client, TcpClient) Dim clientStream As NetworkStream = tcpClient.GetStream Dim message As Byte() = New Byte(4095) {} Dim bytesRead As Integer While True If (bytesRead = 0) Then Exit While End If Dim encoder As New asciiencoding() Dim serverResponse As String = "Response to send" 'Response to send back to the testing client Dim sendBytes As [Byte]() = encoding.ascii.getbytes(serverResponse) clientStream.Write(sendBytes, 0, sendBytes.Length) End While tcpClient.Close() End Sub

更多推荐

本文发布于:2023-08-06 12:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何用   端口   vb   port   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!