public void SendMessage(int type)
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(new IPEndPoint(ipAd, port));

            ClientState state = new ClientState();
            state.Client = socket;
            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] data = asen.GetBytes("[" + type + "," + DomainController.getInstance().myAccount.accountId + "]");
            //add prefix to data
            state.DataToSend = new byte[data.Length + 4];
            byte[] prefix = BitConverter.GetBytes(data.Length);
            //copy data size prefix
            Buffer.BlockCopy(prefix, 0, state.DataToSend, 0, prefix.Length);
            //copy the data
            Buffer.BlockCopy(data, 0, state.DataToSend, prefix.Length, data.Length);

            socket.BeginSend(state.DataToSend, 0, state.DataToSend.Length,
              SocketFlags.None, new AsyncCallback(ClientSendCallback), state);
            Thread.Sleep(50);
        }
예제 #2
0
        public void SendMessage(int type, string username, string password)
        {
            if (type == 0)
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                GuiController.getInstance().SetDebugText("Connecting...");
                socket.Connect(new IPEndPoint(ipAdress, port));

                ClientState state = new ClientState();
                state.Client = socket;
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] data = asen.GetBytes("[" + type + "]" + username);
                //add prefix to data
                state.DataToSend = new byte[data.Length + 4];
                byte[] prefix = BitConverter.GetBytes(data.Length);
                //copy data size prefix
                Buffer.BlockCopy(prefix, 0, state.DataToSend, 0, prefix.Length);
                //copy the data
                Buffer.BlockCopy(data, 0, state.DataToSend, prefix.Length, data.Length);
                loggingOut = true;
                if (NetworkController.getInstance().stopConnections())
                {
                    socket.BeginSend(state.DataToSend, 0, state.DataToSend.Length,
                      SocketFlags.None, new AsyncCallback(ClientSendCallback), state);
                }
            }
            else
            {
                ClientState state = new ClientState();
                state.Client = socket;
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] data = asen.GetBytes("[" + type + "]" + username + "," + password);
                //add prefix to data
                state.DataToSend = new byte[data.Length + 4];
                byte[] prefix = BitConverter.GetBytes(data.Length);
                //copy data size prefix
                Buffer.BlockCopy(prefix, 0, state.DataToSend, 0, prefix.Length);
                //copy the data
                Buffer.BlockCopy(data, 0, state.DataToSend, prefix.Length, data.Length);

                socket.BeginSend(state.DataToSend, 0, state.DataToSend.Length,
                  SocketFlags.None, new AsyncCallback(ClientSendCallback), state);
            }

            //Thread.Sleep(50);
        }