Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There,

I am writing an service that should be reply some information via TCP.
After starting the service a second PC try to connect with the service.

In the OnStart of the service I create an instance of my comunication class with:

string urlService = "net.tcp://";

host = new ServiceHost(typeof(ServicePCBI));
host.Opening += new EventHandler(host_Opening);
host.Opened += new EventHandler(host_Opened);
host.Closing += new EventHandler(host_Closing);
host.Closed += new EventHandler(host_Closed);

NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);

host.AddServiceEndpoint(typeof(IServiceTest), tcpBinding, urlService);


This part of code runs and listen to the port, I write an logfile to check all events and it looks like everything is ok.
I have checked the IP of the service (first I had an wrong IP, but was no big change to correct this).

Here one of the tests, that not work
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPAddress addr = IPAddress.Parse("");
IPEndPoint endpoint = new IPEndPoint(addr, 1234);
AddToLogFile("start listen! ");
while (true)
     Socket client = sock.Accept();
     AddToLogFile("Now ok! " + client.LocalEndPoint.ToString());

Has anyone an idea why I can't connect with the socket in the service?

1 solution

Ok it was just the firewall, after turn off everything is working.
Here is one good example for TCP connection:
A TCP/IP Server written in C#[^]

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900