private void MessageReceived(string message, string ip, Information info, string recvIndex, string countRes) // the clients who wants to download send me my index and how many resources there are { try { int start, end; string name = ""; name = message; // file name int resourceIndx = int.Parse(recvIndex); int countResources = int.Parse(countRes); /** sending file content to client**/ TcpClient fileClnt = new TcpClient(); // create tcp listener fileClnt.Connect(ip, info.Port); // connect with port 8006 to endpoint strm = fileClnt.GetStream(); string type = "file"; byte[] typeB = Encoding.ASCII.GetBytes(type); Thread.Sleep(4000); strm.Write(typeB, 0, typeB.Length); // send msg type DownloadMessage downloadMsg = new DownloadMessage(); downloadMsg.fileName = name; /** reading file content **/ byte[] fileContentMsg = File.ReadAllBytes(info.Path + "\\" + name); start = resourceIndx * (fileContentMsg.Length / countResources); if (resourceIndx == countResources - 1) // last index needs to add modolu { end = (resourceIndx + 1) * (fileContentMsg.Length / countResources) + (fileContentMsg.Length % countResources); } else { end = (resourceIndx + 1) * (fileContentMsg.Length / countResources); } byte[] sendFileB = new byte[end - start]; // create bytes array with section size (2 rows) Array.Copy(fileContentMsg, start, sendFileB, 0, end - start); downloadMsg.fileContent = sendFileB; downloadMsg.index = "" + resourceIndx; downloadMsg.numOfResorces = "" + countResources; downloadMsg.fileSize = "" + fileContentMsg.Length; string downloadSendMsg = MessageHendler.SerializeAnObject(downloadMsg); // convert from object to xml string byte[] contentBytes = Encoding.ASCII.GetBytes(downloadSendMsg); // convert to bytes Thread.Sleep(4000); strm.Write(contentBytes, 0, contentBytes.Length); // send strm.Close(); fileClnt.Close(); } catch (Exception ex) { System.Windows.MessageBox.Show("In download progress, please try later"); } }
/** this is the thread function which recieves messages and listen to clients - server **/ private void Receiver(Object obj) { Information info = (Information)obj; receivingClient.Start(); // strat tcpListener IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port); // get messages from any ip event1 += new AddMessage(MessageReceived); // add action listener to delegete string message; while (true) { try { Socket s = receivingClient.AcceptSocket(); /** recieving request type **/ byte[] bReq = new byte[s.SendBufferSize]; int reqSize = s.Receive(bReq); message = ""; for (int i = 0; i < reqSize; i++) // convert from byte to string { message += Convert.ToChar(bReq[i]); } // check request type: if (info.IpAddress.Equals(message)) // log out { // closing window request - if recieved ip is equal to my ip - stop listening. break; } else { if (message.Equals("file")) // recieve file that i downloaded { byte[] recvFileInfoB = new byte[s.SendBufferSize]; // get file info in bytes int k = s.Receive(recvFileInfoB); string recvFileInfo = ""; for (int i = 0; i < k; i++) // convert from byte to string { recvFileInfo += Convert.ToChar(recvFileInfoB[i]); } Array.Clear(recvFileInfoB, 0, recvFileInfoB.Length); DownloadMessage DownloadMessage = new DownloadMessage(); // class of downloaded message details Object ob = MessageHendler.DeSerializeAnObject(recvFileInfo, DownloadMessage.GetType()); // convert from string to DownloadMessage object (2 rows) DownloadMessage myDownloadMessage = (DownloadMessage)ob; int resourceIndex = int.Parse(myDownloadMessage.index); // get idx from DownloadMessage obj int countResources = int.Parse(myDownloadMessage.numOfResorces); // get num of resources from DownloadMessage obj int fileSize = int.Parse(myDownloadMessage.fileSize); // get file size from DownloadMessage obj string fileInfo = ""; fileInfo = info.Path + "\\" + myDownloadMessage.fileName; if (resourceIndex == 0) // create new file only if this is the first source index { File.WriteAllBytes(fileInfo, myDownloadMessage.fileContent); } else // not first source index { using (var stream = new FileStream(fileInfo, FileMode.Append)) { // concat to last part of file stream.Write(myDownloadMessage.fileContent, 0, myDownloadMessage.fileContent.Length); } } ts = DateTime.Now - dt; // calc dowmload time string downloadedMsg = ""; int idx = resourceIndex + 1; downloadedMsg += "" + idx + " / " + countResources + " arrived! \n"; downloadedMsg += "total time: " + ts.Seconds + " seconds. \n"; downloadedMsg += "total length: " + myDownloadMessage.fileContent.Length + " kb. \n"; downloadedMsg += "bit rate: " + myDownloadMessage.fileContent.Length / ts.Seconds + " kb/s. \n"; System.Windows.MessageBox.Show(downloadedMsg); if (resourceIndex == countResources - 1) // last resource { // create new MyFile and add it to DB long length = new System.IO.FileInfo(fileInfo).Length; MyFile myFile = new MyFile(); myFile.fileName = myDownloadMessage.fileName; myFile.size = fileSize; myFile.IP = info.IpAddress; myFile.port = info.Port; myFile.path = info.Path; MessageHendler.insertNewFile(myFile); } } else // another client download file from me { byte[] recvFileInfoB = new byte[s.SendBufferSize]; // get wanted file info int k = s.Receive(recvFileInfoB); string recvFileInfo = ""; for (int i = 0; i < k; i++) { recvFileInfo += Convert.ToChar(recvFileInfoB[i]); } Array.Clear(recvFileInfoB, 0, recvFileInfoB.Length); FileReqMessage fileReq = new FileReqMessage(); Object ob = MessageHendler.DeSerializeAnObject(recvFileInfo, fileReq.GetType()); // convert from string to FileReqMessage obj (2 rows) FileReqMessage myfileReq = (FileReqMessage)ob; event1(myfileReq.fileName, myfileReq.myIP, info, myfileReq.resourceIndex, myfileReq.numOfResources); // call delegete func } } } catch (Exception ex) { System.Windows.MessageBox.Show("In download progress, please try later"); } } receivingClient.Stop(); // stop listening System.Windows.MessageBox.Show("bye bye"); return; }