private void SendMyAnswer() { if (myNick != Allname[j]) { byte[] bytes = new byte[1024 * 4]; Answer answer = new Answer(); Stream stream = new NetworkStream(client.Client); answer.type = (int)CM_All.정답; if (txt_Answer.InvokeRequired) { txt_Answer.Invoke(new MethodInvoker(delegate() { answer.txtAnswer = txt_Answer.Text; })); } else { answer.txtAnswer = txt_Answer.Text; } answer.Nicname = myNick; bytes = CM_Library.Serialize(answer); stream.Write(bytes, 0, bytes.Length); stream.Flush(); txt_Answer.Text = ""; } }
void DoWork() //핸들러 쓰레드 { NetworkStream stream = null; try { int cli_num = dictionary_num[client]; while (true) { try { stream = client.GetStream(); int bytes = stream.Read(readbuffer, 0, readbuffer.Length); CM_Library library = (CM_Library)CM_Library.Deserialize(readbuffer); if (ClientEvent != null) { ClientEvent(library.type, readbuffer, client); } Set_byte(readbuffer); } catch (Exception) { } } } catch (Exception) { } }
private void btn_Log_Click(object sender, EventArgs e) { //--로그인 성공여부// string Path = "SELECT * FROM CM_User"; try { SqlConnection conn = new SqlConnection(); SqlCommand sqlComm = new SqlCommand(); string connectingsql = "Server = 127.0.0.1,1433; database = CM_data ; uid = qornwh ; pwd = qweasd12"; conn.ConnectionString = connectingsql; conn.Open(); sqlComm.CommandText = Path; sqlComm.Connection = conn; SqlDataReader sd = sqlComm.ExecuteReader(); while (sd.Read()) { if (sd["CM_id"].ToString() == textBox1.Text && sd["CM_pw"].ToString() == textBox2.Text) { Success = true; break; } } } catch (Exception) { Console.WriteLine("오류발생"); } finally { if (Success == true) { string ip = "127.0.0.1"; client = new TcpClient(); client.Connect(ip, 9876); Stream stream = new NetworkStream(client.Client); byte[] bytes = new byte[1024 * 4]; Login login = new Login(); login.type = (int)CM_All.닉네임; login.id = textBox1.Text; login.pw = textBox2.Text; bytes = CM_Library.Serialize(login); stream = client.GetStream(); stream.Write(bytes, 0, bytes.Length); stream.Flush(); CM_Ready ready = new CM_Ready(client, login.id); this.Hide(); ready.ShowDialog(); this.Close(); } else { MessageBox.Show("로그인 실패^^"); } } }
private void label6_Click(object sender, EventArgs e) { if (on_off == true) { this.pictureBox1.Image = null; byte[] bytes = new byte[1024 * 4]; resetImage resetimage = new resetImage(); resetimage.type = (int)CM_All.그림초기화; bytes = CM_Library.Serialize(resetimage); Stream stream_rs = new NetworkStream(client.Client); stream_rs.Write(bytes, 0, bytes.Length); stream_rs.Flush(); } }
private void btn_MSend_Click(object sender, EventArgs e) { byte[] bytes = new byte[1024 * 4]; Library.Message message = new Library.Message(); message.type = (int)CM_All.메시지; message.MMS = myNick + " : " + this.txt_Chat.Text; bytes = CM_Library.Serialize(message); Stream stream = new NetworkStream(client.Client); stream.Write(bytes, 0, bytes.Length); this.txt_Chat.Text = ""; stream.Flush(); Set_byte(bytes); }//메시지를 보내는거 구현
private void SendMyTurn() { if (myNick == Allname[j]) { byte[] bytes = new byte[1024 * 4]; on_off = true; Stream stream = new NetworkStream(client.Client); Turn turn = new Turn(); turn.type = (int)CM_All.턴; bytes = CM_Library.Serialize(turn); stream.Write(bytes, 0, bytes.Length); stream.Flush(); groupBox1.Enabled = true; } }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (on_off == true && e.Button == MouseButtons.Left) { point.X = e.X; point.Y = e.Y; byte[] bytes = new byte[1024 * 4]; GPoint gPoint = new GPoint(); gPoint.type = (int)CM_All.처음좌표; gPoint.X = e.X; gPoint.Y = e.Y; gPoint.gColor = pen_num; gPoint.gWidth = (int)pen.Width; bytes = CM_Library.Serialize(gPoint); Stream stream = new NetworkStream(client.Client); stream.Write(bytes, 0, bytes.Length); stream.Flush(); Set_byte(bytes); } }
private void btn_Start_Click(object sender, EventArgs e) { if (on_off == false) { Ready_Off ready_Off = new Ready_Off(); byte[] bytes = new byte[1024 * 4]; ready_Off.On = true; ready_Off.type = (int)CM_All.레디; Stream stream = new NetworkStream(client.Client); bytes = CM_Library.Serialize(ready_Off); stream.Write(bytes, 0, bytes.Length); stream.Flush(); on_off = true; //ReadyToGame(); thread1 = new Thread(new ThreadStart(ReadyToGame)); thread1.Start(); } }
private void Ready_Request() { while (true) { byte[] bytes = new byte[1024 * 4]; Stream stream = new NetworkStream(client.Client); int len = stream.Read(bytes, 0, bytes.Length); CM_Library Two = (CM_Library)CM_Library.Deserialize(bytes); stream.Flush(); switch (Two.type) { case (int)CM_All.클라이언트라벨: { Ready ready = new Ready(); ready = (Ready)CM_Library.Deserialize(bytes); for (int i = 0; i < ready.nickNameList.Length; i++) { if (ready.nickNameList[i] != null) { this.labels[i].Text = ready.nickNameList[i].ToString(); } } break; } case (int)CM_All.자기번호: { Ready_On ready_On = new Ready_On(); ready_On = (Ready_On)CM_Library.Deserialize(bytes); for (int i = 0; i < 4; i++) { if (ready_On.On[i] == true) { labels[i].BackColor = Color.Blue; } } break; } } } }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (on_off == true && e.Button == MouseButtons.Left) { byte[] bytes = new byte[1024 * 4]; gh = pictureBox1.CreateGraphics(); gh.DrawLine(pen, point, new Point(e.X - 1, e.Y - 1)); GPoint gPoint = new GPoint(); gPoint.type = (int)CM_All.좌표; gPoint.X = point.X; gPoint.Y = point.Y; //gPoint.gColor = pen_num; //gPoint.gWidth = (int)pen.Width; bytes = CM_Library.Serialize(gPoint); Stream stream = new NetworkStream(client.Client); stream.Write(bytes, 0, bytes.Length); stream.Flush(); Set_byte(bytes); point.X = e.X; point.Y = e.Y; } }
//-------------스트림(타입에 따른)을 받아오는 쓰레드 핸들러 Request 구현 해야한다네 (메시지, 좌표, 턴, 정답 등)------------------------------// private void Request() { SendMyTurn(); while (true) { byte[] bytes = new byte[1024 * 4]; Stream stream = new NetworkStream(client.Client); int len = stream.Read(bytes, 0, bytes.Length); CM_Library Two = (CM_Library)CM_Library.Deserialize(bytes); stream.Flush(); switch (Two.type) { case (int)CM_All.메시지: { Library.Message message = (Library.Message)CM_Library.Deserialize(bytes); //메시지 출력 구현해야함 this.txt_Mul.Invoke(new MethodInvoker(delegate() { txt_Mul.Text += message.MMS + "\r\n"; })); Set_byte(bytes); break; } case (int)CM_All.좌표: { GPoint gPoint = (GPoint)CM_Library.Deserialize(bytes); //좌표찍는거 구현해야함 gh = pictureBox1.CreateGraphics(); //sel_Color(pen, gPoint.gColor); //pen.Width = gPoint.gWidth; gh.DrawLine(pen, point.X, point.Y, gPoint.X, gPoint.Y); point.X = gPoint.X; point.Y = gPoint.Y; Set_byte(bytes); break; } case (int)CM_All.문제: { Problem problem = (Problem)CM_Library.Deserialize(bytes); //문제받기 this.Invoke(new MethodInvoker(delegate() { txt_problem.Text = problem.problem; })); if (this.CM_My.InvokeRequired) { this.CM_My.Invoke(new MethodInvoker(delegate() { CM_My.Text = "MY TURN"; CM_My.BackColor = Color.Lime; })); } else { CM_My.Text = "MY TURN"; CM_My.BackColor = Color.Lime; } Set_byte(bytes); break; } case (int)CM_All.점수: { Upoint upoint = (Upoint)CM_Library.Deserialize(bytes); User_Point[upoint.Nic] = upoint.point; Set_byte(bytes); if (j == 4) { Ranking(); } break; } case (int)CM_All.처음좌표: { GPoint gPoint = (GPoint)CM_Library.Deserialize(bytes); //좌표찍는거 구현해야함 gh = pictureBox1.CreateGraphics(); sel_Color(pen, gPoint.gColor); pen.Width = gPoint.gWidth; point.X = gPoint.X; point.Y = gPoint.Y; Set_byte(bytes); break; } case (int)CM_All.그림초기화: { pictureBox1.Image = null; Set_byte(bytes); break; } } } }
public void Request(int Type, byte[] bytes, TcpClient client) //Type에 따라 달라지는 직렬화 { switch (Type) { case (int)CM_All.메시지: { Library.Message message = new Library.Message(); message = (Library.Message)CM_Library.Deserialize(bytes); DisplayText(message.MMS); //Set_byte(bytes); //bytes = CM_Library.Serialize(message); //모든 클라이언트에 보내주는 메소드 만들어야함 for (int i = 0; i < count; i++) { Stream stream4 = new NetworkStream(tcpClients[i].Client); stream4.Write(bytes, 0, bytes.Length); stream4.Flush(); } break; } case (int)CM_All.레디: { Ready_Off ready_Off = new Ready_Off(); ready_Off = (Ready_Off)CM_Library.Deserialize(bytes); on.On[Number_D[client]] = true; on.type = (int)CM_All.자기번호; Set_byte(bytes); bytes = CM_Library.Serialize(on); for (int i = 0; i < count; i++) { Stream stream3 = new NetworkStream(tcpClients[i].Client); stream3.Write(bytes, 0, bytes.Length); stream3.Flush(); } break; } case (int)CM_All.닉네임: { Login login = new Login(); login = (Login)CM_Library.Deserialize(bytes); //login_D.Add(dictionary, login); User_point.Add(login.id, 0); Nick.Add(login.id); Set_byte(bytes); Ready ready = new Ready(); ready.type = (int)CM_All.클라이언트라벨; for (int i = 0; i < count; i++) { ready.nickNameList[i] = Nick[i].ToString(); } bytes = CM_Library.Serialize(ready); for (int i = 0; i < count; i++) { Stream stream2 = new NetworkStream(tcpClients[i].Client); stream2.Write(bytes, 0, bytes.Length); stream2.Flush(); } break; } case (int)CM_All.좌표: { //GPoint gPoint = new GPoint(); //gPoint = (GPoint)CM_Library.Deserialize(bytes); for (int i = 0; i < count; i++) { if (i != ti_time) { Stream stream1 = new NetworkStream(tcpClients[i].Client); stream1.Write(bytes, 0, bytes.Length); stream1.Flush(); } } break; } case (int)CM_All.턴: { ++ti_time; if (ti_time == 0) { Library.Message mms = new Library.Message(); mms.type = (int)CM_All.메시지; mms.MMS = "게임시작"; byte[] buffer = new byte[1024 * 4]; buffer = CM_Library.Serialize(mms); foreach (var cl in tcpClients) { Stream stream_2 = new NetworkStream(cl.Client); stream_2.Write(buffer, 0, buffer.Length); stream_2.Flush(); } } Set_byte(bytes); Problem problem = new Problem(); problem.type = (int)CM_All.문제; problem.problem = Allproblem[ti_time].ToString(); bytes = CM_Library.Serialize(problem); Stream stream0 = new NetworkStream(tcpClients[ti_time].Client); stream0.Write(bytes, 0, bytes.Length); stream0.Flush(); break; } case (int)CM_All.정답: { /*클라이언트의 답과 제시한 문제의 답과 같을 경우 점수를 주고 마지막 폼4번에서 점수를 출력해준다. <- 만들어야한다*/ Answer answer = new Answer(); answer = (Answer)CM_Library.Deserialize(bytes); if (Allproblem[ti_time] == answer.txtAnswer) { User_point[answer.Nicname] += 10; DisplayText(answer.Nicname + " : " + User_point[answer.Nicname].ToString()); } //모두에게 각자의 점수를 보내준다 Upoint upoint = new Upoint(); upoint.Nic = answer.Nicname; upoint.point = User_point[answer.Nicname]; upoint.type = (int)CM_All.점수; Set_byte(bytes); bytes = CM_Library.Serialize(upoint); for (int i = 0; i < count; i++) { Stream stream_1 = new NetworkStream(tcpClients[i].Client); stream_1.Write(bytes, 0, bytes.Length); stream_1.Flush(); } break; } case (int)CM_All.처음좌표: { for (int i = 0; i < count; i++) { if (i != ti_time) { Stream stream1 = new NetworkStream(tcpClients[i].Client); stream1.Write(bytes, 0, bytes.Length); stream1.Flush(); } } break; } case (int)CM_All.그림초기화: { for (int i = 0; i < count; i++) { if (i != ti_time) { Stream stream_2 = new NetworkStream(tcpClients[i].Client); stream_2.Write(bytes, 0, bytes.Length); stream_2.Flush(); } } break; } } }