public void NotifyNewOffline(Client cOffline, String nickname) { deleteClient(cOffline); foreach (Client client in clients) { client.Send("UserLeave:" + nickname); } }
static void client_Disconnected(Client sender) { Console.WriteLine("User hat sich ausgeloggt: " + sender.sck.RemoteEndPoint.ToString()); serverCmd.CurrClient = sender; serverCmd.LogOff(); sender.Close(); clients.Remove(sender); }
public void NewConnection(Socket clientSock) { // creo nuovo oggetto Client c = new Client(this, clientSock); // aggiungo alla lista di clienti connessi clients.Add(c); // ascolto per dati in entrata c.SetupReceiveCallback(); }
private void ReceiveData(IAsyncResult asyncResult) { try { byte[] data; // Initialise a packet object to store the received data Packet receivedData = new Packet(this.dataStream); // Initialise a packet object to store the data to be sent Packet sendData = new Packet(); // Initialise the IPEndPoint for the clients IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0); // Initialise the EndPoint for the clients EndPoint epSender = (EndPoint)clients; // Receive all data serverSocket.EndReceiveFrom(asyncResult, ref epSender); // Start populating the packet to be sent sendData.ChatDataIdentifier = receivedData.ChatDataIdentifier; sendData.ChatName = receivedData.ChatName; switch (receivedData.ChatDataIdentifier) { case DataIdentifier.Message: sendData.ChatMessage = string.Format("{0}: {1}", receivedData.ChatName, receivedData.ChatMessage); break; case DataIdentifier.LogIn: // Populate client object Client client = new Client(); client.endPoint = epSender; client.name = receivedData.ChatName; // Add client to list this.clientList.Add(client); sendData.ChatMessage = string.Format("-- {0} is online --", receivedData.ChatName); break; case DataIdentifier.LogOut: // Remove current client from list foreach (Client c in this.clientList) { if (c.endPoint.Equals(epSender)) { this.clientList.Remove(c); break; } } sendData.ChatMessage = string.Format("-- {0} has gone offline --", receivedData.ChatName); break; } // Get packet as byte array data = sendData.GetDataStream(); foreach (Client client in this.clientList) { if (client.endPoint != epSender || sendData.ChatDataIdentifier != DataIdentifier.LogIn) { // Broadcast to all logged on users serverSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, client.endPoint, new AsyncCallback(this.SendData), client.endPoint); } } // Listen for more connections again... serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(this.ReceiveData), epSender); // Update status through a delegate this.Invoke(this.updateStatusDelegate, new object[] { sendData.ChatMessage }); } catch (Exception ex) { MessageBox.Show("ReceiveData Error: " + ex.Message, "UDP Server", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public ChatProtocol(Client c) { this.c = c; }
private void ReceiveData(IAsyncResult asyncResult) { try { byte[] data; // Initialise a packet object to store the received data Packet receivedData = new Packet(this.dataStream); // Initialise a packet object to store the data to be sent Packet sendData = new Packet(); // Initialise the IPEndPoint for the clients IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0); // Initialise the EndPoint for the clients EndPoint epSender = (EndPoint)clients; // Receive all data serverSocket.EndReceiveFrom(asyncResult, ref epSender); // Start populating the packet to be sent sendData.ChatDataIdentifier = receivedData.ChatDataIdentifier; sendData.ChatName = receivedData.ChatName; switch (receivedData.ChatDataIdentifier) { case DataIdentifier.Message: sendData.ChatMessage = string.Format("{0}: {1}", receivedData.ChatName, receivedData.ChatMessage); break; case DataIdentifier.LogIn: // Populate client object Client client = new Client(); client.endPoint = epSender; client.name = receivedData.ChatName; // Add client to list this.clientList.Add(client); sendData.ChatMessage = string.Format("-- {0} is online --", receivedData.ChatName); Console.Write(sendData.ChatMessage); break; case DataIdentifier.LogOut: // Remove current client from list foreach (Client c in this.clientList) { if (c.endPoint.Equals(epSender)) { this.clientList.Remove(c); break; } } sendData.ChatMessage = string.Format("-- {0} has gone offline --", receivedData.ChatName); break; } // Get packet as byte array data = sendData.GetDataStream(); foreach (Client client in this.clientList) { if (client.endPoint != epSender || sendData.ChatDataIdentifier != DataIdentifier.LogIn) { // Broadcast to all logged on users serverSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, client.endPoint, new AsyncCallback(this.SendData), client.endPoint); } } // Listen for more connections again... serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(this.ReceiveData), epSender); // Update status through a delegate this.Invoke(this.updateStatusDelegate, new object[] { sendData.ChatMessage }); } catch (Exception ex) { MessageBox.Show("ReceiveData Error: " + ex.Message, "UDP Server", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private static void onConnect(Socket socket) { try{ SendText("/get login", socket); byte[] rec = new byte[500]; socket.Receive (rec); string login = Encoding.ASCII.GetString (rec); string[] tmp = login.Split(Character.ENDVARCHAR); string name = tmp[0]; string ip = tmp[1]; Client c = new Client (name, ip, socket); if (hasBeenSeen (c, true)) { SendToAll ("Welcome back, " + c.name); }else{ SendToAll("Welcome, " + c.name); fileSaver.saveToMain(c.name + "|" + c.remoteEnd + ";"); seen.Add(c); } }catch(SocketException s){ LogError (s); } }
private static bool hasBeenSeen(Client clnt, bool shouldUpdate) { foreach (Client c in seen) { if ((clnt.name == c.name) || (clnt.remoteEnd == c.remoteEnd)) { if(shouldUpdate){ c.name = clnt.name; c.remoteEnd = clnt.remoteEnd; c.socket = clnt.socket; } return true; } } return false; }
public void NotifyNewOnline(Client cOnline, String nickname) { foreach (Client client in clients) { if(client != cOnline) client.Send("NewUser:" + nickname); } }
public void deleteClient(Client c) { clients.Remove(c); }
public void ExecuteCmd(Client _currClient, byte[] _data) { string[] args=this.DecodeCmdStringArray(_data); this.CurrClient = _currClient; switch (CurrClientCommand) { case ClientCommand.connect: break; case ClientCommand.sendMsg: getMsg(args); break; case ClientCommand.disconnect: disconnectUser(); break; case ClientCommand.changeName: break; case ClientCommand.login: Login(args); break; case ClientCommand.getLatestMsgLog: this.sendLatestMsgLog(); break; case ClientCommand.getLoggedUserList: this.getLoggedUserList(); break; case ClientCommand.getAllUserList: break; case ClientCommand.setMyStatus: break; case ClientCommand.getMyStatus: break; case ClientCommand.kickUser: break; case ClientCommand.banUser: break; case ClientCommand.setUserRole: break; case ClientCommand.UnbanUser: break; case ClientCommand.sendPrivateMsg: break; case ClientCommand.changePwd: break; case ClientCommand.getServerInfo: SendServerInfo(); break; case ClientCommand.getServerReadme: break; default: break; } }
private void SendToClient(Client cl, string message) { try{ byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray()); cl.Sock.Send(buffer,buffer.Length,0); } catch(Exception e){ cl.Sock.Close(); cl.CLThread.Abort(); clients.Remove(cl); lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString()); //MessageBox.Show("Could not reach " + cl.Name + " - disconnected","Error", //MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void ServiceClient() { Socket client = clientsocket; bool keepalive = true; while (keepalive) { Byte[] buffer = new Byte[1024]; client.Receive(buffer); string clientcommand = System.Text.Encoding.ASCII.GetString(buffer); string[] tokens = clientcommand.Split(new Char[]{'|'}); Console.WriteLine(clientcommand); if (tokens[0] == "CONN") { for(int n=0; n<clients.Count; n++) { Client cl = (Client)clients[n]; SendToClient(cl, "JOIN|" + tokens[1]); } EndPoint ep = client.RemoteEndPoint; //string add = ep.ToString(); Client c = new Client(tokens[1], ep, clientservice, client); clients.Add(c); SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\School\Secure programmer\DataFiles\C#\VS7 Projects\ChatServer\ChatDatabase.mdf;Integrated Security=True;Connect Timeout=30"); MessageBox.Show("hallo " + tokens[1] + " Hallo"); //if (tokens[1] == "Miiuh") //{ // MessageBox.Show("YAY"); //} //else //{ // MessageBox.Show("Zandaap"); //} conn.Close(); string message = "LIST|" + GetChatterList() +"\r\n"; SendToClient(c, message); //lbClients.Items.Add(c.Name + " : " + c.Host.ToString()); lbClients.Items.Add(c); } if (tokens[0] == "CHAT") { for(int n=0; n<clients.Count; n++) { Client cl = (Client)clients[n]; SendToClient(cl, clientcommand); } } if (tokens[0] == "PRIV") { string destclient = tokens[3]; for(int n=0; n<clients.Count; n++) { Client cl = (Client)clients[n]; if(cl.Name.CompareTo(tokens[3]) == 0) SendToClient(cl, clientcommand); if(cl.Name.CompareTo(tokens[1]) == 0) SendToClient(cl, clientcommand); } } if (tokens[0] == "GONE") { int remove = 0; bool found = false; int c = clients.Count; for(int n=0; n<c; n++) { Client cl = (Client)clients[n]; SendToClient(cl, clientcommand); if(cl.Name.CompareTo(tokens[1]) == 0) { remove = n; found = true; lbClients.Items.Remove(cl); //lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString()); } } if(found) clients.RemoveAt(remove); client.Close(); keepalive = false; } } }