// Called when BeginReceive completes private static void ReceiveCallback(IAsyncResult ar) { TCPLineClient client = (TCPLineClient)ar.AsyncState; try { // Retrieve the state object and the client socket // from the asynchronous state object. Socket socket = client.socket; // Read data from the remote device. int bytesRead = socket.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. client.inData.Append(Encoding.ASCII.GetString(client.recvBuffer, 0, bytesRead)); // Signal that all bytes have been received. client.receiveInProgress = false; client.newData = true; } else { client.Close(); } } catch (Exception e) { client.Close(); } }
// Constructor public ProcessControls(TCPLineClient client, string name, int id, int xPos, int yPos) { this.client = client; this.id = id; this.StartMinimizedCheckbox.Location = new System.Drawing.Point(xPos, yPos + 65); this.StartMinimizedCheckbox.Name = "StartMinimizedCheckbox" + id; this.StartMinimizedCheckbox.Size = new System.Drawing.Size(98, 25); this.StartMinimizedCheckbox.TabIndex = 0; this.StartMinimizedCheckbox.Text = "Start minimized"; this.StartMinimizedCheckbox.UseVisualStyleBackColor = true; this.StartMinimizedCheckbox.Enabled = false; this.NameLabel.BackColor = System.Drawing.SystemColors.ControlDark; this.NameLabel.Location = new System.Drawing.Point(xPos, yPos); this.NameLabel.Size = new System.Drawing.Size(155, 60); this.NameLabel.TabIndex = 2; this.NameLabel.Text = name + "\n@" + client.name; this.NameLabel.Click += new System.EventHandler(this.NameLabel_Click); this.NameLabel.AutoSize = false; this.NameLabel.Font = new System.Drawing.Font("Arial", 16); this.NameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.NameLabel.BackColor = System.Drawing.Color.Gray; this.KillButton.Location = new System.Drawing.Point(xPos + 97, yPos + 65); this.KillButton.Name = "KillButton" + id; this.KillButton.Size = new System.Drawing.Size(59, 25); this.KillButton.TabIndex = 1; this.KillButton.Text = "Kill"; this.KillButton.UseVisualStyleBackColor = true; this.KillButton.Click += new System.EventHandler(this.KillButton_Click); this.KillButton.Enabled = false; }
// Called when send completes private static void SendCallback(IAsyncResult ar) { TCPLineClient client = (TCPLineClient)ar.AsyncState; try { // Retrieve the socket from the state object. Socket socket = client.socket; // Complete sending the data to the remote device. int bytesSent = socket.EndSend(ar); } catch (Exception e) { client.Close(); } }
// Called when connect completes private static void ConnectCallback(IAsyncResult ar) { TCPLineClient client = (TCPLineClient)ar.AsyncState; try { // Retrieve the socket from the state object. Socket socket = client.socket; // Complete the connection. socket.EndConnect(ar); // Signal that the connection has been made. client.socketConnected = true; } catch (Exception e) { client.Close(); } }