/// <summary>AcceptCallback método da classe Server /// Evento realizado para aceitar conexões dos clientes adicionando em uma lista genérica /// /// </summary> private void AcceptCallback(IAsyncResult asyncronousResult) { Socket socket; try { if (ListaDeClientesSockets.Count <= 8) { socket = _serverSocket.EndAccept(asyncronousResult); ListaDeClientesSockets.Add(socket); socket.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket); Console.WriteLine("Cliente Conectado " + socket.RemoteEndPoint.ToString()); _serverSocket.BeginAccept(AcceptCallback, null); string idCorr = funcoes.idSocket(socket.RemoteEndPoint.ToString()); byte[] data = Encoding.UTF8.GetBytes(idCorr); socket.Send(data); jogo.insereJogador(socket.RemoteEndPoint.ToString()); } } catch (ObjectDisposedException) { return; } catch (Exception) { return; } }
public void insereJogador(string endPoint) { Boolean inserido = false; int id = Convert.ToInt32(funcoes.idSocket(endPoint)); string ip = endPoint.Replace(":" + id.ToString(), ""); for (int i = 0; i < jogadores.Length; i++) { if (!inserido) { if (!jogadores[i].conectado) { jogadores[i].ID = id; jogadores[i].ip = ip; jogadores[i].conectado = true; inserido = true; } } } }
private void b_jogar_Click(object sender, EventArgs e) { timer1.Enabled = false; Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "JOGAR"; string id = funcoes.idSocket(socket.RemoteEndPoint.ToString()); int idInt = Convert.ToInt32(id); comando.jogadores[0].ID = this.ID; byte[] jogar = this.funcoes.ObjectToByteArray(comando); socket.Send(jogar); timer1.Enabled = true; }