예제 #1
0
        private void CloseConnectionCallback(IAsyncResult ar)
        {
            int sent = 0;

            try
            {
                sent = sender.EndSend(ar);
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
                return;
            }

            WriteLog(LogStrings.BytesWereSuccessfulySent(sent));
            WriteLog(LogStrings.CloseSocket());

            try
            {
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
            }

            senderSema.Release();
        }
예제 #2
0
        private void ConnectToServer()
        {
            IPHostEntry ipHostInfo = Dns.GetHostEntry(host);
            IPAddress   ipAddress  = ipHostInfo.AddressList[0];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddress, port);

            senderSema.Wait();
            sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            while (true)
            {
                try
                {
                    WriteLog(LogStrings.TryingToConnectToEndPoint(ipEndPoint));
                    sender.Connect(ipEndPoint);
                    WriteLog(LogStrings.ConnectedSuccessfullyToEndPoint(ipEndPoint));
                    senderSema.Release();
                    break;
                }
                catch (Exception exc)
                {
                    WriteLog(LogStrings.ExceptionWasThrown(exc));
                }
            }
        }
예제 #3
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workerSocket;

            string name     = GetSocketName(handler);
            int    bytesRec = 0;

            try
            {
                bytesRec = handler.EndReceive(ar);
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
                return;
            }

            WriteLog(LogStrings.BytesWereReceivedSuccessfuly(bytesRec, name));

            string msg = Encoding.UTF8.GetString(state.buffer, 0, bytesRec);

            WriteLog(LogStrings.MessageWasReceivedSuccessfuly(msg, name));

            if (msg.IndexOf(endMessage) != -1)
            {
                Invoke(new Method(() => clientsListBox.Items.Remove(clientsListBox.Items[clientsListBox.Items.IndexOf(name)])));

                WriteLog(LogStrings.ClosingSocket(name));

                RemoveClient(name);

                try
                {
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                } catch (Exception e)
                {
                    WriteLog(LogStrings.ExceptionWasThrown(e));
                }

                return;
            }

            string[] paths = msg.Split(';');

            string response = MultiplyVectorAndMatrix(paths[0], paths[1]);

            if (response == null)
            {
                response = invalidInputException;
            }

            byte[] bytes = Encoding.UTF8.GetBytes(response);

            WriteLog(LogStrings.AttemptingToSendMessageToClient(response, name));

            handler.BeginSend(bytes, 0, bytes.Length, 0, new AsyncCallback(SendCallback), handler);
        }
예제 #4
0
        private void SendCallback(IAsyncResult ar)
        {
            Socket handler = (Socket)ar.AsyncState;

            int bytesSent = 0;

            try
            {
                bytesSent = handler.EndSend(ar);
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
                return;
            }

            WriteLog(LogStrings.BytesWereSentSuccessfuly(bytesSent, GetSocketName(handler)));

            Receive(handler);
        }
예제 #5
0
        private void StartListening()
        {
            IPHostEntry ipHostEntry   = Dns.GetHostEntry(host);
            IPAddress   ipAddress     = ipHostEntry.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, port);

            listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                WriteLog(LogStrings.TryingToBindListenerToEndPoint(localEndPoint));
                listener.Bind(localEndPoint);
                WriteLog(LogStrings.BindingWasSuccessful(localEndPoint));
                listener.Listen(maxNumberOfListeners);
                WriteLog(LogStrings.StartingToListenConnections());
            } catch (Exception exc)
            {
                WriteLog(LogStrings.ExceptionWasThrown(exc));
            }
        }
예제 #6
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            StateObject state    = (StateObject)ar.AsyncState;
            Socket      handler  = state.workSocket;
            int         bytesRec = 0;

            try
            {
                bytesRec = handler.EndReceive(ar);
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
                return;
            }

            WriteLog(LogStrings.BytesWereReceivedFromServer(bytesRec));

            senderSema.Release();

            string path = Encoding.UTF8.GetString(state.buffer, 0, bytesRec);

            PrintResult(path);
        }
예제 #7
0
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = null;

            try
            {
                handler = listener.EndAccept(ar);
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
                return;
            }

            AddSocket(handler);
            string name = GetSocketName(handler);

            WriteLog(LogStrings.ClientConnectionWasAccepted(name));

            Invoke(new Method(() => clientsListBox.Items.Add(name)));

            Receive(handler);
        }
예제 #8
0
        private void SendCallback(IAsyncResult ar)
        {
            Socket sender    = (Socket)ar.AsyncState;
            int    bytesSent = 0;

            try
            {
                bytesSent = sender.EndSend(ar);
            } catch (Exception e)
            {
                WriteLog(LogStrings.ExceptionWasThrown(e));
                return;
            }

            WriteLog(LogStrings.BytesWereSuccessfulySent(bytesSent));

            StateObject state = new StateObject();

            state.workSocket = sender;

            WriteLog(LogStrings.WaitingToReceiveMessageFromServer());

            sender.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveCallback), state);
        }