示例#1
0
        public void FILE_UDPSend(string fileName, string formKey, string rcvId, IPEndPoint iep) 
        {
            logWrite("FILE_UDPSend: 파일전송 포트 변경 :" + iep.Port.ToString());

            SendFileForm sendFileForm = (SendFileForm)FileSendFormList[formKey];
            FileSendDetailListView fileSendDetailList = (FileSendDetailListView)FileSendDetailList[formKey];

            showFileSendDetailDelegate = new ShowFileSendDetailDelegate(ShowFileSendDetail);
            showFileSendStatDelegate = new ShowFileSendStatDelegate(ShowFileSendStat);
            showCloseButtonDelegate = new ShowCloseButtonDelegate(ShowCloseButton);
            
            FileInfo fi = new FileInfo(fileName);
            logWrite("FILE_UDPSend: FileInfo 인스턴스 생성 : " + fileName);

            int read = 0;
            byte[] buffer = null;
            byte[] re = null;

            if (filesendSock == null)
            {
                filesend = new IPEndPoint(IPAddress.Any, filesendport);
                filesendSock = new UdpClient(filesend);
            }
            else
            {
                filesendSock.Close();
                //filesend = new IPEndPoint(IPAddress.Any, filesendport);
                filesendSock = new UdpClient(filesend);
            }

            filesendSock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);

            if (!fi.Exists)
            {
                logWrite("SendFile() 파일이 없음 : " + fileName);
                return;
            }

            BufferedStream bs = new BufferedStream(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 40960), 40960);

            double sendfilesize = Convert.ToDouble(fi.Length);
            double percent = (40960 / sendfilesize) * 100;
            double total = 0.0;

            lock (filesendSock)
            {
                while (true)
                {
                    for (int i = 0; i < 3; i++) //udp 통신의 전송실패 방지
                    {
                        try
                        {
                            logWrite("FileReceiver IP : " + iep.Address.ToString());
                            logWrite("FileReceiver port : " + iep.Port.ToString());
                            if (sendfilesize >= 40960.0)
                                buffer = new byte[40960];
                            else buffer = new byte[Convert.ToInt32(sendfilesize)];
                            read = bs.Read(buffer, 0, buffer.Length);
                            filesendSock.Send(buffer, buffer.Length, iep);
                            logWrite("filesendSock.Send() : " + i.ToString() + " 번째 시도!");
                        }
                        catch (Exception e)
                        {
                            logWrite("SendFile() BufferedStream.Read() 에러 :" + e.ToString());
                        }
                        try
                        {
                            re = filesendSock.Receive(ref iep);
                            int reSize = int.Parse(Encoding.UTF8.GetString(re));
                            if (reSize == buffer.Length) break;
                        }
                        catch (SocketException e1)
                        {
                            logWrite(e1.ToString());
                        }
                    }

                    if (re == null || re.Length == 0)
                    {
                        logWrite("filesendSock.Send() 상대방이 응답하지 않습니다. 수신자 정보 : " + iep.Address.ToString() + ":" + iep.Port.ToString());
                        MessageBox.Show("파일전송 실패 : 상대방이 응답하지 않음", "전송실패", MessageBoxButtons.OK);
                        if (FileSendFormList.Count != 0 && FileSendFormList[formKey] != null)
                        {
                            Invoke(showFileSendStatDelegate, new object[] { "전송실패",-1, sendFileForm});
                        }
                        break;
                    }
                    else
                    {
                        sendfilesize = (sendfilesize - 40960.0);
                        total += percent;
                        if (total >= 100.0) total = 100.0;
                        string[] totalArray = (total.ToString()).Split('.');

                        if (!iep.Address.ToString().Equals(serverIP))
                        {
                            if (FileSendFormList.Count != 0 && FileSendFormList[formKey] != null)
                            {
                                Invoke(showFileSendStatDelegate, new object[] { "전송중(" + totalArray[0] + " %)",(int)total, sendFileForm });
                            }
                            if (FileSendDetailList.Count != 0 && FileSendDetailList[formKey] != null)
                            {
                                string detailmsg = "전송중(" + totalArray[0] + " %)";
                                Invoke(showFileSendDetailDelegate, new object[] { rcvId, detailmsg, fileSendDetailList });
                            }
                        }
                    }
                    if (total == 100.0)
                    {
                        string detailmsg = "전송완료";
                        if (iep.Address.ToString().Equals(serverIP))
                        {
                            Invoke(showFileSendDetailDelegate, new object[] { "server", detailmsg, fileSendDetailList });
                        }
                        else
                        {
                            Invoke(showFileSendDetailDelegate, new object[] { rcvId, detailmsg, fileSendDetailList });
                        }
                        bool isFinished = (bool)Invoke(showCloseButtonDelegate, fileSendDetailList);  //자세히
                        if (isFinished == false)
                            Invoke(showFileSendStatDelegate, new object[] { detailmsg, (int)total, sendFileForm });  //FileSendForm
                        else
                            Invoke(showFileSendStatDelegate, new object[] { "Finish",(int)total, sendFileForm });  //FileSendForm
                    }
                    if (total == 100.0)
                    {
                        bs.Close();
                        break;
                    }
                }
            }

            try
            {
                if (FileSendThreadList.Count != 0 && FileSendThreadList[formKey + "|" + rcvId] != null)
                {
                    ((Thread)FileSendThreadList[formKey + "|" + rcvId]).Abort();
                    logWrite("FileSendThread Abort()!");
                }
            }
            catch (ThreadAbortException te)
            {
                logWrite(te.ToString());
            }
        }
示例#2
0
        public void FILE_TCPSend(string fileName, string formKey, string rcvId, IPEndPoint iep) 
        {
            mRcvId = rcvId;
            logWrite("FILE_TCPSend: 파일전송 포트 변경 :" + SocketDef.PORT_FILE_NEW_SERVER);

            FileInfo fi = new FileInfo(fileName);
            logWrite("FILE_TCPSend: FileInfo 인스턴스 생성 : " + fileName);

            showFileSendDetailDelegate = new ShowFileSendDetailDelegate(ShowFileSendDetail);
            showFileSendStatDelegate = new ShowFileSendStatDelegate(ShowFileSendStat);
            showFileSendStatExDelegate = new ShowFileSendStatExDelegate(ShowFileSendStatEx);
            showCloseButtonDelegate = new ShowCloseButtonDelegate(ShowCloseButton);
            
            SendFileForm sendFileForm = (SendFileForm)FileSendFormList[formKey];
            FileSendDetailListView fileSendDetailList = (FileSendDetailListView)FileSendDetailList[formKey];

            if (!fi.Exists)
            {
                logWrite("SendFile() 파일이 없음 : " + fileName);
                return;
            }

            FtpClientManager ftpClient = new FtpClientManager(iep.Address.ToString(), SocketDef.PORT_FILE_NEW_SERVER, formKey);
            FtpClientThreadList[formKey] = ftpClient;
            ftpClient.SocStatusChanged += FTP_SndStatusChanged;

            if (!ftpClient.IsConnected())
            {
                if (ftpClient.Connect())
                    logWrite("[FILE_TCPSend]Server Connected.");
                else
                {
                    logWrite("[FILE_TCPSend]Server Not Connected.");
                    MessageBox.Show("파일전송 실패 : 상대방이 응답하지 않음", "전송실패", MessageBoxButtons.OK);
                    if (FileSendFormList.Count != 0 && FileSendFormList[formKey] != null)
                    {
                        //CHOI_DEBUG Invoke(showFileSendStatDelegate, new object[] { "전송실패", -1, sendFileForm });
                        Invoke(showFileSendStatExDelegate, new object[] { DownloadStatus.FAILED, "전송실패", -1, sendFileForm });
                    }
                    return;
                }
            }
            else
                logWrite("[FILE_TCPSend]Server Already Connected.");

            string file = Utils.GetFileName(fileName);
            string path = Utils.GetPath(fileName);

            ftpClient.setFileName(file);
            ftpClient.setFilePath(path);

            try
            {
                if (ftpClient.SendFile())
                {
                    string detailmsg = "전송완료";
                    if (iep.Address.ToString().Equals(serverIP))
                    {
                        Invoke(showFileSendDetailDelegate, new object[] { "server", detailmsg, fileSendDetailList });
                    }
                    else
                    {
                        Invoke(showFileSendDetailDelegate, new object[] { rcvId, detailmsg, fileSendDetailList });
                    }
                    bool isFinished = (bool)Invoke(showCloseButtonDelegate, fileSendDetailList);  //자세히
                    if (isFinished == false)
                        //CHOI_DEBUG Invoke(showFileSendStatDelegate, new object[] { detailmsg, 100, sendFileForm });  //FileSendForm
                        Invoke(showFileSendStatExDelegate, new object[] { DownloadStatus.SUCCESS, detailmsg, 100, sendFileForm });  //FileSendForm
                    else
                        //CHOI_DEBUG Invoke(showFileSendStatDelegate, new object[] { "Finish", 100, sendFileForm });  //FileSendForm
                        Invoke(showFileSendStatExDelegate, new object[] { DownloadStatus.SUCCESS, "Finish", 100, sendFileForm });  //FileSendForm
                }
                else
                {
                    logWrite("[FILE_TCPSend]Server Not Connected.");
                    MessageBox.Show("파일전송 실패 : 상대방이 응답하지 않음", "전송실패", MessageBoxButtons.OK);
                    if (FileSendFormList.Count != 0 && FileSendFormList[formKey] != null)
                    {
                        Invoke(showFileSendStatExDelegate, new object[] { DownloadStatus.FAILED, "전송실패", -1, sendFileForm });
                        //CHOI_DEBUG Invoke(showFileSendStatDelegate, new object[] { "전송실패", -1, sendFileForm });
                    }

                    ftpClient.Close();
                }
            }
            catch (IOException)
            {
            }
            finally
            {

            }
            try
            {
                if (FileSendThreadList.Count != 0 && FileSendThreadList[formKey + "|" + rcvId] != null)
                {
                    ((Thread)FileSendThreadList[formKey + "|" + rcvId]).Abort();
                    logWrite("FileSendThread Abort()!");
                }
            }
            catch (ThreadAbortException te)
            {
                logWrite(te.ToString());
            }
        }
示例#3
0
        //tempMsg(Y|���ϸ�|������Ű|������id)
        /// <summary>
        /// ���� ���� �޼ҵ�
        /// </summary>
        /// <param name="obj">���� ���ۿ� �ʿ��� ����(Y|���ϸ�|����������key|������id) </param>
        private void SendFile(object obj)
        {
            try
            {
                Hashtable table = (Hashtable)obj;
                IPEndPoint iep = null;
                string[] info = null;

                foreach (DictionaryEntry de in table)
                {
                    info = (string[])de.Key;
                    iep = (IPEndPoint)de.Value;
                }
                if (info[0].Equals("Y"))
                {
                    iep.Port = filereceiveport;   //������������� ��Ʈ�� ����
                }
                else
                {
                    iep.Port = 9001;   //������������ ��Ʈ�� ����
                }
                logWrite("SendFile() �������� ��Ʈ ���� :" + iep.Port.ToString());

                FileInfo fi = new FileInfo(info[1]);
                logWrite("SendFile() FileInfo �ν��Ͻ� ���� : " + info[1]);

                int read = 0;
                byte[] buffer = null;
                byte[] re = null;

                ShowFileSendDetailDelegate detail = new ShowFileSendDetailDelegate(ShowFileSendDetail);
                ShowFileSendStatDelegate show = new ShowFileSendStatDelegate(ShowFileSendStat);
                ShowCloseButtonDelegate close = new ShowCloseButtonDelegate(ShowCloseButton);
                SendFileForm form = (SendFileForm)FileSendFormList[info[2]];
                FileSendDetailListView view = (FileSendDetailListView)FileSendDetailList[info[2]];

                if (filesendSock == null)
                {
                    filesend = new IPEndPoint(IPAddress.Any, filesendport);
                    filesendSock = new UdpClient(filesend);
                }
                else
                {
                    filesendSock.Close();
                    //filesend = new IPEndPoint(IPAddress.Any, filesendport);
                    filesendSock = new UdpClient(filesend);
                }

                filesendSock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);

                if (fi.Exists == true)
                {

                    BufferedStream bs = new BufferedStream(new FileStream(info[1], FileMode.Open, FileAccess.Read, FileShare.Read, 40960), 40960);

                    double sendfilesize = Convert.ToDouble(fi.Length);
                    double percent = (40960 / sendfilesize) * 100;
                    double total = 0.0;

                    lock (filesendSock)
                    {
                        while (true)
                        {
                            for (int i = 0; i < 3; i++) //udp ����� ���۽��� ����
                            {
                                try
                                {
                                    logWrite("FileReceiver IP : " + iep.Address.ToString());
                                    logWrite("FileReceiver port : " + iep.Port.ToString());
                                    if (sendfilesize >= 40960.0)
                                        buffer = new byte[40960];
                                    else buffer = new byte[Convert.ToInt32(sendfilesize)];
                                    read = bs.Read(buffer, 0, buffer.Length);
                                    filesendSock.Send(buffer, buffer.Length, iep);
                                    logWrite("filesendSock.Send() : " + i.ToString() + " ��° �õ�!");
                                }
                                catch (Exception e)
                                {
                                    logWrite("SendFile() BufferedStream.Read() ���� :" + e.ToString());
                                }
                                try
                                {
                                    re = filesendSock.Receive(ref iep);
                                    int reSize = int.Parse(Encoding.UTF8.GetString(re));
                                    if (reSize == buffer.Length) break;
                                }
                                catch (SocketException e1)
                                {
                                    logWrite(e1.ToString());
                                }
                            }

                            if (re == null || re.Length == 0)
                            {
                                logWrite("filesendSock.Send() ������ �������� �ʽ��ϴ�. ������ ���� : " + iep.Address.ToString() + ":" + iep.Port.ToString());
                                MessageBox.Show("�������� ���� : ������ �������� ����", "���۽���", MessageBoxButtons.OK);
                                if (FileSendFormList.Count != 0 && FileSendFormList[info[2]] != null)
                                {
                                    Invoke(show, new object[] { "���۽���", form });
                                }
                                break;
                            }
                            else
                            {
                                sendfilesize = (sendfilesize - 40960.0);
                                total += percent;
                                if (total >= 100.0) total = 100.0;
                                string[] totalArray = (total.ToString()).Split('.');

                                if (!iep.Address.ToString().Equals(serverIP))
                                {
                                    if (FileSendFormList.Count != 0 && FileSendFormList[info[2]] != null)
                                    {
                                        Invoke(show, new object[] { "������(" + totalArray[0] + " %)", form });
                                    }
                                    if (FileSendDetailList.Count != 0 && FileSendDetailList[info[2]] != null)
                                    {
                                        string detailmsg = "������(" + totalArray[0] + " %)";
                                        Invoke(detail, new object[] { info[3], detailmsg, view });
                                    }
                                }
                            }
                            if (total == 100.0)
                            {
                                string detailmsg = "���ۿϷ�";
                                if (iep.Address.ToString().Equals(serverIP))
                                {
                                    Invoke(detail, new object[] { "server", detailmsg, view });
                                }
                                else
                                {
                                    Invoke(detail, new object[] { info[3], detailmsg, view });
                                }
                                bool isFinished = (bool)Invoke(close, view);  //�ڼ���
                                if (isFinished == false)
                                    Invoke(show, new object[] { detailmsg, form });  //FileSendForm
                                else
                                    Invoke(show, new object[] { "Finish", form });  //FileSendForm
                            }
                            if (total == 100.0)
                            {
                                bs.Close();
                                break;
                            }
                        }
                    }
                    try
                    {
                        if (FileSendThreadList.Count != 0 && FileSendThreadList[info[2] + "|" + info[3]] != null)
                        {
                            ((Thread)FileSendThreadList[info[2] + "|" + info[3]]).Abort();
                            logWrite("FileSendThread Abort()!");
                        }
                    }
                    catch (ThreadAbortException te)
                    {
                        logWrite(te.ToString());
                    }
                }
                else
                {
                    logWrite("SendFile() ������ ���� : " + info[1]);
                }
            }
            catch (IOException ioexception)
            {
                MessageBox.Show("������ ���� �������� �Դϴ�.\r\n ������ �ݰų� �б��������� ������ �ּ���", "���", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception exception)
            {
                logWrite(exception.ToString());
            }
        }