コード例 #1
0
    //连接类(信息回馈)
    public void RoomNum(ProtocolBase protoBase)
    {
        ProtocolBytes proto     = (ProtocolBytes)protoBase;
        int           start     = 0;
        string        protoName = proto.GetString(start, ref start);
        int           num       = proto.GetInt(start, ref start);

        Client.instance.roomnum = num;
        if (num == 2 || ServerNet.IsUse())
        {
            SceneManager.instance.StartScene();
        }
    }
コード例 #2
0
    public bool Connect()
    {
        string host = "119.23.52.136";

        // Debug
        if (ServerNet.IsUse())
        {
            host = "127.0.0.1";
        }

        int port = 9970;

        try
        {
            socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            UDPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            Host = host;
            //ip = new IPEndPoint(IPAddress.Any, port - 2);
            //ip = new IPEndPoint(IPAddress.Any, port);

            //socket.Connect(host, port);
            IsConnect = false;
            TimeoutObjct.Reset();

            socket.BeginConnect(host, port, ConnectCb, socket);

            if (TimeoutObjct.WaitOne(timeoutMSec, false))
            {
                if (IsConnect)
                {
                }
                else
                {
                    //Debug.Log(1);
                    Close();
                    return(false);
                }
            }
            else
            {
                Close();
                return(false);
            }
            //Debug.Log(((IPEndPoint)socket.LocalEndPoint).Port);
            ip = new IPEndPoint(IPAddress.Any, ((IPEndPoint)socket.LocalEndPoint).Port);
            UDPsocket.Bind(ip);

            isUse    = true;
            ipAdress = host;

            socket.BeginReceive(readBuff, buffCount, BUFFER_SIZE - buffCount, SocketFlags.None, ReceiveCb, readBuff);
            UDPsocket.BeginReceive(UDPreadBuff, 0, BUFFER_SIZE, SocketFlags.None, UDPReceiveCb, UDPreadBuff);
            return(true);
        }
        catch (Exception e)
        {
            Debug.Log(isUse);
            Debug.Log(e.Message);
            return(false);
        }
    }