Exemple #1
0
        public void Connect(string ip, int port)
        {
            try
            {
                // Read the IP address
                IPAddress serverIpAddress;
                if (!IPAddress.TryParse(ip, out serverIpAddress))
                {
                    Console.WriteLine("Invalid IP address: " + ip);
                    return;
                }

                // Begin connecting to the remote IP
                ClientSocket = new SimpleClientTcpSocket();
                ClientSocket.ConnectCompleted += ClientSocket_ConnectCompleted;
                ClientSocket.PacketArrived += ClientSocket_PacketArrived;
                ClientSocket.WriteCompleted += args => ClientSocket_WriteCompleted(ClientSocket, args);
                ClientSocket.ShutdownCompleted += ClientSocket_ShutdownCompleted;
                ClientSocket.ConnectAsync(serverIpAddress, port);
                ClientSocketState = SocketState.Connecting;
                Console.WriteLine("Connecting socket to " + (new IPEndPoint(serverIpAddress, port)));
            }
            catch (Exception ex)
            {
                ResetSocket();
                Console.WriteLine("Error creating connecting socket: [" + ex.GetType().Name + "] " + ex.Message);
            }
            finally
            {
                RefreshDisplay();
            }
        }
Exemple #2
0
        /// <summary>
        /// Closes and clears the socket, without causing exceptions.
        /// </summary>
        protected void ResetSocket()
        {
            // Close the socket
            ClientSocket.Close();
            ClientSocket = null;

            // Indicate there is no socket connection
            ClientSocketState = SocketState.Closed;
        }
Exemple #3
0
 public void CloseAbortively()
 {
     try
     {
         ClientSocket.AbortiveClose();
         ClientSocket = null;
         ClientSocketState = SocketState.Closed;
         Console.WriteLine("Abortively closed socket");
     }
     catch (Exception ex)
     {
         ResetSocket();
         Console.WriteLine("Error aborting socket: [" + ex.GetType().Name + "] " + ex.Message);
     }
     finally
     {
         RefreshDisplay();
     }
 }
Exemple #4
0
 private void buttonAbortiveClose_Click(object sender, EventArgs e)
 {
     try
     {
         ClientSocket.AbortiveClose();
         ClientSocket      = null;
         ClientSocketState = SocketState.Closed;
         textBoxLog.AppendText("Abortively closed socket" + Environment.NewLine);
     }
     catch (Exception ex)
     {
         ResetSocket();
         textBoxLog.AppendText("Error aborting socket: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
     }
     finally
     {
         RefreshDisplay();
     }
 }
Exemple #5
0
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                // Read the IP address
                IPAddress serverIPAddress;
                if (!IPAddress.TryParse(textBoxIPAddress.Text, out serverIPAddress))
                {
                    MessageBox.Show("Invalid IP address: " + textBoxIPAddress.Text);
                    textBoxIPAddress.Focus();
                    return;
                }

                // Read the port number
                int port;
                if (!int.TryParse(textBoxPort.Text, out port))
                {
                    MessageBox.Show("Invalid port number: " + textBoxPort.Text);
                    textBoxPort.Focus();
                    return;
                }

                // Begin connecting to the remote IP
                ClientSocket = new SimpleClientTcpSocket();
                ClientSocket.ConnectCompleted  += ClientSocket_ConnectCompleted;
                ClientSocket.PacketArrived     += ClientSocket_PacketArrived;
                ClientSocket.WriteCompleted    += (args) => ClientSocket_WriteCompleted(ClientSocket, args);
                ClientSocket.ShutdownCompleted += ClientSocket_ShutdownCompleted;
                ClientSocket.ConnectAsync(serverIPAddress, port);
                ClientSocketState = SocketState.Connecting;
                textBoxLog.AppendText("Connecting socket to " + (new IPEndPoint(serverIPAddress, port)).ToString() + Environment.NewLine);
            }
            catch (Exception ex)
            {
                ResetSocket();
                textBoxLog.AppendText("Error creating connecting socket: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
            }
            finally
            {
                RefreshDisplay();
            }
        }