Win32 Service socket erroe
무겸 박
0
Reputation points
Hello I'm trying to develop C# windows service. My code works properly in IDE(visual studio console application) but when I build my code with windows service, port opens properly but interactioin failed. So I tried to switch dot net version, reinstalled service, changed timeout, but always failed. I need to send PC info to android application.
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System;
using System.IO;
using System.Runtime.Remoting.Messaging;
namespace Perch_Service
{
public partial class StartService : ServiceBase
{
EventLog eventLog1;
public StartService()
{
InitializeComponent();
eventLog1 = new System.Diagnostics.EventLog();
this.AutoLog = true;
// create an event source, specifying the name of a log that
// does not currently exist to create a new, custom log
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyLog");
}
// configure the event log instance to use this source name
eventLog1.Source = "MySource";
eventLog1.Log = "MyLog";
}
protected override void OnStart(string[] args)
{
Thread pcRegister = new Thread(PcRegister);
pcRegister.Start();
}
protected override void OnStop()
{
eventLog1.WriteEntry("종료");
}
public void PcRegister()
{
try
{
int intPort = 50520;
TcpListener listener = new TcpListener(IPAddress.Any, intPort);
listener.Start();
eventLog1.WriteEntry("포트 열림");
byte[] buffer = new byte[1024];
TcpClient tc = listener.AcceptTcpClient();
NetworkStream ns = tc.GetStream();
StreamWriter sw = new StreamWriter(ns);
eventLog1.WriteEntry("스트림 만들기");
string HostInfo = (GetPcName() + "// AND //" + GetMacAddr());
sw.WriteLine(HostInfo);
sw.Flush();
eventLog1.WriteEntry("데이터 전송됨");
}
catch (Exception e)
{
eventLog1.WriteEntry("에러발생");
eventLog1.WriteEntry(e.ToString());
}
}
public static String GetPcName()
{
string hostName = System.Net.Dns.GetHostName();
return hostName;
}
public static String GetMacAddr()
{
string MacAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();
return MacAddr;
}
}
}
Error Code in android application
java.net.SocketTimeoutException: failed to connect to /(i deleted my ip) (port 50520( from / 192.168.0.2(port 33218) after 500ms
Developer technologies .NET Other
4,103 questions
Developer technologies C#
11,570 questions
Sign in to answer