www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Server side command execution.

reply holo <holosian gmail.comm> writes:
Hello

Im trying to execute commands on server side. Here is my server 
based on other example from forum:

#!/usr/bin/rdmd

import std.stdio;
import std.socket;
import std.algorithm;
import std.conv;

void main() {
     Socket server = new TcpSocket();
     server.setOption(SocketOptionLevel.SOCKET, 
SocketOption.REUSEADDR, true);
     server.bind(new InternetAddress(8080));
     server.listen(1);


	Socket client = server.accept();

     while(true) {


         char[1024] buffer = 0;
         auto received = client.receive(buffer);

         writefln("The client said:\n%s", buffer[0.. received]);

         enum header =
             "HTTP/1.0 200 OK\nContent-Type: text/html; 
charset=utf-8\n\n";

         string response = header ~ "Hello World!\n";
         client.send(response);
	

	if(to!string(buffer) == "exit")
	{
		
		break;
	}
    }

     client.shutdown(SocketShutdown.BOTH);
     client.close();

}

When i send "exit" nothing is happening. How it should be done to 
make it working?

//holo
Sep 27 2015
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Sunday, 27 September 2015 at 23:56:10 UTC, holo wrote:
 Hello

 Im trying to execute commands on server side. Here is my server 
 based on other example from forum:

 [...]
You are comparing whole buffer to "exit"
Sep 27 2015
parent reply holo <holosian gmail.comm> writes:
On Monday, 28 September 2015 at 04:55:30 UTC, tcak wrote:
 On Sunday, 27 September 2015 at 23:56:10 UTC, holo wrote:
 Hello

 Im trying to execute commands on server side. Here is my 
 server based on other example from forum:

 [...]
You are comparing whole buffer to "exit"
I changed my condition to: if(to!string(buffer[0..received]) == "exit") { break; } But it still dint help.
Sep 28 2015
parent reply anonymous <anonymous example.com> writes:
On Monday 28 September 2015 11:59, holo wrote:

 I changed my condition to:
 
 if(to!string(buffer[0..received]) == "exit")
 	{
 		
 		break;
 	}
 
 
 But it still dint help.
The client probably sends a newline; i.e. buffer[0 .. received] is "exit\n".
Sep 28 2015
parent reply anonymous <anonymous example.com> writes:
On Monday 28 September 2015 12:40, anonymous wrote:

 The client probably sends a newline; i.e. buffer[0 .. received] is
 "exit\n".
Or more likely it's "exit\r\n".
Sep 28 2015
parent reply holo <holosian gmail.com> writes:
On Monday, 28 September 2015 at 10:52:07 UTC, anonymous wrote:
 On Monday 28 September 2015 12:40, anonymous wrote:

 The client probably sends a newline; i.e. buffer[0 .. 
 received] is "exit\n".
Or more likely it's "exit\r\n".
I changed condition to: if(to!string(buffer[0..received]) == "exit\n") { break; } and now it is working. Thank you all for help.
Sep 28 2015
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 28 September 2015 at 11:44:32 UTC, holo wrote:
 if(to!string(buffer[0..received]) == "exit\n")
You shouldn't need that to!string by the way. I believe that will work just comparing the buffer directly. Converting to string is more important when you are storing a copy than just comparing it.
Sep 28 2015
parent holo <holosian gmail.com> writes:
On Monday, 28 September 2015 at 13:01:25 UTC, Adam D. Ruppe wrote:
 On Monday, 28 September 2015 at 11:44:32 UTC, holo wrote:
 if(to!string(buffer[0..received]) == "exit\n")
You shouldn't need that to!string by the way. I believe that will work just comparing the buffer directly. Converting to string is more important when you are storing a copy than just comparing it.
Yep it it like you wrote. Thank you for advice.
Sep 28 2015