2 u011743368 u011743368 于 2017.08.28 10:23 提问

C# socket物理断线重连,报错。

代码如下,提示错误 1 非静态的字段、方法或属性“com.tchuin.channel.IChannel.Start()”要求对象引用 C:\Users\Administrator\Desktop\FTU代码--断线重连\源代码\Maintenance\Channel\network\ChannelTcpIpClient.cs 185 13 Channel
错误。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;

namespace com.tchuin.channel.network
{
///************************************************************************
//* 原不可断线重连的Socket的类
class ChannelTcpIpClient : IChannel
{
#region IChannel 成员

    public event Action<byte[]> ReadByteArrayEvent;
    public event Action<byte[]> WroteByteArrayEvent;

    bool IChannel.IsStart
    {
        get
        {
            if (mSocket == null)
                return false;
            else
                return mSocket.Connected;
        }
    }

    void IChannel.Start()
    {
        mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint iep = new IPEndPoint(mServerIpAddress, mServerPort);
        mSocket.BeginConnect(iep, ConnectCallBack, mSocket);
    }

    void IChannel.Stop()
    {
        if (mSocket != null)
        {
            if (mSocket.Connected)
                mSocket.Shutdown(SocketShutdown.Both);
            mSocket.Close();
            mSocket = null;
        }
    }

    int IChannel.Write(byte[] buffer)
    {
        if (mSocket == null)
            return -1;

        if (mSocket.Connected)
        {
            int wroteLen = mSocket.Send(buffer);
            byte[] temp = new byte[wroteLen];
            Array.Copy(buffer, temp, temp.Length);
            if (WroteByteArrayEvent != null)
                WroteByteArrayEvent(temp);
            return wroteLen;
        }
        else
            return -1;
    }

    int IChannel.Read(byte[] buffer)
    {
        int readLen = mSocket.Receive(buffer);
        byte[] temp = new byte[readLen];
        Array.Copy(buffer, temp, temp.Length);
        if (ReadByteArrayEvent != null)
            ReadByteArrayEvent(buffer);
        return readLen;
    }


    #endregion


    public ChannelTcpIpClient(IPAddress address, int port)
    {
        mDataBuffer = new Byte[1024 * 4];
        mServerIpAddress = address;
        mServerPort = port;
    }

    private void RecieveCallBack(IAsyncResult result)
    {
        Socket client = (Socket)result.AsyncState;
        if (client == null)
            return;
        if (client.Connected)
        {
            int recvByteCount;
            try
            {
                recvByteCount = client.EndReceive(result);
                if (recvByteCount > 0)
                {
                    byte[] buffer = new byte[recvByteCount];
                    Array.Copy(mDataBuffer, buffer, buffer.Length);
                    if (socketDataArrival != null)
                    {
                        socketDataArrival(buffer);
                    }
                }
                else
                {
                    if (mSocket.Connected)
                    {
                        if (socketDisconnected != null)
                        {
                            socketDisconnected();
                            return;
                        }
                    }
                }
                //继续监听下一次的数据接收
                client.BeginReceive(mDataBuffer, 0, mDataBuffer.Length, SocketFlags.None, RecieveCallBack, client);
            }
            catch (SocketException ex)
            {
                if (socketDisconnected != null)
                {
                    socketDisconnected();
                    return;
                }
                else
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ((IChannel)this).Stop();
                }

            }
        }
    }

    private void ConnectCallBack(IAsyncResult result)
    {
        Socket client = (Socket)result.AsyncState;
        try
        {
            client.EndConnect(result);
            client.BeginReceive(mDataBuffer, 0, mDataBuffer.Length, SocketFlags.None, RecieveCallBack, client);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

        }
        finally
        {
            TimeoutObject.Set();
        }
       /*
        if (client == null)
            return;
        if (client.Connected)
        {
            client.EndConnect(result);
            client.BeginReceive(mDataBuffer, 0, mDataBuffer.Length, SocketFlags.None, RecieveCallBack, client);
        }
        */
    }

    private static void socketDataArrivalHandler(byte[] data)
    {}

    private static void socketDisconnectedHandler()
    {
        Reconnect();
    }

    private static void Reconnect()
    {
        //关闭socket
        mSocket.Shutdown(SocketShutdown.Both);

        mSocket.Disconnect(true);

        mSocket.Close();

        //创建Socket
        IChannel.Start();
    }

    //委托
    private delegate void delSocketDataArrival(byte[] data);
    static delSocketDataArrival socketDataArrival = socketDataArrivalHandler;

    private delegate void delSocketDisconnected();
    static delSocketDisconnected socketDisconnected = socketDisconnectedHandler;

    private static Socket mSocket = null;
    private static byte[] mDataBuffer = null;
    private static int mServerPort;
    private static IPAddress mServerIpAddress;
    private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);
}
Csdn user default icon
上传中...
上传图片
插入图片