コード例 #1
0
ファイル: Form1.cs プロジェクト: silverfoxy/ChatMat
        void ReceiveData(object clientSocketObj)
        {
            ClientSocketObj obj = (ClientSocketObj)clientSocketObj;
            Socket clientSocket = obj.socket;
            ChatBox cb = new ChatBox(clientSocket);
            FileTransfer ft = new FileTransfer();
            bool cbOpen = false;
            int len;
            string data;
            string clientName = string.Empty;
            byte[] buffer = new byte[1024];
            int flag = 0;
            string receivedPath = string.Empty;
            int fileNameLen = 0;
            int fileLen = 0;
            int received = 0;
            bool newClient = true;

            while (true)
            {
                try
                {
                    len = clientSocket.Receive(buffer);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                    clientSocket.Close();
                    return;
                }
                data = Encoding.UTF8.GetString(buffer, 0, len);
                try
                {
                    if (data != "RCVD" && data != "[X]\r\n")
                    {
                        if (data == "/quit\n" || data == "/quit")
                        {
                            clientSocket.Send(Encoding.UTF8.GetBytes("Bye"));
                            AppendTextBox(cb, clientSocket.RemoteEndPoint.ToString() + " Left\r\n", true);
                            clientSocket.Close();
                            if (cb.IsHandleCreated)
                            {
                                CloseForm(cb);
                            }
                            return;
                        }
                        else if (data == "AreYouThere?")
                        {
                            if (online)
                                clientSocket.Send(Encoding.UTF8.GetBytes("yeah"));
                            else
                                clientSocket.Send(Encoding.UTF8.GetBytes("no"));
                            continue;
                        }
                        else if (data.Length > 2 && data.Substring(0, 2) == "/s")
                        {
                            if (!online && !obj.initiatedByUs)
                            {
                                clientSocket.Close();
                                return;
                            }
                            if (!cbOpen)
                            {
                                cbOpen = true;
                                OpenNewChatBox(cb);
                            }
                            newClient = false;
                            string addr = data.Substring(3, data.Length - 3).Split('|').GetValue(0).ToString();
                            string ip = addr.Split(':')[0].ToString();
                            int port = int.Parse(addr.Split(':')[1].ToString());
                            cb.RIpAddr = ip;
                            cb.Rport = port;

                            string name =data.Split('|').GetValue(1).ToString();
                            clientName = name.Substring(6, name.Length - 6);
                            SetFormText(cb, clientName);
                            continue;
                        }
                        else if (data.Contains("/name"))
                        {

                            clientName = data.Substring(data.IndexOf("/name") + 6, data.Length - 6);
                            SetFormText(cb, clientName);
                            //SetFormText(cb, "Friend");
                            continue;
                        }
                        else if (newClient)
                        {
                            if (len > 0)
                            {

                                if (flag == 0)
                                {
                                    fileNameLen = BitConverter.ToInt32(buffer, 0);
                                    string fileName = Encoding.UTF8.GetString(buffer, 4, fileNameLen);
                                    receivedPath = fileName;
                                    fileLen = BitConverter.ToInt32(buffer, 4 + fileNameLen);
                                    flag++;
                                    ft = new FileTransfer();
                                    OpenFileTransferForm(ft);
                                    FileTransferSetStatus(ft, string.Format("Receiving '{0}'  [{1} Bytes]", Path.GetFileName(receivedPath), fileLen));
                                }
                                if (flag >= 1)
                                {
                                    BinaryWriter writer = new BinaryWriter(File.Open(Path.GetFileName(receivedPath), FileMode.Append));
                                    if (flag == 1)
                                    {
                                        writer.Write(buffer, 8 + fileNameLen, len - (8 + fileNameLen));
                                        flag++;
                                    }
                                    else
                                        writer.Write(buffer, 0, len);
                                    received += len;
                                    if (received > fileLen)
                                    {
                                        received = fileLen;
                                    }
                                    FileTransferSetStatus(ft, string.Format("Receiving '{0}'  [{1}/{2} Bytes]", Path.GetFileName(receivedPath), received, fileLen));
                                    FileTransferSetPBar(ft, fileLen, received);
                                    if (received == fileLen)
                                    {
                                        //clientSocket.Send(Encoding.UTF8.GetBytes("filereceived"));
                                        FileTransferCompleted(ft, receivedPath);
                                    }
                                    writer.Close();
                                }
                            }
                            continue;
                        }
                        clientSocket.Send(Encoding.UTF8.GetBytes("RCVD"));
                        AppendTextBox(cb, clientName + ": " + data + "\r\n", true);
                    }
                    else
                        AppendTextBox(cb, "   [X]\r\n", true); //ACK

                }
                catch (Exception ex)
                {
                    if (cb.IsHandleCreated)
                    {
                        CloseForm(cb);
                    }
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: silverfoxy/ChatMat
 void OpenNewChatBox(ChatBox cb)
 {
     if (InvokeRequired)
     {
         this.BeginInvoke((Action)delegate
         {
             cb.Show();
         });
     }
 }