private void SendToClient(string m, Socket cl) { string s = m; byte[] buffer = Encoding.Unicode.GetBytes(s); this.p = new SocketPacket(cl); p.Buffer = buffer; cl.BeginSend(p.Buffer, 0, p.Buffer.Length, SocketFlags.None, new AsyncCallback(SendMessage), p); //if (cl.Connected) //{ // cl.BeginSend(p.Buffer, 0, p.Buffer.Length, SocketFlags.None,new AsyncCallback(SendMessage), p); // return; //} //msg = null;sendTo = null;from = null; //foreach (Socket client in this.clients) //{ // if (client.Connected&&client==cl) // { // client.Send(buffer); // //Console.WriteLine("msg send"); // break; // } //} }
private void SendMessage(IAsyncResult ar) { SocketPacket sp = (SocketPacket)ar.AsyncState; Console.WriteLine("Server send : " + Encoding.ASCII.GetString(sp.Buffer)); //sp.Client.BeginSend(p.Buffer, 0, p.Buffer.Length, SocketFlags.None, new AsyncCallback(SendMessage), sp); sp.Client.EndSend(ar); }
private string GetSenderName(SocketPacket p) { foreach (var item in clientList) { if (item.ClientData.LocalEndPoint.ToString() == p.Client.LocalEndPoint.ToString()) { // Console.WriteLine(item.ClientData.LocalEndPoint + " equal " + p.Client.LocalEndPoint+"..."+item.UData.Name); //return item.UData.Name.ToString(); } } return("user"); }
private void OnReceive(IAsyncResult ar) { SocketPacket p = (SocketPacket)ar.AsyncState; string msg = Encoding.Unicode.GetString(p.Buffer); //string[] s = msg.Split('#'); //string recfrom = s[1]; string name = GetSenderName(p); this.WriteToLog(msg, name); this.WaitingForData(p.Client); }
private void OnReceive(IAsyncResult ar) { SocketPacket packet = (SocketPacket)ar.AsyncState; // Console.WriteLine("on recienve: "+packet.Client.RemoteEndPoint); recvMsg = Encoding.Unicode.GetString(packet.Buffer); Console.WriteLine("Server receive: " + recvMsg); this.WriteToLog(recvMsg); ReadyForData(packet.Client); //packet.Client.EndReceive(ar); packet.Buffer = null; }
private void WaitingForData(Socket cl) { SocketPacket packet = new SocketPacket(cl); cl.BeginReceive(packet.Buffer, 0, packet.Buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), packet); }