private void getClients() { try { while (hostUp) { UdpClient uc = new UdpClient(3030); uc.EnableBroadcast = true; IPEndPoint end = new IPEndPoint(IPAddress.Any, 3040); string s = Encoding.UTF8.GetString(uc.Receive(ref end)); uc.Close(); Client c = new Client(end); Clients.Add(c); c.ID = Clients.IndexOf(c)+1; c.Connected += new EventHandler<ConnectedEventArgs>(c_Connected); c.ExceptionOccured += new EventHandler<ClientExceptionEventArgs>(c_ExceptionOccured); c.ConnectAsync(); } } catch { } }
private void listenForClient() { while (online) { TcpListener list = new TcpListener(IPAddress.Any, 3031); list.Start(); Client c = new Client(list.AcceptTcpClient()); if (Peers == null) Peers = new List<Client>(); Peers.Add(c); Console.WriteLine("weer een connectie"); c.NewDataAvailable +=new EventHandler<DataAvailableEventArgs>(c_NewDataAvailable); c.ExceptionOccured+=new EventHandler<ClientExceptionEventArgs>(c_ExceptionOccured); list.Stop(); } }
private void listenForHost() { try { TcpListener list = new TcpListener(IPAddress.Any, 3031); list.Start(); TcpClient tc = list.AcceptTcpClient(); hostClient = new Client(tc); remoteHostFound = true; hostClient.NewDataAvailable += new EventHandler<DataAvailableEventArgs>(hostClient_NewDataAvailable); hostClient.ExceptionOccured += new EventHandler<ClientExceptionEventArgs>(hostClient_ExceptionOccured); list.Stop(); Thread ClientListenThread = new Thread(listenForClient); ClientListenThread.IsBackground = true; ClientListenThread.Start(); } catch { } }
void hostClient_NewDataAvailable(object sender, DataAvailableEventArgs e) { Client h = (Client)sender; string s = h.RecieveString(); if (!string.IsNullOrWhiteSpace(s)) { if (!s.ToLower().Contains("succes")) { try { Console.WriteLine(s); Data.DataGram d = Data.DataGram.Deserialize(s); if (d.InitData != null) { if (d.InitData.ConnectIpAdresses != null) { foreach (Data.ClientData sr in d.InitData.ConnectIpAdresses) { Console.WriteLine(sr.IpAdress); IPAddress ip = IPAddress.Parse(sr.IpAdress); Client c = new Client(new IPEndPoint(ip, 3031)); c.ID = sr.ID; c.ExceptionOccured += new EventHandler<ClientExceptionEventArgs>(c_ExceptionOccured); c.Connected += new EventHandler<ConnectedEventArgs>(c_Connected); c.NewDataAvailable += new EventHandler<DataAvailableEventArgs>(c_NewDataAvailable); c.ConnectAsync(); if (Peers == null) Peers = new List<Client>(); Peers.Add(c); } hostClient.NewDataAvailable -= hostClient_NewDataAvailable; hostClient.ExceptionOccured -= hostClient_ExceptionOccured; if (Peers == null) Peers = new List<Client>(); Peers.Add(hostClient); hostClient.NewDataAvailable += new EventHandler<DataAvailableEventArgs>(c_NewDataAvailable); hostClient.ExceptionOccured += new EventHandler<ClientExceptionEventArgs>(c_ExceptionOccured); hostClient.ID = 0; } else if (d.InitData.ClientID != null) { this.ID = d.InitData.ClientID; if (DataRevieved!=null) DataRevieved(this, new NewDataEventArgs() { Data = "connected with id: " + ID.ToString() }); } } } catch { if (DataRevieved != null) { Console.WriteLine("error sending"); DataRevieved(this, new NewDataEventArgs() { Data = s }); } } } } }