Exemplo n.º 1
0
 // Send 버튼 클릭시 TextWrite를 통해 서버로 메세지 전송.
 private void SendButton_Click(object sender, EventArgs e)
 {
     //자기닉네임을 결정하지 않았을 경우 MsgBox 출력
     if (clientNickname == null)
     {
         MessageBox.Show("You should input your id first", "Error");
     }
     else
     {
         //내용이 없을 경우 아무 반응 안함, 내용이 있을 경우 출력
         if (SendMsgTextbox.Text != null && !SendMsgTextbox.Text.Equals(""))
         {
             textWrite(SendMsgTextbox.Text, true, true);
             SendMsgTextbox.Clear();
         }
     }
 }
Exemplo n.º 2
0
        //ContentTextbox 에 입력텍스트를 출력하고 소켓을 통해 서버에 같은 메시지 전송
        private void textWrite(String text, Boolean printId = false, Boolean serverSend = false)
        {
            try
            {
                if (ContentTextbox.InvokeRequired)
                {
                    setTextCallback cb = new setTextCallback(textWrite);
                    Invoke(cb, new object[] { text, printId, serverSend });
                }
                else
                {
                    if (printId)
                    {
                        text = "[" + clientNickname + "] " + text;
                    }

                    if (clientStream != null && clientStream.CanWrite && serverSend)
                    {
                        byte[] sendMsg = new byte[BUFFERSIZE];
                        Encoding.UTF8.GetBytes(text).CopyTo(sendMsg, 0);

                        clientStream.Write(sendMsg, 0, sendMsg.Length);
                        clientStream.Flush();
                    }
                    else if (!serverSend)
                    {
                        //통신불안정으로 서버 브로드캐스팅을 받아오지 못하는 부분이 아닌 명시적으로 내부메세지로 사용하기 위함
                        ContentTextbox.AppendText(text + "\n");
                    }
                }
            }
            catch (SocketException)
            {
                ContentTextbox.AppendText("\n>> Socket Connection Failed Unexpectably! Please click Reconnect\n");
                SendMsgTextbox.Clear();
                CloseSocket();
                button1.Visible = true;
            }
            catch (IOException)
            {
                ContentTextbox.AppendText("\n>> Socket Connection Failed Unexpectably! Please click Reconnect\n");
                SendMsgTextbox.Clear();
                CloseSocket();
                button1.Visible = true;
            }
        }