public static void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. AllDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); // Create the state object. StateObject state = new StateObject(); state.workSocket = handler; Client client = new Client(ClientController.GetClientListCount()); client.ConnectionTime = DateTime.Now; client.IP = Convert.ToString(IPAddress.Parse(((IPEndPoint)state.workSocket.RemoteEndPoint).Address.ToString())); client.Port = ((IPEndPoint)state.workSocket.RemoteEndPoint).Port; ClientController.AddClient(client); IPEndPoint clientEndPoint = (IPEndPoint)state.workSocket.RemoteEndPoint; Sistem.printF(string.Format("Client Connected: {0}", clientEndPoint.Address.ToString()), ConsoleColor.Green); handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }
private static void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket handler = (Socket)ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = handler.EndSend(ar); Sistem.printF(string.Format("Sent {0} bytes to client.", bytesSent), ConsoleColor.Gray); handler.Shutdown(SocketShutdown.Both); handler.Close(); } catch (Exception e) { Sistem.printF(e.ToString(), ConsoleColor.Red); } }
public static void ReadCallback(IAsyncResult ar) { string content = string.Empty; // Retrieve the state object and the handler socket from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); withrawBytes(ref buffer, state.buffer, bytesRead); // Check for end-of-file tag. If it is not there, read more data. content = state.sb.ToString(); if (content.Contains("<File.") || content.Contains("<SQL")) { // All the data has been read from the client. Display it on the console. bool result = InterpretData(buffer); if (result) { Sistem.printF(string.Format("Read {0} bytes from socket. \n", content.Length), ConsoleColor.Green); // Echo the data encrypted back to the client. Send(handler, buffer); } else { Send(handler, new byte[0]); } } else // Not all data received. Get more. { handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } } }
public static void StartListening() { // Establish the local endpoint for the socket. // The DNS name of the computer // running the listener is "host.contoso.com". IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Port); // Create a TCP/IP socket. Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(100); Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Async Socket Server ONLINE at " + ipAddress.ToString() + ":" + Port, ConsoleColor.Green); while (true) { // Set the event to nonsignaled state. AllDone.Reset(); // Start an asynchronous socket to listen for connections. Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Waiting for a connection...", ConsoleColor.Gray); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing. AllDone.WaitOne(); } } catch (Exception e) { Sistem.printF(e.ToString(), ConsoleColor.Red); } Sistem.printF("\nPress ENTER to continue...", ConsoleColor.Gray, false, false, true, true, true); }