Exemplo n.º 1
0
        // GET: api/SocketStart
        //public IEnumerable<string> Get()
        //{
        //    return new string[] { "value1", "value2" };
        //}

        // GET: api/SocketStart/5
        //public string Get(int id)
        //{
        //    return "value";
        //}

        // POST: api/SocketStart
        public HttpResponseMessage Post(dynamic value)
        {
            HttpResponseMessage response;
            string data = JsonConvert.SerializeObject(value);

            try
            {
                Socketcmd socketcmd = JsonConvert.DeserializeObject <Socketcmd>(data);
                if (GlobalVariables.GlobalSocketList != null)
                {
                    if (GlobalVariables.GlobalSocketList.Count > 0)
                    {
                        response = Request.CreateResponse(HttpStatusCode.OK, @"{""Status"": ""There are " + GlobalVariables.GlobalSocketList.Count + @" connections running. Please stop the current connections to create more connections.""}");
                        //@"{""Status"": ""There are " + GlobalVariables.GlobalSocketList.Count + @" connections opened.""}"
                    }
                    else
                    {
                        GlobalVariables.GlobalSocketList = AsynchronousClient.StartClient(socketcmd);
                        response = Request.CreateResponse(HttpStatusCode.OK, @"{""Status"": ""Requests created " + GlobalVariables.GlobalSocketList.Count + @" number of connections.""}");
                    }
                }
                else
                {
                    GlobalVariables.GlobalSocketList = AsynchronousClient.StartClient(socketcmd);
                    response = Request.CreateResponse(HttpStatusCode.OK, @"{""Status"": ""Requests created " + GlobalVariables.GlobalSocketList.Count + @" number of connections.""}");
                }
            }
            catch (Exception)
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, "Unable to DeserializeObject" + data);
                return(response);
            }
            return(response);
        }
Exemplo n.º 2
0
        public static List <Socket> StartClient(Socketcmd cmd)
        {
            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                // The name of the
                // remote device is "host.contoso.com".
                //IPHostEntry ipHostInfo = Dns.GetHostEntry("host.contoso.com");
                IPHostEntry ipHostInfo = Dns.GetHostEntry(cmd.Host);
                IPAddress   ipAddress  = ipHostInfo.AddressList[0];
                IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, cmd.Port);

                List <Socket> sockets = new List <Socket>();

                for (int i = 0; i < cmd.Connections; i++)
                {
                    // Create a TCP/IP socket.
                    Socket client = new Socket(ipAddress.AddressFamily,
                                               SocketType.Stream, ProtocolType.Tcp);

                    // Connect to the remote endpoint.
                    client.BeginConnect(remoteEP,
                                        new AsyncCallback(ConnectCallback), client);
                    connectDone.WaitOne();
                    sockets.Add(client);
                }

                return(sockets);
                //Thread.Sleep(60 * 1000);
                //foreach (var socket in sockets)
                //{
                //    //    //client.Shutdown(SocketShutdown.Both);
                //    //    //client.Close();
                //    socket.Shutdown(SocketShutdown.Both);
                //    socket.Close();
                //}

                //// Create a TCP/IP socket.
                //Socket client = new Socket(ipAddress.AddressFamily,
                //    SocketType.Stream, ProtocolType.Tcp);

                //// Connect to the remote endpoint.
                //client.BeginConnect(remoteEP,
                //    new AsyncCallback(ConnectCallback), client);
                //connectDone.WaitOne();

                //string request = "GET / HTTP/1.1\r\nHost: " + server +
                //    "\r\nConnection: Close\r\n\r\n";
                //// Send test data to the remote device.
                ////Send(client, "This is a test<EOF>");
                //Send(client, request);
                //sendDone.WaitOne();

                //// Receive the response from the remote device.
                //Receive(client);
                //receiveDone.WaitOne();

                //// Write the response to the console.
                //Console.WriteLine("Response received : {0}", response);
                //response = String.Empty;
                //// Send test data to the remote device.
                ////Send(client, "This is a test<EOF>");
                //Send(client, request);
                //sendDone.WaitOne();

                //// Receive the response from the remote device.
                //Receive(client);
                //receiveDone.WaitOne();

                //// Write the response to the console.
                //Console.WriteLine("Response received : {0}", response);

                // Release the socket.
                //client.Shutdown(SocketShutdown.Both);
                //client.Close();
            }
            catch (System.Net.Sockets.SocketException e)
            {
                Console.WriteLine(e.ToString());
                throw e;
            }
        }