public void broadcast(Data d) { foreach (ClientInfo clientInfo in clientList) { byte[] message = new byte[1024]; message.Initialize(); d.ToByte().CopyTo(message, 0); clientInfo.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), clientInfo.socket); } }
//send a Data public void send(Data d) { try { d.setIdNumber(localIdNumber);//sign the signature. byte[] message = new byte[1024]; message.Initialize(); d.ToByte().CopyTo(message, 0); clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), null); } catch (Exception ex) { } }
private void OnReceive(IAsyncResult ar) { byte[] msgBuffer = new byte[1024]; try { clientSocket.EndReceive(ar); byteData.CopyTo(msgBuffer, 0); Data dataRecved = new Data(msgBuffer); if (dataRecved.getDataType() != Data.DataType.AssignID) { serviceAgent.receiveDataHandler(msgBuffer); } else//assign id number { localIdNumber = dataRecved.getIdNumber(); } for (int i = 0; i < byteData.Length; i++) byteData[i] = 0xff; clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } catch (Exception ex) { MessageBox.Show(ex.Message, "Client.onReceive: " , MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void assignIdNumber() { Data idData = new Data(); idData.setDataType(Data.DataType.AssignID); }
public void send(Data d) { byte[] message=new byte[1024]; message.Initialize(); d.setIdNumber(activeIdNumber); d.ToByte().CopyTo(message, 0); foreach (ClientInfo c in clientList) { if (c.idNumber==d.getIdNumber() ||d.getIdNumber()==Data.IDNUMBER_BROADCAST) { c.socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnSend), c.socket); } } }