コード例 #1
0
        public void RequestAllAvailableThread()
        {
            TcpClient tcpClient = null;
            NetworkStream nStream = null;

            try
            {
                tcpClient = new TcpClient(TorrentFConfig.GetConfig().trackerIp, TorrentFConfig.GetConfig().trackerHttpPort);
                Trace.Assert(tcpClient != null, "MultiFileLookupThreadParam::RequestAllAvailableThread, cannot open connexion to the local tracker for lookup purpose.");

                // Sending the request
                string message = "GET /GetListOfPublishedTorrents HTTP/1.1\n\n";
                nStream = tcpClient.GetStream();
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message);
                nStream.Write(buffer, 0, buffer.Length);
                nStream.Flush();

                // Treating the answer
                byte[] receiveBuffer = new byte[3000];
                string receivedMessage = null;
                Int32 nRead = nStream.Read(receiveBuffer, 0, receiveBuffer.Length);
                // received list format :node1Ip#file1*file1.size-file2*file2.size-file3*file3.size\n
                receivedMessage = System.Text.Encoding.ASCII.GetString(receiveBuffer, 0, nRead);
                if (receivedMessage.Length > 0)
                {
                    receivedMessage = System.Text.Encoding.ASCII.GetString(receiveBuffer, 0, nRead);
                    ParseTrackerMessage ptm = new ParseTrackerMessage();
                    ptm.ParseMultiFileMessage(receivedMessage, ref existingFileName);
                    lookupList = ptm.ResultingFiles;
                }
                else
                {
                    MessageBox.Show("Invalid received data from the local tracker.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }

            catch
            {
                // Problem occured while trying to request a file from the tracker
                MessageBox.Show("Problem occured while trying to request a file from the tracker","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
            }

            finally
            {
                if (tcpClient != null)
                {
                    if(tcpClient!=null)
                        tcpClient.Close();
                    if (nStream != null)
                        nStream.Close();
                }
            }
        }
コード例 #2
0
        public void RequestSingleFile()
        {
            Trace.Assert(CurrentFileDescription != null, "SingleFileLookupThreadParam::RequestSingleFile, invalid keyword.");
            TcpClient tcpClient = null;
            NetworkStream nStream = null;
            //try
            {
                tcpClient = new TcpClient(TorrentFConfig.GetConfig().trackerIp, TorrentFConfig.GetConfig().trackerHttpPort);
                Trace.Assert(tcpClient != null, "SingleFileLookupThreadParam::RequestSingleFile, cannot open connexion to the local tracker for lookup purpose.");
                // Construct the message to send
                StringBuilder message = new StringBuilder("GET /GetTorrentOwner?file_name=");
                // Add the file name to look for
                message.Append(currentFileDescription);
                message.Append("&black_list=local");
                message.Append(" HTTP/1.1\n\n");

                nStream = tcpClient.GetStream();
                byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message.ToString());
                nStream.Write(buffer, 0, buffer.Length);
                nStream.Flush();
                // Receive response
                byte[] receiveBuffer = new byte[1024];
                int nRead = nStream.Read(receiveBuffer, 0, receiveBuffer.Length);
                // received data format: node1Ip#file1*file1.size
                string receivedMessage = null;
                receivedMessage = System.Text.Encoding.ASCII.GetString(receiveBuffer, 0, nRead);
                if (receivedMessage.Length > 0)
                {
                    // Parse received message
                    ParseTrackerMessage ptm = new ParseTrackerMessage();

                    ptm.ParseMultiFileMessage(receivedMessage,ref existingFileName);
                    if (ptm.ResultingFiles.Count > 0)
                    {
                        if (ptm.ResultingFiles.Count == 1)
                        {
                            // Just one answer
                            CurrentFileDetails.Add(ptm.ResultingFiles[0]);
                            NumberOfAnswers = 1;
                        }
                        else
                        {
                            // Multiple possible choices
                            foreach (FileDetails fd in ptm.ResultingFiles)
                            {
                                CurrentFileDetails.Add(fd);
                            }
                            NumberOfAnswers = ptm.ResultingFiles.Count;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Invalid received data from the local tracker.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }

            }

               // catch
            {
                // Problem occured while trying to request a file from the tracker
            //                MessageBox.Show("Problem occured while trying to request a file from the tracker", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }

               // finally
            {
                if (nStream != null)
                    nStream.Close();

                if (tcpClient != null)
                    tcpClient.Close();
            }
        }