Reputation: 11
I am developing a .NET service that acts as a server listening on port 3000. The service is implemented and running correctly in a local environment. However, I am facing difficulties getting the time clock device on my local network to communicate with the server on port 3000.
The configuration on the time clock device is straightforward and done through a panel. In this panel, I provide the host IP, the communication port, the communication mode, and the operation mode of the time clock (client or server). In my case, I have configured the time clock to operate as a client, so it periodically attempts to communicate with the host on the configured port.
When I run the service locally, everything works perfectly. However, when deploying it to a production environment or a network setup, the communication fails. I have already opened the necessary firewall rules for both inbound and outbound traffic on port 3000, but I still cannot get it to work.
I would appreciate any assistance in identifying what might be missing or any additional configurations I should check. Thank you in advance!
In the code, I use a TcpListener configured to listen for connections on port 3000. I also implemented a log file to record communication events. When testing locally using PowerShell with the command:
Test-NetConnection -ComputerName 191...175 -Port 3000
or using Telnet, the communication works as expected, and the log records the events correctly.
Here block the code
For creating the program:
1st step: Open Visual Studio
2nd step: Select template Worker Service
3rd step: replace the Worker.cs class code with:
using System.Net.Sockets;
using System.Net;
namespace service
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
TcpListener listener = new(IPAddress.Any, 3000);
listener.Start();
string path = @"C:\LogsService";
string filePath = Path.Combine(path, "log.txt");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (var write = new StreamWriter(fileStream))
{
await write.WriteLineAsync($"Worker running");
}
}
while (!stoppingToken.IsCancellationRequested)
{
var client = await listener.AcceptTcpClientAsync();
try
{
using (var fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using(var write = new StreamWriter(fileStream))
{
await write.WriteLineAsync($"Worker running at:{client.Client.RemoteEndPoint?.ToString()}");
}
}
}
catch (Exception)
{
throw;
}
}
try
{
using (var fileStream = new FileStream(Path.Combine(path, "exit.txt"), FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (var write = new StreamWriter(fileStream))
{
await write.WriteLineAsync($"Exit: {stoppingToken.IsCancellationRequested.ToString()}");
}
}
}
catch (Exception)
{
throw;
}
}
}
}
Here settings time clock device
Upvotes: 0
Views: 33