首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#网络通信 在XP停能正常通信,但换Win7却不能通信

2013-05-02 
C#网络通信 在XP下能正常通信,但换Win7却不能通信麻烦各位帮我看一下啊 在XP下能正常通信啊,换Win7却不行

C#网络通信 在XP下能正常通信,但换Win7却不能通信
麻烦各位帮我看一下啊 
在XP下能正常通信啊,换Win7却不行了
TcpSend函数还是正常的,但是TcpReceive不能重复接收,问题出在哪里呢?
是Thread.Sleep延时的问题吗? 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace Network {
    public class TcpClient {
        private Socket _Socket = null;  //定义Socket
        private IPEndPoint ipEndPoint = null; //定义IP端口

        public byte[] buffer = new byte[1];  //字符缓冲区

        public bool IsConnected { //判断网络是否已连接
            get {
                if (_Socket == null) {
                    return false;
                }
                return true;
            }
        }

        public TcpClient() {
            
        }

        public TcpClient(DataStructure.IPContext _ipContext) {
            InitSocket(_ipContext.Ip, _ipContext.Port);  //调用初始化Socket函数
        }

        public TcpClient(IPAddress _ip, int _port) {
            InitSocket(_ip, _port);  //调用初始化Socket函数
        }

        public void InitSocket(IPAddress _ip, int _port) {  //初始化Socket
            ipEndPoint = new IPEndPoint(_ip, _port);
            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  //TCP Socket
            _Socket.SendTimeout = 2000;  //发送时间
            _Socket.ReceiveTimeout = 3000;  //接收时间
            _Socket.ReceiveBufferSize = 1;  //缓冲区大小
        }

        /*
         * 连接初始化后的_Socket
         * 连接成功返回true
         * 连接失败返回false
         */
        public bool TcpConnect() {  


            try {
                _Socket.Connect(ipEndPoint);  //Socket连接
            } catch (SocketException se) {    //捕获Socket异常
                Console.WriteLine(" SocketException : {0}", se.ToString());
                return false;
            } catch (Exception e) { //捕获异常
                Console.WriteLine(e.ToString());
                return false;
            }

            return true;
        }

        /*
         * 发送msg字符串到_Socket连接的端点
         * 
         */
        public bool TcpSend(string msg) {
            do {
                try {
                    if (_Socket.Poll(50, SelectMode.SelectWrite)) {  //每隔50毫秒检测缓冲区是否能写   Poll函数
                        /*
                         *检测网络是否能够被写(即能否发送)
                         *正确执行Send函数
                         *不正确程序睡眠100ms,继续检测
                         *该函数陷入不会死循环
                         */
                        int bytesSent = _Socket.Send(Encoding.ASCII.GetBytes(msg)); //发送msg
                        Console.WriteLine(" TcpSend --> Send Message : {0}", msg.ToString());  //打印msg
                        if (bytesSent > 0) break;
                    }
                } catch (Exception e) {


                    Console.WriteLine(" TcpSend --> Network connection is break ,{0}", e.ToString()); //捕获异常
                    return false;
                }
                Thread.Sleep(100);  //延时0.1秒   Thread概念
            } while (true);
            return true;
        }

        public bool TcpSend(byte[] msg) {
            do {
                try {
                    if (_Socket.Poll(50, SelectMode.SelectWrite)) {  //每隔50毫秒检测缓冲区是否能写
                        /*
                         *检测网络是否能够被写(即能否发送)
                         *正确直行Send函数
                         *不正确程序睡眠100ms,继续检测
                         *该函数陷入不会死循环
                         */
                        int bytesSent = _Socket.Send(msg);  //发送msg
                        for (int i = 0; i < msg.Length; i++)
                            Console.WriteLine(" TcpSend --> Send Message : " + msg[i]);  //打印msg
                        if (bytesSent > 0) break;
                    }
                } catch (Exception e) {
                    Console.WriteLine(" TcpSend --> Network connection is break ,{0}", e.ToString()); //捕获异常
                    return false;
                }


                Thread.Sleep(100);  //延时0.1秒   Thread概念
            } while (true);
            return true;
        }

        public bool TcpSend(byte _msg) {
            byte[] msg = new byte[1];
            msg[0] = _msg;
            do {
                try {
                    if (_Socket.Poll(50, SelectMode.SelectWrite)) {  //每隔50毫秒检测缓冲区是否能写
                        /*
                         *检测网络是否能够被写(即能否发送)
                         *正确直行Send函数
                         *不正确程序睡眠100ms,继续检测
                         *该函数陷入不会死循环
                         */
                        int bytesSent = _Socket.Send(msg);
                        for (int i = 0; i < msg.Length; i++)
                            Console.WriteLine(" TcpSend --> Send Message : " + msg[i]);
                        if (bytesSent > 0) break;
                    }
                } catch (Exception e) {
                    Console.WriteLine(" TcpSend --> Network connection is break ,{0}", e.ToString());
                    return false;
                }
                Thread.Sleep(100);  //延时0.1秒   Thread概念
            } while (true);


            return true;
        }


        /*
         * 从已经初始化完成的_Socket中,获取已接收到得字符串
         */
        public bool TcpReceive() {
            int TimeOut = 0;
            do {
                Thread.Sleep(150);  //延时0.1秒   Thread概念
                try {
                    if (_Socket.Poll(50, SelectMode.SelectRead)) {  //每隔50毫秒检测缓冲区是否能读
                        /*
                         *检测网络是否能够被写(即能否发送)
                         *正确直行Send函数
                         *不正确程序睡眠100ms,继续检测
                         *该函数陷入死循环可能性极小,情况如下:
                         *在100ms内,信号机回传了
                         */
                        int bytesRecv = _Socket.Receive(buffer);
                        if (bytesRecv > 0) {
                            //retMsg = Encoding.ASCII.GetString(buffer);
                            Console.WriteLine(" TcpReceive --> Recv Message :" + (_Socket.RemoteEndPoint as IPEndPoint).Address.ToString() + " " + buffer[0] + " len : " + bytesRecv.ToString());
                            break;
                        }
                    }
                } catch (Exception e) {
                    Console.WriteLine(" TcpReceive --> Network connection is break ,{0}", e.ToString());


                    return false;
                }
                if (++TimeOut >= 19) return false; //超时设置2秒
            } while (true);
            return true;
        }

        public byte[] TcpReceive(byte[] data) {
            int TimeOut = 0;
            int bytesRecv = 0;
            do {
                Thread.Sleep(150);  //延时0.1秒   Thread概念
                try {
                    if (_Socket.Poll(50, SelectMode.SelectRead)) {  //每隔50毫秒检测缓冲区是否能读
                        /*
                         *检测网络是否能够被写(即能否发送)
                         *正确直行Send函数
                         *不正确程序睡眠100ms,继续检测
                         *该函数陷入死循环可能性极小,情况如下:
                         *在100ms内,信号机回传了
                         */
                        bytesRecv = _Socket.Receive(data);
                        if (bytesRecv > 0) {
                            //retMsg = Encoding.ASCII.GetString(buffer);
                            Console.WriteLine(" TcpReceive --> Recv Message :" + (_Socket.RemoteEndPoint as IPEndPoint).Address.ToString() + " " + buffer[0] + " len : " + bytesRecv.ToString());
                            break;
                        }


                    }
                } catch (Exception e) {
                    Console.WriteLine(" TcpReceive --> Network connection is break ,{0}", e.ToString());
                    return null;
                }
                if (++TimeOut >= 19) return null; //超时设置2秒
            } while (true);
            return data;
        }

        public bool TcpEnd() {
            try {
                _Socket.Shutdown(SocketShutdown.Both);  //关闭读写功能
                _Socket.Close();  //关闭IP地址 端口号
                _Socket = null;   //赋值为空
            } catch (Exception e) {
                Console.WriteLine(" TcpEnd --> Network connection is break ,{0}", e.ToString());
                return false;
            }
            return true;
        }
    }

}


[解决办法]
关闭防火墙再进行测试
[解决办法]
看下xp下面用到了哪些dll库都复制过来,再在win7上试试,因为有时候两边在GAC中的dll不一致
[解决办法]
win最好关闭IPv6
[解决办法]
在win7下编译
[解决办法]
防火墙,UAC都可能引起问题,先都关闭了试试
[解决办法]
可能是端口关闭了。
防火墙那里打开你使用的端口。
[解决办法]
可能是获取的IP地址的问题吧,IPV4,IPV6
[解决办法]
你调一下通信的端口 ,尽量端口在 5000以后,因为系统本身也占用了蛮多端口的
[解决办法]
也可能是系统环境的原因,可以将解决方案放到Wind7下跑跑,看下抛什么异常。

热点排行