| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1245 人关注过本帖
标题:为什么在PPC下发送会失败,在控制台下发送却正常???
取消只看楼主 加入收藏
gundumw100
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2008-1-22
收藏
 问题点数:0 回复次数:1 
为什么在PPC下发送会失败,在控制台下发送却正常???
写了PPC的客户端代码,每次发送的时候都说:目标计算机拒绝.
后来在控制台下写了一端测试代码,却可以发送,这是什么原因???
服务器端代码:
class Program
    {
        static void Main(string[] args)
        {            
            IPAddress IP = IPAddress.Parse("127.0.0.1");//socket://220.248.36.5
            int port = 6000;
            TcpListener server = new TcpListener(IP, port);
            server.Start();
            Console.WriteLine("等待客户机连接");
            Socket socket = server.AcceptSocket();
            Console.WriteLine("服务器接受连接");
            while (true)
            {
                try
                {
                     byte[] buff = new byte[1024];
                    int length = socket.Receive(buff);
                    Console.WriteLine("接受到数据!");
                    string data = Encoding.ASCII.GetString(buff, 0, length);
                    Console.WriteLine(data);
                }
                catch (Exception e)
                {
                    Console.WriteLine("客户机离开");
                    socket.Close();
                    break;
                }
            }
            Console.ReadLine();
        }
    }
控制台客户端代码:
class Program
    {
        static void Main(string[] args)
        {
            NetProcess net = new NetProcess();
            net.WriteData("abcdefg");
              
            Console.ReadLine();
        }
    }
class NetProcess
    {
        TcpClient client;
        NetworkStream stream;
        string IP = "127.0.0.1";//socket://220.248.36.5
        int port = 6000;
        bool isFail = false;
        string text;
        public NetProcess()
        {
            Connect();           
        }
        public void Connect()
        {
            client = new TcpClient();
            try
            {
                client.Connect(IP, port);
                text = "连接服务器成功!";

            }
            catch (Exception e)
            {
                isFail = true;
                text = "连接失败!" + e.ToString();
            }
        }
        public void WriteData(string data)
        {
            Byte[] mybytes = Encoding.ASCII.GetBytes(data);
            stream = client.GetStream();
            stream.Write(mybytes, 0, mybytes.Length);

        }
        public string ReadData()
        {
            int bufferSize = client.ReceiveBufferSize;
            byte[] mybytes = new byte[bufferSize];
            stream.Read(mybytes, 0, bufferSize);//bufferSize=mybytes.Length
            string data = Encoding.ASCII.GetString(mybytes, 0, bufferSize);
            return data;
        }
    }

PPC客户端代码片段:

//按下确认键发送消息
case ST_INFO_VALIDATE://还款信息确认                           
                          
    NetProcess net = new NetProcess();  //打开连接,这里为什么会失败?????                                               
                                                               
    net.WriteData("1234567890");      //把信用卡卡号发送出去
                              
break;

[[it] 本帖最后由 gundumw100 于 2008-2-3 09:34 编辑 [/it]]
搜索更多相关主题的帖子: PPC 计算机 服务器 socket 
2008-02-02 14:32
gundumw100
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2008-1-22
收藏
得分:0 
没人知道吗????郁闷阿

 public void WriteData(string data)
        {
            client = new TcpClient();
            IPAddress ip = IPAddress.Parse(IP);
            client.Connect(ip, port);//这里出错,why???

                 Byte[] mybytes = Encoding.ASCII.GetBytes(data);
                stream = client.GetStream();
                stream.Write(mybytes, 0, mybytes.Length);
   
            
        }

  未处理
  Message="由于目标计算机主动拒绝,未能建立连接"
  ErrorCode=10061
  NativeErrorCode=10061
  StackTrace:
    位于 ()
    位于 ()
    位于 ()
    位于 ()
    位于 ()
    位于 ()

[[it] 本帖最后由 gundumw100 于 2008-2-13 10:54 编辑 [/it]]
2008-02-13 09:37
快速回复:为什么在PPC下发送会失败,在控制台下发送却正常???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028005 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved