public void Data()
        {
            IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress   = ipHostEntry.AddressList[0];
            IPEndPoint  ipEndPoint  = new IPEndPoint(ipAddress, 4747);

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

            try
            {
                socketLisener.Bind(ipEndPoint);
                socketLisener.Listen(10);

                while (true)
                {
                    Console.WriteLine("Waiting for data input ...");

                    Socket clientSocket = socketLisener.Accept();

                    //Data buffer
                    byte[] bytes = new Byte[4096];
                    string data  = null;

                    //Geting data from server
                    while (true)
                    {
                        int amountOfBytesReseved = clientSocket.Receive(bytes);
                        data += Encoding.Unicode.GetString(bytes, 0, amountOfBytesReseved);
                        if (data.IndexOf("<EOF>", StringComparison.Ordinal) > -1)
                        {
                            break;
                        }
                    }

                    string   serialisedXml = data.Substring(0, data.Length - 5);
                    DataItem dataItem      = DataItemSerialisation.GetDataItem(serialisedXml);
                    Console.WriteLine($"\nData received: {dataItem.Id}\n");

                    string sheakData = DataManagmentServer.DataLayout(dataItem.Id);
                    response = new DataItem(sheakData);
                    Console.WriteLine($"Data send: \n{sheakData}\n");

                    string serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);
                    byte[] message        = Encoding.Unicode.GetBytes(serialisedItem);

                    clientSocket.Send(message);
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.ToString());
            }
        }
Пример #2
0
    {    /// <summary>
         /// Runs DataClient instances that requests data to the server.
         /// </summary>
         ///

        public string Request(string message)
        {
            try
            {
                IPHostEntry ipHostDetails    = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipAddressDetails = ipHostDetails.AddressList[0];
                IPEndPoint  localEndPoint    = new IPEndPoint(ipAddressDetails, 4242);

                Socket sender = new Socket(ipAddressDetails.AddressFamily,
                                           SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    DataItem request        = new DataItem(message);
                    string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(request);
                    sender.Connect(localEndPoint);

                    // Send data request to server
                    byte[] messageToSend = Encoding.ASCII.GetBytes(serialisedItem + "<EOF>");
                    int    byteSent      = sender.Send(messageToSend);

                    // Data buffer
                    byte[] messageReceived = new byte[4096];

                    // Recieve answer from server
                    int    byteRecv = sender.Receive(messageReceived);
                    string response = Encoding.ASCII.GetString(messageReceived, 0, byteRecv);



                    DataItem dataItem = DataItemSerialisation.GetDataItem(response);
                    return(dataItem.package);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                    return("");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return("");
            }
        }
        public void Run(string data)
        {
            try
            {
                ipHostDetails   = Dns.GetHostEntry(Dns.GetHostName());
                ipAddressDetail = ipHostDetails.AddressList[0];
                localEndPoint   = new IPEndPoint(ipAddressDetail, 4747);

                sender = new Socket(ipAddressDetail.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    request        = new DataItem(data);
                    serialisedItem = DataItemSerialisation.GetSerialisedDataItem(request);
                    sender.Connect(localEndPoint);
                    //Console.WriteLine($"Socket connected to: {sender.RemoteEndPoint}");

                    //send data request to server
                    messageToSend = Encoding.Unicode.GetBytes(serialisedItem + "<EOF>");
                    byteSent      = sender.Send(messageToSend);

                    //Data buffer
                    messageReceived = new byte[4096];

                    //Recieve answer from server
                    byteRequest = sender.Receive(messageReceived);
                    response    = Encoding.Unicode.GetString(messageReceived, 0, byteRequest);

                    dataItem = DataItemSerialisation.GetDataItem(response);
                    Console.WriteLine(DataManagment.DataLayout(dataItem.Id));
                }
                catch (Exception error)
                {
                    Console.WriteLine(error.ToString());
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.ToString());
            }
        }
        /// <summary>
        /// Run a DataServer instance.
        /// </summary>
        public void Run()
        {
            IPHostEntry ipHostDetails    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddressDetails = ipHostDetails.AddressList[0];
            IPEndPoint  localEndPoint    = new IPEndPoint(ipAddressDetails, 4242);

            Socket listenerSocket = new Socket(ipAddressDetails.AddressFamily,
                                               SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listenerSocket.Bind(localEndPoint);
                listenerSocket.Listen(10);

                while (true)
                {
                    WriteLine("Listening for data ");
                    Socket clientSocket = listenerSocket.Accept();

                    // Data buffer
                    byte[] bytes = new Byte[4096];
                    string data  = null;
                    // Get the data from the server
                    while (true)
                    {
                        int numberOfBytes = clientSocket.Receive(bytes);
                        data += Encoding.ASCII.GetString(bytes, 0, numberOfBytes);
                        if (data.IndexOf("<EOF>", StringComparison.Ordinal) > -1)
                        {
                            break;
                        }
                    }
                    string   serialisedXml = data.Substring(0, data.Length - 5);
                    DataItem dataItem      = DataItemSerialisation.GetDataItem(serialisedXml);
                    WriteLine("Text received -> {0} ", dataItem.package);
                    string[] packageItems = dataItem.package.Split(',');

                    if (packageItems[0] == "RequestFlightList")
                    //Chain of Responsibility
                    {
                        List <string> FlightIDList = new List <string>();

                        foreach (Flight flight in FlightList.Flights)
                        {
                            FlightIDList.Add(flight.FlightID);
                        }

                        FlightItemList response       = new FlightItemList(FlightIDList);
                        string         serialisedItem = FlightItemListSerialisation.GetSerialisedDataItem(response);
                        byte[]         message        = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Recieved and responds to quest for Flight list.
                    }
                    else if (packageItems[0] == "NoOfFlights")
                    {
                        DataItem response       = new DataItem((FlightList.Flights.Count - 1).ToString());
                        string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Returns list of Flights
                    }
                    else if (packageItems[0] == "BookASeat")
                    {
                        bool     Successful = FlightList.Flights[Convert.ToInt32(packageItems[1])].BookSeat(packageItems[2]);
                        DataItem response   = new DataItem(" ");
                        if (Successful)
                        {
                            response = new DataItem("Seat Booked Successfully");
                        }
                        else
                        {
                            response = new DataItem("Seat Unavailable");
                        }
                        //Prevents seat being booked if already occupied.


                        string serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                    }
                    else if (packageItems[0] == "SeatPlan")
                    {
                        DataItem response       = new DataItem(FlightList.Flights[Convert.ToInt32(packageItems[1])].GetSeatingPlan());
                        string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Sends seating plan to client
                    }
                    else if (packageItems[0] == "AvailableSeats")
                    {
                        DataItem response       = new DataItem(FlightList.Flights[Convert.ToInt32(packageItems[1])].GetAvailableSeats());
                        string   serialisedItem = DataItemSerialisation.GetSerialisedDataItem(response);

                        byte[] message = Encoding.ASCII.GetBytes(serialisedItem);

                        clientSocket.Send(message);
                        //Sends available seat list to Client.
                    }



                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                }
            }
            catch (Exception e)
            {
                WriteLine(e.ToString());
            }
        }