Exemplo n.º 1
0
        // 'Connet' 버튼 클릭 시 서버 아이피와 연결 시도
        private void btn_Connect_Click(object sender, EventArgs e)
        {
            // 통신 초기화
            _mainSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (_mainSock.Connected)
            {
                lbox_Content.Items.Add("이미 연결됨");
                return;
            }

            int port;

            if (!int.TryParse(tbox_Port.Text, out port))
            {
                lbox_Content.Items.Add("포트번호잘못 또는 입력x");
                tbox_Port.Focus();
                tbox_Port.SelectAll();
                return;
            }

            try
            {
                _mainSock.Connect(tbox_ServerIP.Text, port);

                lbox_Content.Items.Add("서버와 연결성공");
                lbox_Content.Items.Add("메시지를 입력하세요");
                tbox_BackColor.BackColor = Color.Green;
                tbox_BackColor.Text      = "ON";

                _moveShapeSetting_Server.Clear(); //sortlist <string, Setting> server init()

                // 연결완료, 서버에서 데이터 올 수 있도록 수신 대기
                AsyncObject obj = new AsyncObject(4096, _mainSock.LocalEndPoint);
                obj._workingSocket = _mainSock;
                _mainSock.BeginReceive(obj._buffer, 0, obj._bufferSize, 0, DataReceived, obj);
            }
            catch (Exception ex)
            {
                lbox_ChatLog.Items.Add(ex.Message.ToString());
                _moveShapeSetting_Server.Clear(); //sortlist <string, Setting> server init()
                return;
            }
        }
Exemplo n.º 2
0
        // 데이터 받는 함수
        private void DataReceived(IAsyncResult ar)
        {
            // BeginReceive에서 추가로 온 데이터를 변환
            // AsyncCallback 메소드로 전달된 객체 반환
            AsyncObject obj = (AsyncObject)ar.AsyncState;

            // 데이터 수신 끝
            try
            {
                int received = obj._workingSocket.EndReceive(ar);
                // 받은 데이터 없으면 비동기 연결끝
                if (received <= 0)
                {
                    obj._workingSocket.Close();
                    return;
                }
                // 텍스트로 변환
                // 서버가 보낼 떄 : "192.168.145.1 : 3000 % t % www"
                // 클라가 보낼 때 : "192.168.2.157 : port : t : mms"
                string text = Encoding.Default.GetString(obj._buffer);

                string[] token = text.Split('%');

                string[] port    = token[0].Split(':');    //port or ip
                string   check   = token[1];
                string   message = token[2];

                if (check.Equals("p") || check.Equals("p@"))
                {
                    MessageConverter(message, check, port[1]);
                }

                else if (check.Equals("g"))
                {
                    Image image = Image.FromFile(message);
                    this.pictureBox1.Image = image;
                    image = Image.FromFile(message, true);
                }
                else
                {
                    //  if (ip == "192.168.2.157")
                    if (port[1].Equals("3000"))
                    {
                        if (lbox_ChatLog.InvokeRequired)
                        {
                            lbox_ChatLog.Invoke(new Action(delegate()
                            {
                                lbox_ChatLog.Items.Add("[server] : " + message);
                            }));
                        }
                        else
                        {
                            lbox_ChatLog.Items.Add("[server] : " + message);
                        }
                    }
                    else
                    {
                        if (lbox_ChatLog.InvokeRequired)
                        {
                            lbox_ChatLog.Invoke(new Action(delegate()
                            {
                                lbox_ChatLog.Items.Add("[클라이언트] : " + message);
                            }));
                        }
                        else
                        {
                            lbox_ChatLog.Items.Add("[클라이언트] : " + message);
                        }
                    }
                }
                // 데이터 받고 버퍼 비움
                obj.ClearBuffer();
                // 수신 대기
                obj._workingSocket.BeginReceive(obj._buffer, 0, 4096, 0, DataReceived, obj);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }