Thread overview |
---|
September 27, 2015 Server side command execution. | ||||
---|---|---|---|---|
| ||||
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 |
September 28, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to holo | 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"
|
September 28, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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. |
September 28, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to holo | 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".
|
September 28, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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".
|
September 28, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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. |
September 28, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to holo | 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.
|
September 29, 2015 Re: Server side command execution. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
Copyright © 1999-2021 by the D Language Foundation