private void sendMessage(object sender, messageReceptionEventArgs eveArgs) { string t = null; if (eveArgs.Disconnection) { foreach (DictionaryEntry item in socketArray) { if (eveArgs.theSocket == item.Value) { t = (string)item.Key; } } if (t != null) { socketArray.Remove(t); clientConnectionEventArgs sd = new clientConnectionEventArgs(t, true); clientConnected(this, sd); } } else { Socket s; byte[] id = new byte[10]; byte[] msg = new byte[1014]; for (int i = 0; i < 10; ++i) if (eveArgs.message[i] != 0) id[i] = eveArgs.message[i]; for (int j = 10, k = 0; j < 1014; ++j, ++k) if (eveArgs.message[j] != 0) msg[k] = eveArgs.message[j]; s = (Socket)socketArray[Encoding.ASCII.GetString(id, 0, id.Length)]; if (s != null) //if the destination user is online s.Send(eveArgs.message); } }
private void showIDs(object sender,clientConnectionEventArgs e) { if(e.Connected) { listBox1.Items.Remove(e.ID); } else { if (listBox1.Items.Contains(e.ID) == false) { //listBox1.Items.Add(e.ID); if (this.listBox1.InvokeRequired) { // It's on a different thread, so we use Invoke. SetTextCallback d = new SetTextCallback(showIDs); this.Invoke(d, this, e); // new object[] { text + " (Invoke)" } } else { // It's on the same thread, no need for Invoke this.listBox1.Items.Add(e.ID); } } } }
public void connectClients() { int a = 1; byte[] IDpassword; byte[] idB; byte[] passB; string id; string pass; bool check = false; ipep = new IPEndPoint(IPAddress.Any, m_port); sock1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { sock1.Bind(ipep); } catch (SocketException op) { MessageBox.Show(op.Message); } for (; ; ) { IDpassword = new byte[20]; idB = new byte[10]; passB = new byte[10]; sock1.Listen(10); //The argument is the maximum length of the pending connections queue sock2 = sock1.Accept(); a = sock2.Receive(IDpassword); if (a == 0) { sock2.Shutdown(SocketShutdown.Both); sock2.Close(); continue; } check = checkIDpassword(IDpassword); if (check == false) { byte[] y = new byte[14]; Encoding.ASCII.GetBytes("wrong password", 0, 14, y, 0); sock2.Send(y); sock2.Close(); continue; } else { int i = 0, n = 0, eachItem = 0; bool flag = false; string temp; byte[] x = new byte[1100]; foreach (string item in IDs) if (item != null) { temp = item.TrimEnd(); flag = true; n = eachItem; Encoding.ASCII.GetBytes(temp, 0, temp.Length, x, n); n += temp.Length; i = 10 - (temp.Length); for (int w = 0; w < i; ++w) { Encoding.ASCII.GetBytes("\0", 0, 1, x, n); ++n; } if (isOnline(temp)) { Encoding.ASCII.GetBytes("$", 0, 1, x, n); } else { Encoding.ASCII.GetBytes("%", 0, 1, x, n); } eachItem += 11; } else { if (flag == false) Encoding.ASCII.GetBytes("welcome", 0, 7, x, 0); } int p = sock2.Send(x); if (p == 0) MessageBox.Show("Agreement was not Sent!"); for (int b = 0; b < 10; ++b) idB[b] = IDpassword[b]; clientConnectionEventArgs ea = new clientConnectionEventArgs(idB); clientConnected(this, ea); //vaghti client be server vasl mishavad yek baste //be toole 20 byte havie id va pass miferestad //ke dar code zir be 2 ghesmate id va pass //joda misgavad //Fires an event to notify the User of the class //that a client is connected to the server //generating the ID and password //from the received BYTE array id = Encoding.ASCII.GetString(idB, 0, idB.Length); try { socketArray.Add(id, sock2); } catch (ArgumentException m) { //eventlog , error } //notifying all the users , the Connection of the new user notify(); receivingThread rt = new receivingThread(); rt.sock = sock2; rt.Disconnection += new disconnectionHandler(rt_Disconnection); rt.messageReceived += new messageReceptionHandler(sendMessage); clientThread = new Thread(new ThreadStart(rt.receiving)); clientThreads.Add(clientThread); clientThread.Start(); } } }