Ejemplo n.º 1
0
        private void Receive()//받자 파일을
        {
            while (this.m_blsConnect)
            {
                try
                {
                    this.m_NetStream.Read(this.readBuffer, 0, readBuffer.Length);
                }
                catch
                {
                    this.m_blsConnect = false;
                    this.m_NetStream  = null;
                }
                FileData temp = (FileData)FileData.Deserialize(readBuffer);

                switch ((int)temp.Type)
                {
                case (int)FileDataType.로그인:
                {
                    break;
                }

                case (int)FileDataType.일리스트:
                {
                    filelist fList = (filelist)FileData.Deserialize(readBuffer);
                    string   list  = fList.fileList;

                    string[] strset = list.Split('%');
                    for (int i = 1; i < strset.Length; i = i + 2)
                    {
                        ListViewItem lvi = new ListViewItem(strset[i - 1]);
                        lvi.SubItems.Add(strset[i]);
                        metroListView1.Items.Add(lvi);
                    }

                    break;
                }

                case (int)FileDataType.텍스트파일:
                {
                    textFile tempText = (textFile)FileData.Deserialize(readBuffer);
                    myText.fillText(tempText.text, tempText.userName);
                    break;
                }

                case (int)FileDataType.일:
                {
                    bool open = false;

                    myFile file = (myFile)FileData.Deserialize(readBuffer);

                    FileStream fs = new FileStream(drPath + "\\" + file.filename, FileMode.Create, FileAccess.Write);

                    fs.Close();

                    fs = new FileStream(drPath + "\\" + file.filename, FileMode.Append, FileAccess.Write);

                    for (int i = 0; i < (int)file.size / (1024 * 4); i++)        //파일크기만큼 반복문
                    {
                        if (open)
                        {
                            m_NetStream.Read(readBuffer, 0, readBuffer.Length);
                            file = (myFile)FileData.Deserialize(readBuffer);
                        }
                        fs.Write(file.data, 0, 1024 * 4);
                        open = true;
                    }
                    if (open)
                    {
                        m_NetStream.Read(readBuffer, 0, readBuffer.Length);
                    }

                    file = (myFile)FileData.Deserialize(readBuffer);
                    fs.Write(file.data, 0, (int)file.size % (1024 * 4));
                    break;
                }

                case (int)FileDataType.접속목록:
                {
                    listItem listitem = (listItem)FileData.Deserialize(readBuffer);
                    if (listitem.index != -1)
                    {
                        listView1.Items.RemoveAt(listitem.index);
                        break;
                    }
                    ListViewItem item;
                    item = new ListViewItem(listitem.list);
                    listView1.Items.Add(item);

                    break;
                }

                case (int)FileDataType.일체크:
                {
                    fileCheck fCheck = (fileCheck)FileData.Deserialize(readBuffer);
                    if (fCheck.fileNeed == 0)
                    {
                        textFile tFile = new textFile(backup);
                        tFile.Type      = (int)FileDataType.텍스트파일;
                        tFile.userName  = textBox3.Text;
                        tFile.userIndex = fCheck.fileNumber;        //원래는 파일넘버가 이게 아니지만 유저 인덱스 넘겨줌
                        sendBuffer      = FileData.Serialize(tFile);

                        m_NetStream.Write(sendBuffer, 0, sendBuffer.Length);
                        m_NetStream.Flush();
                    }
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
        }
Ejemplo n.º 2
0
        private void server_run()//서버를 열고 갯수만큼 가지고 있자.
        {
            int count = server_count;

            byte[] sendBuffer = new byte[1024 * 8];
            byte[] readBuffer = new byte[1024 * 8];
            m_Listener[count] = new TcpListener(Convert.ToInt32(port_text_box.Text));
            this.m_Listener[0].Start();//단일 리스너로 하면 되는거 같은데 잘모르겟다.
            m_ClientOn[count] = false;

            Socket Client = m_Listener[0].AcceptSocket();//이것도 연결요청 대기

            server_count++;
            people_count++;
            m_Thread[server_count] = new Thread(new ThreadStart(server_run));
            m_Thread[server_count].Start();

            if (Client.Connected)
            {
                m_ClientOn[count]     = true;
                m_NetStream[count]    = new NetworkStream(Client);
                connect_peopel[count] = true;
                Network_start[count]  = false;
            }

            while (m_ClientOn[count])
            {
                try
                {
                    m_NetStream[count].Read(readBuffer, 0, readBuffer.Length);
                }
                catch
                {
                    m_ClientOn[count]  = false;
                    m_NetStream[count] = null;
                }

                FileData temp = (FileData)FileData.Deserialize(readBuffer);

                switch ((int)temp.Type)
                {
                case (int)FileDataType.로그인:
                {
                    login log = (login)FileData.Deserialize(readBuffer);
                    if (log.logout)
                    {
                        int check = -1;
                        m_ClientOn[count] = false;
                        for (int i = 0; i < 32; i++)
                        {
                            if (client_text_list.Items[i].SubItems[1].Text == log.id)
                            {
                                check = i;
                                break;
                            }
                        }
                        server_text_box.AppendText(client_text_list.Items[check].SubItems[1].Text + "님이 로그아웃 하셧습니다.\n");
                        connect_peopel[Convert.ToInt32(client_text_list.Items[check].SubItems[0].Text)] = false;
                        client_text_list.Items.RemoveAt(check);

                        listItem removeItem = new listItem();
                        removeItem.index = check;
                        removeItem.Type  = (int)FileDataType.접속목록;
                        sendBuffer       = FileData.Serialize(removeItem);
                        for (int i = 0; i < 32; i++)
                        {
                            if (connect_peopel[i])
                            {
                                m_NetStream[i].Write(sendBuffer, 0, sendBuffer.Length);
                                this.m_NetStream[i].Flush();
                            }
                        }
                        people_count--;
                        break;
                    }
                    ListViewItem item;
                    string[]     itemStr = new string[3];
                    itemStr[0] = count.ToString();
                    itemStr[1] = log.id;
                    itemStr[2] = "접속중...";
                    item       = new ListViewItem(itemStr);
                    client_text_list.Items.Add(item);
                    server_text_box.AppendText(log.id + "님이 로그인 하셧습니다.\n");

                    listItem listitem = new listItem();
                    listitem.list  = itemStr;
                    listitem.index = -1;
                    listitem.Type  = (int)FileDataType.접속목록;
                    sendBuffer     = FileData.Serialize(listitem);

                    for (int i = 0; i < 32; i++)
                    {
                        if (connect_peopel[i] && i != count)
                        {
                            m_NetStream[i].Write(sendBuffer, 0, sendBuffer.Length);
                            this.m_NetStream[i].Flush();
                        }
                    }
                    listitem.first = true;
                    for (int i = 0; i < people_count; i++)
                    {
                        itemStr[0]    = client_text_list.Items[i].SubItems[0].Text;
                        itemStr[1]    = client_text_list.Items[i].SubItems[1].Text;
                        itemStr[2]    = client_text_list.Items[i].SubItems[2].Text;
                        listitem.list = itemStr;
                        sendBuffer    = FileData.Serialize(listitem);
                        m_NetStream[count].Write(sendBuffer, 0, sendBuffer.Length);
                        m_NetStream[count].Flush();
                        Thread.Sleep(100);
                    }

                    break;
                }

                case (int)FileDataType.텍스트파일:
                {
                    textFile     tFile   = (textFile)FileData.Deserialize(readBuffer);
                    FileStream   fStream = new FileStream(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\" + tFile.userName + ".txt", FileMode.Create, FileAccess.Write);
                    StreamWriter fWriter = new StreamWriter(fStream);
                    fWriter.Write(tFile.text);
                    mutex[tFile.userIndex] = true;
                    fWriter.Close();
                    fStream.Close();

                    break;
                }

                case (int)FileDataType.일:
                {
                    while (!mutex[count])
                    {
                        ;
                    }
                    mutex[count] = false;

                    bool open = false;

                    myFile file = (myFile)FileData.Deserialize(readBuffer);

                    server_text_box.AppendText(file.filename + " : 업데이트 요청 \n");

                    FileStream fs = new FileStream(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\file\\" + file.filename, FileMode.Create, FileAccess.Write);

                    fs.Close();

                    fs = new FileStream(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\file\\" + file.filename, FileMode.Append, FileAccess.Write);

                    for (int i = 0; i < (int)file.size / (1024 * 4); i++)      //파일크기만큼 반복문
                    {
                        if (open)
                        {
                            m_NetStream[count].Read(readBuffer, 0, readBuffer.Length);
                            file = (myFile)FileData.Deserialize(readBuffer);
                        }
                        fs.Write(file.data, 0, 1024 * 4);
                        open = true;
                    }
                    if (open)
                    {
                        m_NetStream[count].Read(readBuffer, 0, readBuffer.Length);
                    }

                    file = (myFile)FileData.Deserialize(readBuffer);
                    fs.Write(file.data, 0, (int)file.size % (1024 * 4));

                    server_text_box.AppendText(file.filename + " 업데이트\n");

                    fs.Close();
                    mutex[count] = true;
                    break;
                }

                case (int)FileDataType.일리스트:
                {
                    DirectoryInfo di       = new DirectoryInfo(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\file");
                    string        fileList = "";
                    foreach (var item in di.GetFiles())
                    {
                        fileList += item.Name + "%";
                        fileList += item.Length + "%";
                    }
                    filelist fList = new filelist();

                    fList.fileList = fileList;
                    fList.Type     = (int)FileDataType.일리스트;
                    sendBuffer     = FileData.Serialize(fList);

                    m_NetStream[count].Write(sendBuffer, 0, sendBuffer.Length);
                    m_NetStream[count].Flush();

                    break;
                }

                case (int)FileDataType.일체크:
                {
                    string     userName = null;
                    string     data;
                    FileStream fStream;
                    fileCheck  fileCheck = (fileCheck)FileData.Deserialize(readBuffer);
                    for (int i = 0; i < 32; i++)
                    {
                        if (Convert.ToInt32(client_text_list.Items[i].SubItems[0].Text) == fileCheck.fileNumber)
                        {
                            userName = client_text_list.Items[i].SubItems[1].Text;
                            break;
                        }
                    }

                    if (fileCheck.fileNeed == 0)         //파일이 text 라면
                    {
                        while (!mutex[count])
                        {
                            ;
                        }
                        mutex[count] = false;

                        fStream = new FileStream(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\" + userName + ".txt", FileMode.OpenOrCreate, FileAccess.Read);
                        StreamReader streamReader = new StreamReader(fStream);
                        data = streamReader.ReadToEnd();

                        textFile tFIle = new textFile(data);
                        tFIle.text     = data;
                        tFIle.userName = userName;
                        tFIle.Type     = (int)FileDataType.텍스트파일;

                        sendBuffer = FileData.Serialize(tFIle);
                        m_NetStream[count].Write(sendBuffer, 0, sendBuffer.Length);
                        m_NetStream[count].Flush();
                        streamReader.Close();
                        fStream.Close();

                        mutex[count] = true;
                    }
                    else if (fileCheck.fileNeed == 1)
                    {
                        while (!mutex[count])
                        {
                            ;
                        }
                        mutex[count] = false;
                        string filename = fileCheck.filename;

                        server_text_box.AppendText(filename + " : 전송요청 \n");

                        fStream = new FileStream(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\file\\" + filename, FileMode.OpenOrCreate, FileAccess.Read);
                        StreamReader streamReader = new StreamReader(fStream);

                        System.IO.FileInfo fi = new FileInfo(path + "\\" + textBox1.Text + "/" + textBox2.Text + "월 " + textBox3.Text + "일" + "\\file\\" + filename);

                        myFile myfile = new myFile();
                        myfile.Type     = (int)FileDataType.일;
                        myfile.size     = (int)fi.Length;
                        myfile.filename = filename;

                        Byte[] byteSendData = new Byte[1024 * 4];

                        for (int i = 0; i < (int)fi.Length / (1024 * 4); i++)
                        {
                            fStream.Read(byteSendData, 0, byteSendData.Length);        //바이트단위로 뜯어서 보냄
                            myfile.data = byteSendData;
                            sendBuffer  = FileData.Serialize(myfile);
                            m_NetStream[count].Write(sendBuffer, 0, sendBuffer.Length);
                            m_NetStream[count].Flush();
                            Thread.Sleep(2);
                        }
                        fStream.Read(byteSendData, 0, (int)fi.Length % (1024 * 4));
                        myfile.data = byteSendData;
                        sendBuffer  = FileData.Serialize(myfile);
                        m_NetStream[count].Write(sendBuffer, 0, sendBuffer.Length);
                        m_NetStream[count].Flush();

                        server_text_box.AppendText(filename + " : 전송 완료 \n");
                        mutex[count] = true;
                    }
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
        }