Beispiel #1
0
        public void vidrecv(string msgReceived, Socket clientSocket)
        {
            if (msgReceived[0] == 'V')
            {
                msgReceived = msgReceived.Substring(1);
                grp tmp = new grp();

                if (vc.Count == 0)
                {
                    tmp.id = 1;
                }
                else
                {
                    tmp.id = vc[vc.Count - 1].id + 1;
                }
                ClientInfo ci = new ClientInfo();

                string snd = "3V" + tmp.id + " ";
                string msg = "";
                int    i   = 0;
                foreach (ClientInfo c in clientList)
                {
                    if (c.socket == clientSocket)
                    {
                        msg += c.id + " " + c.strName + "**";
                        clientList[i].busy = true;
                        ci.socket          = c.socket;
                        ci.strName         = c.strName;
                        ci.id   = c.id;
                        ci.busy = c.busy;
                        tmp.mem.Add(ci);
                        vc.Add(tmp);
                        string rep = "3O";
                        message = Encoding.ASCII.GetBytes(rep);
                        clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);

                        break;
                    }
                    i++;
                }

                string[] x = msgReceived.Split('*');
                string   no, nam;
                foreach (string s in x)
                {
                    if (s.Length > 1)
                    {
                        no  = s.Split(' ')[0];
                        nam = s.Split(' ')[1];
                        //int j = 0;
                        foreach (ClientInfo c in clientList)
                        {
                            if (c.id == Convert.ToInt32(no) && c.strName == nam)
                            {
                                if (!c.busy)
                                {
                                    msg += c.id + " " + c.strName + "**";
                                    //clientList[i].busy = true;
                                    break;
                                }
                                else
                                {
                                    string rep = "I" + c.id + " " + c.strName + " Is Busy right now..Please try again later!! ";
                                    message = Encoding.ASCII.GetBytes(rep);
                                    clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
                                    break;
                                }
                            }
                        }
                    }
                }

                x       = null;
                x       = msg.Split('*');
                message = Encoding.ASCII.GetBytes(snd + msg);
                foreach (string s in x)
                {
                    if (s.Length > 1)
                    {
                        no  = s.Split(' ')[0];
                        nam = s.Split(' ')[1];
                        int j = 0;
                        foreach (ClientInfo c in clientList)
                        {
                            if (c.id == Convert.ToInt32(no) && c.strName == nam)
                            {
                                if (c.socket == clientSocket)
                                {
                                    break;
                                }
                                else
                                {
                                    c.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), c.socket);
                                    break;
                                }
                            }
                            j++;
                        }
                    }
                }
            }

            else if (msgReceived[0] == 'S')
            {
                int i = 0;
                foreach (ClientInfo c in clientList)
                {
                    if (c.socket == clientSocket)
                    {
                        clientList[i].busy = false;
                        break;
                    }
                    i++;
                }
            }

            else if (msgReceived[0] == 'R')
            {
                msgReceived = msgReceived.Substring(1);

                string no = "";
                int    i  = 0;
                while (msgReceived[i] != ' ')
                {
                    no += msgReceived[i];
                    i++;
                }
                msgReceived = msgReceived.Substring(i + 1);


                if (msgReceived[0] == 'Y')
                {
                    string     msg = "";
                    string     all = "3N";
                    ClientInfo ci  = new ClientInfo();
                    i = 0;
                    foreach (ClientInfo c in clientList)
                    {
                        if (c.socket == clientSocket)
                        {
                            msg = "3N" + c.strName + " " + (IPAddress.Parse(((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString())) + "**";
                            clientList[i].busy = true;
                            ci.socket          = c.socket;
                            ci.strName         = c.strName;
                            ci.id   = c.id;
                            ci.busy = c.busy;
                            break;
                        }
                        i++;
                    }


                    message = Encoding.ASCII.GetBytes(msg);
                    foreach (grp g in vc)
                    {
                        if (g.id == Convert.ToInt32(no))
                        {
                            foreach (ClientInfo c in g.mem)
                            {
                                c.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), c.socket);
                                all += c.strName + " " + (IPAddress.Parse(((IPEndPoint)c.socket.RemoteEndPoint).Address.ToString())) + "**";
                            }
                            g.mem.Add(ci);

                            message = Encoding.ASCII.GetBytes(all);
                            ci.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), ci.socket);
                            break;
                        }
                    }
                }



                else
                {
                    string k = "";
                    foreach (ClientInfo c in clientList)
                    {
                        if (c.socket == clientSocket)
                        {
                            k = "Irequest to " + c.strName + " rejected";
                        }
                    }
                    if (k != "")
                    {
                        foreach (grp g in vc)
                        {
                            if (g.id == Convert.ToInt32(no))
                            {
                                message = Encoding.ASCII.GetBytes(k);
                                g.mem[0].socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), g.mem[0].socket);
                                break;
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public bool txtrecv(string msgReceived, Socket clientSocket)
        {
            if (msgReceived[0] == 'A')
            {
                ClientInfo clientInfo = new ClientInfo();
                clientInfo.socket  = clientSocket;
                clientInfo.strName = msgReceived.Substring(1);
                if (clientList.Count == 0)
                {
                    clientInfo.id = 1;
                }
                else
                {
                    clientInfo.id = clientList[clientList.Count - 1].id + 1;
                }
                clientList.Add(clientInfo);

                //Set the text of the message that we will broadcast to all users
                string s = "<<<" + msgReceived.Substring(1) + " has joined the room>>>";
                textBox1.Text += "\r\n" + s;
                sendtoall(s);
                //sendlist();
            }


            else if (msgReceived[0] == 'X')
            //x->remove client entirely
            //x12->remove client from group 12 only
            {
                if (msgReceived.Length == 1)
                {
                    int nIndex = 0;
                    foreach (ClientInfo client in clientList)
                    {
                        if (client.socket == clientSocket)
                        {
                            string s = "<<< " + client.strName + " Has left the room >>>";
                            textBox1.Text += "\r\n" + s;
                            clientList.RemoveAt(nIndex);
                            foreach (grp g in ppl)
                            {
                                if (g.mem.Contains(client))
                                {
                                    g.mem.Remove(client);
                                    sendgrplst(g.id);
                                }
                            }
                            sendtoall(s);
                            break;
                        }
                        ++nIndex;
                    }

                    clientSocket.Close();
                    sendlist();
                    return(false);
                }
                else
                {
                    int n = Convert.ToInt32(msgReceived.Substring(1));
                    foreach (grp g in ppl)
                    {
                        if (g.id == n)
                        {
                            for (int i = g.mem.Count - 1; i >= 0; i--)
                            {
                                if (g.mem[i].socket == clientSocket)
                                {
                                    string s = "<<< " + g.mem[i].strName + " Has left group" + n + " >>>";
                                    textBox1.Text += "\r\n" + s;
                                    g.mem.Remove(g.mem[i]);
                                    break;
                                }
                            }

                            //not necessary
                            //string s = "C" + g.id.ToString();
                            //message = Encoding.ASCII.GetBytes(s);

                            //clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
                            ////till here

                            sendgrplst(g.id);
                            break;
                        }
                    }
                }
            }
            else if (msgReceived[0] == 'L')
            {
                sendlist();
            }
            else if (msgReceived[0] == 'M')
            {
                if (msgReceived[1] == 'A')
                {
                    sendtoall(msgReceived.Substring(2));
                }
                else
                {
                    string no = null;
                    int    i  = 1;
                    while (msgReceived[i] != ' ')
                    {
                        no += msgReceived[i];
                        i++;
                    }
                    //now no has group no for which msg has been sent
                    msgReceived = msgReceived.Substring(i + 1);//it should have only msg left by now
                    multigp(msgReceived, Convert.ToInt32(no));
                }
            }
            else if (msgReceived[0] == 'T') //create new group
            {
                grp tmp = new grp();
                if (ppl.Count == 0)
                {
                    tmp.id = 1;
                }
                else
                {
                    tmp.id = ppl[ppl.Count - 1].id + 1;
                }
                ClientInfo ci = new ClientInfo();


                msgReceived = msgReceived.Substring(1);
                string[] lis = msgReceived.Split('*');
                string   no, nam;
                string   sendmsg = "1T" + tmp.id + " " + msgReceived;
                message = Encoding.ASCII.GetBytes(sendmsg);
                foreach (ClientInfo client in clientList)
                {
                    if (client.socket == clientSocket)
                    {
                        ci.id      = client.id;
                        ci.socket  = clientSocket;
                        ci.strName = client.strName;
                        ci.busy    = client.busy;
                        tmp.mem.Add(ci);
                        ppl.Add(tmp);
                        byte[] mssg = Encoding.ASCII.GetBytes("1O" + tmp.id);
                        client.socket.BeginSend(mssg, 0, mssg.Length, SocketFlags.None, new AsyncCallback(OnSend), client.socket);
                        break;

                        //coz i've got corresponding no n name for this loop,i wont be getting them again
                    }
                }
                foreach (string s in lis)
                {
                    if (s.Length > 0)
                    {
                        no  = s.Split(' ')[0];
                        nam = s.Split(' ')[1];
                        foreach (ClientInfo client in clientList)
                        {
                            if ((client.id == Convert.ToInt32(no)) && (client.strName == nam))
                            {
                                client.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), client.socket);
                                break;
                            }
                        }
                    }
                }
            }

            else if (msgReceived[0] == 'R')
            {
                if (msgReceived[1] == 'Y')
                {
                    int no = Convert.ToInt32(msgReceived.Substring(2));
                    //clientsocket accepted
                    bool found = false;
                    foreach (ClientInfo client in clientList)
                    {
                        if (client.socket == clientSocket)
                        {
                            found = true;
                            ClientInfo ci = new ClientInfo();
                            ci.id      = client.id;
                            ci.socket  = clientSocket;
                            ci.strName = client.strName;
                            ci.busy    = client.busy;
                            foreach (grp g in ppl)
                            {
                                if (g.id == no)
                                {
                                    g.mem.Add(ci);
                                    multigp(ci.strName + " has joined The group ", no); //send everyone thet new user has been added
                                    textBox1.Text += "\r\n" + ci.strName + " has joined The group " + no.ToString();
                                    sendgrplst(no);                                     //update group list

                                    break;
                                }
                            }
                        }
                        if (found)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    //clientsocket rejected
                    textBox1.Text += "\r\n" + "Rejected The group request";
                }
            }
            else if (msgReceived == "")
            {
                message        = Encoding.ASCII.GetBytes("resend");
                textBox1.Text += "\r\n" + "broken message recieved..sending request to retransmit";

                clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientSocket);
            }


            return(true);
        }