Пример #1
0
        // 포트 리스닝 시작
        private void Listen()
        {
            if (listenCheck)
            {
                return;
            }

            try
            {
                // 연결 허용
                receiveSocket.Listen(10);
                receiveSocket = receiveSocket.Accept();

                // 실제로 통신을 담당하는 쓰레드 생성
                listenThread = new Thread(SocketReceiveData);
                listenThread.Start();
                listenCheck = true;

                ConnectStateBtn.BackColor = Color.Green;
                ConnectStateBtn.Update();
            }
            catch (Exception ex)
            {
                ConnectStateBtn.BackColor = Color.Red;
                ConnectStateBtn.Update();

                MessageBox.Show(ex.Message);

                if (listenCheck)
                {
                    listenCheck = false;
                }
            }
        }
Пример #2
0
        // 서버에 연결
        private void TcpSocketConnect()
        {
            if (conCheck)
            {
                return;
            }

            try
            {
                // Ethernet 통신 정보 Update
                ServerIPName     = Config.GetInformation("SERVER", "ServerIP");
                Ethernet_PortNum = int.Parse(Config.GetInformation("SERVER", "Ethernet_Port"));

                // 소켓 객체 생성
                sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sendSocket.Connect(IPAddress.Parse(ServerIPName), Ethernet_PortNum);
                conCheck = true;

                ConnectStateBtn.BackColor = Color.Green;
                ConnectStateBtn.Update();
            }
            catch (Exception ex)
            {
                ConnectStateBtn.BackColor = Color.Red;
                ConnectStateBtn.Update();

                MessageBox.Show(ex.Message);
            }
        }
Пример #3
0
        // 소켓 오픈, 포트바인딩
        private void TcpSocketOpen()
        {
            try
            {
                // 소켓 객체 생성, IPAddress.Any: 아무 IP주소나 다 받음
                receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, Ethernet_PortNum);
                receiveSocket.Bind(endPoint);

                SendRateTbx.Text = "대기 중";
                SendRateTbx.Update();
            }
            catch (Exception ex)
            {
                ConnectStateBtn.BackColor = Color.Red;
                ConnectStateBtn.Update();

                MessageBox.Show(ex.Message);
            }
        }
Пример #4
0
        private void RecvSendBtn_Click(object sender, EventArgs e)
        {
            // 소켓통신
            if (Ethernet == true)
            {
                if (Master == true)
                {
                    TcpSocketOpen();
                    Listen();

                    ConnectStateBtn.BackColor = Color.Green;
                    ConnectStateBtn.Update();

                    MessageBox.Show("파일을 수신합니다.");
                }
                else if (Slave == true)
                {
                    ConnectStateBtn.BackColor = Color.Green;
                    ConnectStateBtn.Update();
                    try
                    {
                        int      persent;
                        FileInfo fi = new FileInfo(filePath);

                        byte[] fileNameSize     = BitConverter.GetBytes(fi.Name.Length);
                        byte[] fileName         = Encoding.UTF8.GetBytes(fi.Name);
                        byte[] fileSizeRealSize = File.ReadAllBytes(filePath);
                        byte[] file             = BitConverter.GetBytes(fileSizeRealSize.Length);
                        byte[] sendBuffer       = new byte[fileName.Length + fileNameSize.Length + file.Length + fileSizeRealSize.Length];

                        Buffer.BlockCopy(fileNameSize, 0, sendBuffer, 0, fileNameSize.Length);
                        Buffer.BlockCopy(fileName, 0, sendBuffer, fileNameSize.Length, fileName.Length);
                        Buffer.BlockCopy(file, 0, sendBuffer, fileNameSize.Length + fileName.Length, file.Length);
                        Buffer.BlockCopy(fileSizeRealSize, 0, sendBuffer, fileNameSize.Length + fileName.Length + file.Length, fileSizeRealSize.Length);

                        byte[] temp      = new byte[30000];
                        int    fullCount = sendBuffer.Length / 30000;
                        int    count     = 0;
                        SendRateTbx.Text = "송신 중";
                        SendRateTbx.Update();

                        if (fullCount != 0)
                        {
                            persent           = (count * 100) / fullCount;
                            SendRateBar.Value = persent;
                            SendRateBar.Update();
                        }
                        else
                        {
                            SendRateBar.Value = 100;
                            SendRateBar.Update();
                        }

                        // 큰 경우에 나눠서 전송
                        while (sendBuffer.Length > 30000)
                        {
                            count++;
                            persent           = (count * 100) / fullCount;
                            SendRateBar.Value = persent;
                            SendRateBar.Update();
                            Buffer.BlockCopy(sendBuffer, 0, temp, 0, temp.Length);
                            sendBuffer = ByteMove(sendBuffer, temp.Length, sendBuffer.Length - temp.Length);
                            sendSocket.Send(temp);
                        }
                        sendSocket.Send(sendBuffer);
                        SendRateTbx.Text = "송신 완료";
                        SendRateTbx.Update();
                    }
                    catch (Exception ex)
                    {
                        ConnectStateBtn.BackColor = Color.Red;
                        ConnectStateBtn.Update();

                        if (MessageBox.Show(ex.Message) == DialogResult.OK)
                        {
                            Application.Exit();
                        }
                    }
                }
            }

            // 시리얼통신
            if (RS232 == true)
            {
                if (Master == true)
                {
                    serialCommunication.MS = true;
                    serialCommunication.Start();
                }
                else if (Slave == true)
                {
                    // 실제로 보내는 부분
                    try
                    {
                        serialCommunication.MS = false;
                        serialCommunication.Start();
                        serialPort1.Write(ByteSendData, 0, TotalSize);
                    }
                    finally
                    {
                        SendRateTbx.Text = "송신 완료";
                    }
                }
            }
        }