无法与服务沟通 [英] cant't comunicate with service

查看:73
本文介绍了无法与服务沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在编写一项服务,应该通过TCP回复一些信息。

启动服务后一秒钟PC尝试连接该服务。



在服务的OnStart中,我创建了一个我的通信类的实例:



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://192.168.10.48:1234/MyService";

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);

host.Open();





这部分代码运行并监听端口,我写的一个日志文件检查所有事件,看起来一切正常。

我检查了服务的IP(首先我有一个错误的IP,但没有大的改动来纠正这个)。 />


其中一项测试,不起作用



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("192.168.10.48");
IPEndPoint endpoint = new IPEndPoint(addr, 1234);
AddToLogFile("start listen! ");
while (true)
{
     sock.Bind(endpoint);
     sock.Listen(10);
     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?
Thanks

推荐答案

好吧它只是防火墙,关闭后一切正常。

这是一个很好的例子TCP连接:

TCP / IP服务器用C#编写 [ ^ ]



谢谢
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#[^]

Thanks


这篇关于无法与服务沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆