예제 #1
0
        // We call this method once to start the server if it is not started
        public bool Start(string cmdstring)
        {
            Exceptionthrown = false;

            // First create a generic socket
            if (CreateSocket(cmdstring))
            {
                try
                {
                    // Now make it a listener socket at the IP address and port that we specified
                    Connectionsocket.Bind(Connectionendpoint);

                    // Now start listening on the listener socket and wait for asynchronous client connections
                    Connectionsocket.Listen(DefaultMaxConnections);
                    StartAcceptAsync(null);
                    _mutex.WaitOne();
                    return(true);
                }
                catch (Exception ex)
                {
                    Exceptionthrown = true;
                    Lasterror       = ex.ToString();
                    return(false);
                }
            }
            Lasterror = "Unknown Error in Server Start.";
            return(false);
        }
예제 #2
0
        // This method implements the asynchronous loop of events
        // that accepts incoming client connections
        public void StartAcceptAsync(SocketAsyncEventArgs acceptEventArg)
        {
            // If there is not an accept socket, create it
            // If there is, reuse it
            if (acceptEventArg == null)
            {
                acceptEventArg            = new SocketAsyncEventArgs();
                acceptEventArg.Completed += OnAcceptCompleted;
            }
            else
            {
                acceptEventArg.AcceptSocket = null;
            }

            // this will return true if there is a connection
            // waiting to be processed (IO Pending)
            var acceptpending = Connectionsocket.AcceptAsync(acceptEventArg);

            // If not, we can go ahead and process the accept.
            // Otherwise, the Completed event we tacked onto the accept socket will do it when it completes
            if (!acceptpending)
            {
                ProcessAccept(acceptEventArg);
            }
        }
예제 #3
0
        // This method connects us to the server.
        // Winsock is very optimistic about connecting to the server.
        // It will not tell you, for instance, if the server actually accepted the connection.  It assumes that it did.
        public bool Connect(string cmdstring)
        {
            Exceptionthrown = false;

            if (!CreateSocket(cmdstring))
            {
                return(false);
            }
            try
            {
                var parameters = OsUtil.ParseParams(cmdstring);
                if (parameters.Count > 1)
                {
                    // This will succeed as long as some server is listening on this IP and port
                    var connectendpoint = CreateIpEndPoint(parameters[0], Convert.ToInt32(parameters[1]));
                    Connectionsocket.Connect(Connectionendpoint);
                    return(true);
                }
                Lasterror = "Server and Port not specified on client connection.";
                return(false);
            }
            catch (Exception ex)
            {
                Exceptionthrown = true;
                Lasterror       = ex.ToString();
                return(false);
            }
        }
예제 #4
0
        // This method is used to send a message to the server
        public bool Send(string cmdstring)
        {
            Exceptionthrown = false;

            var parameters = OsUtil.ParseParams(cmdstring);

            if (parameters.Count > 0)
            {
                try
                {
                    // We need a connection to the server to send a message
                    if (!Connectionsocket.Connected)
                    {
                        return(false);
                    }
                    var byData = Encoding.ASCII.GetBytes(parameters[0]);
                    Connectionsocket.Send(byData);
                    return(true);
                }
                catch (Exception ex)
                {
                    Lasterror = ex.ToString();
                    return(false);
                }
            }
            Lasterror = "No message provided for Send.";
            return(false);
        }
예제 #5
0
 // This method disconnects us from the server
 public void DisConnect()
 {
     try
     {
         Connectionsocket.Dispose(); // Connectionsocket.Close();
     }
     catch
     {
         //nothing to do since connection is already closed
     }
 }
예제 #6
0
 // This method is called once to stop the server if it is started.
 // We could check for the open socket here
 // to stop some exception noise.
 public void Stop()
 {
     Connectionsocket.Dispose(); // Connectionsocket.Close();
     _mutex.ReleaseMutex();
 }