当前位置:编程学习 > C#/ASP.NET >>

C# 一个windows服务程序

自己写了一个C#程序   可用于两个电脑之间的通讯。有一个客户端与服务端。现在把服务端放到一个同学的电脑上前台运行着,在我的电脑上打开客户端输入同学的IP地址就能直接连接上服务端。但是我把服务端做成一个windows服务之后在安装到同学的电脑上(服务已经启动),再连接的时候却就连不上这个windows服务程序了。一直没有响应。请问这是怎么回事啊?     更奇怪的是:我把这个windows服务装到自己的电脑上,然后在自己电脑上打开客户端,却能成功连接了。这是什么原因啊?? Windows C# 通信 --------------------编程问答-------------------- 源码调试呗,光这么说鬼知道为什么 --------------------编程问答-------------------- 这涉及两台计算机请问怎么调试啊   我调试本地客户端的时候一直显示 “消息筛选器显示应用程序正在使用中。引发了一个System.Runtime.InteropServiecs.COMException:          引发了一个System.Net.Sockets.SocketException"请求的名称有效,但是找不到请求的类型数据。

下面是客户端源码:调试的时候直接输入IP42.299.59.251就行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace 客户端
{
    //客户端进程
    class 客户端
    {
        static void Main(string[] args)
        {
            string ip = string.Empty;
            Console.WriteLine("please input IP Address:");
            ip = Console.ReadLine();
            Console.Write("You can input commend [get] [kill processname] [shut down now]:\n");
            IPHostEntry ipHost = Dns.Resolve(ip);
            //IPHostEntry ipHost = Dns.Resolve("42.229.61.33");
            IPAddress ipAddress = ipHost.AddressList[0];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 2112);
            Process[] processes;
            Process process;
            processes = Process.GetProcesses();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Starting:Creating Socket object");
            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sender.Connect(ipEndPoint);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Successfully connected to {0}", sender.RemoteEndPoint);
            Console.WriteLine("Please input your commend!!");
            string sendingMessage=string.Empty;
   
            var rece = new Thread(receivethread);
            rece.Start(sender);
            while (true)
            {
                sendingMessage = null;
                Console.Write("客户端请输入:");
                sendingMessage = Console.ReadLine();//要发送的语句
                
                byte[] forwardMessage = Encoding.ASCII.GetBytes(sendingMessage);
                sender.Send(forwardMessage);//forwarMessage 中保存最终发送给服务器的信息
                //if (sendingMessage == "q")
                //    break;
               // Thread.Sleep(10000);
            }

            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
            Console.ReadLine();
            
        }
        static void receivethread(object o)
        {
            while (true)
            {

                Socket sender = (Socket)o;
                byte[] receivedBytes = new byte[1024];
                int totalBytesReceived = sender.Receive(receivedBytes);//接受从服务器发过来的消息
                string receiv = Encoding.ASCII.GetString(receivedBytes, 0, totalBytesReceived);
                if (receiv == "q")
                {
                    break;
                }
                Console.WriteLine("服务器:{0}", receiv);
            }
        }
    }
}
--------------------编程问答-------------------- 可以在服务端用Eventlog加些调试信息,逐步定位问题。 --------------------编程问答-------------------- 楼上正解,推荐加上日志文件,这样可以更清楚知道问题。 --------------------编程问答-------------------- 先确定一下,既然是通讯,就有局域网和广域网之分,同在一个局域网,或者同在广域网。访问时没有问题的,现在的通讯基本上都是基于tcp/ip协议的
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,