Ejemplo n.º 1
0
        private void addBtn_Click(object sender, EventArgs e)
        {
            if (pathText.Text == "")
            {
                MessageBox.Show("유효한 경로가 입력되지 않았습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // send request
            ListViewItem checkedItem = serverList.FocusedItem;
            this.requestNo = serverList.Items.IndexOf(checkedItem).ToString();
            int i = 0;
            while (true)
            {
                if(playList.Items.Count == 0)
                {
                    break;
                }
                string title = playList.Items[i].SubItems[0].Text;
                if (checkedItem.SubItems[0].Text.CompareTo(title) == 0)
                {
                    MessageBox.Show("이미 등록되었습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }else if(i+1 == playList.Items.Count)
                {
                    break;
                }

                i++;
            }
            m_requestMusic = new RequestMusic();

            m_requestMusic.requestNo = requestNo;

            Packets.Serialize(m_requestMusic).CopyTo(sendBuffer, 0);

            this.Send();
            // send request end
            
            
            progressBar.Value = 0;
            

            // recieve file name
            try
            {
                int nRead = 0;
                nRead = this.m_NetStream.Read(this.readBuffer, 0, 1024 * 4);

            }
            catch
            {
                //this.m_NetStream = null;
            }
            object obj = Packets.Deserialize(this.readBuffer);
            this.m_fileName = (FileName)obj;
            string filename = m_fileName.fileName;
            int size = m_fileName.Length;
            // recieve file name end

            // recieve file
            byte[] b = new byte[1024 * 4];
            int totalLength = 0;
            string songname = pathText.Text + "\\\\" + filename;

            fs = new FileStream(songname, FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);

            while (totalLength < size)
            {
                int nRead = 0;
                try
                {
                    nRead = 0;
                    nRead = this.m_NetStream.Read(this.readBuffer, 0, 1024 * 4);

                }
                catch
                {
                    //this.m_NetStream = null;
                }

                int recieveLength = nRead;
                bw.Write(this.readBuffer, 0, recieveLength);
                totalLength += recieveLength;
                progressBar.PerformStep();
            }

            bw.Close();
            fs.Close();
            // recieve file end

            // add music list
            this.playList.Items.Add((ListViewItem)checkedItem.Clone());
            // add music list end

            // delete server list
            // this.serverList.Items.Remove(checkedItem);
            // delete server list end

            // append playerlist
            currentPlay = wmp.newMedia(@"" + pathText.Text + "\\" + filename);
            playerList.appendItem(currentPlay);
            // append playerlist end
        }
Ejemplo n.º 2
0
        public void RUN()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                serverMessage("Server-start\nStorage Path:" + pathText.Text + "\n");
                this.m_Listener = new TcpListener(Int32.Parse(portText.Text));
            }));
            this.m_Listener.Start();

            serverMessage("waiting client access...\n");


            Socket client = this.m_Listener.AcceptSocket();

            if (client.Connected)
            {
                serverMessage("client access!\n");
                this.m_NetStream = new NetworkStream(client);
            }

            serverMessage("리스트 전송...\n");

            this.Invoke(new MethodInvoker(delegate()
            {
                for (int i = 0; i < musicList.Items.Count; i++)
                {
                    m_musicList        = new MusicList();
                    m_musicList.Type   = (int)PacketType.음악리스트;
                    m_musicList.Length = musicList.Items.Count;
                    string strText     = null;
                    for (int j = 0; j < 4; j++)
                    {
                        strText += musicList.Items[i].SubItems[j].Text;
                        strText += "_";
                    }
                    m_musicList.list = strText;
                    Packets.Serialize(m_musicList).CopyTo(this.sendBuffer, 0);

                    this.Send();
                }
            }));

            serverMessage("리스트 전송 완료\n");


            /* 요청 파일 전송 대기 */
            int nRead = 0;

            while (client.Connected)
            {
                serverMessage("요청 대기중...\n");
                try
                {
                    nRead = 0;
                    nRead = this.m_NetStream.Read(this.readBuffer, 0, 1024 * 4);
                }catch
                {
                    this.m_NetStream = null;
                    break;
                }
                this.m_requestMusic = (RequestMusic)Packets.Deserialize(this.readBuffer);
                this.requestNo      = this.m_requestMusic.requestNo;
                int    index    = Int32.Parse(this.requestNo);
                string filename = fi[index].Name;

                /* mp3 길이 전송 */
                string songname = pathText.Text + "\\\\" + filename;

                fs = new FileStream(songname, FileMode.Open, FileAccess.Read);
                int    fileLength = (int)fs.Length;
                byte[] lengthB    = BitConverter.GetBytes(fileLength);

                /* mp3 파일명 전송 */
                m_fileName          = new FileName();
                m_fileName.fileName = filename;
                m_fileName.Length   = fileLength;
                Packets.Serialize(m_fileName).CopyTo(this.sendBuffer, 0);
                this.Send();
                /* mp3 전송 */
                int          count = fileLength / (1024 * 4) + 1;
                BinaryReader br    = new BinaryReader(fs);
                for (int i = 0; i < count; i++)
                {
                    lengthB = br.ReadBytes(1024 * 4);
                    lengthB.CopyTo(this.sendBuffer, 0);
                    this.Send();
                }
                br.Close();
                fs.Close();
            }
        }