www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Client socket sending data only one time.

reply holo <holosian gmail.comm> writes:
Hello

I'm trying to connect to server and send data, with such simple 
client:



import std.stdio;
import std.socket;
import std.socketstream;
import std.process;
import std.conv;
import core.time;

void main()
{
         char[1024] buffer = 0;

         Socket client = new TcpSocket();
         auto addrServer = new InternetAddress("localhost", 8080);
         client.connect(addrServer);

         while(1)
         {

                 client.send(readln());
                 client.receive(buffer);
                 writeln(buffer);
                 buffer = 0;
          }
}

It is working but only one time, when I'm trying to send again 
(second loop of while) it's stooping on readln() but not sending 
input data.

What am i missing here?

Thanks in advance for any help.
Sep 24 2015
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Thursday, 24 September 2015 at 14:20:39 UTC, holo wrote:
 Hello

 I'm trying to connect to server and send data, with such simple 
 client:



 import std.stdio;
 import std.socket;
 import std.socketstream;
 import std.process;
 import std.conv;
 import core.time;

 void main()
 {
         char[1024] buffer = 0;

         Socket client = new TcpSocket();
         auto addrServer = new InternetAddress("localhost", 
 8080);
         client.connect(addrServer);

         while(1)
         {

                 client.send(readln());
                 client.receive(buffer);
                 writeln(buffer);
                 buffer = 0;
          }
 }

 It is working but only one time, when I'm trying to send again 
 (second loop of while) it's stooping on readln() but not 
 sending input data.

 What am i missing here?

 Thanks in advance for any help.
Where is the other side of this client? There must be a TCP Server to listen connections, and create a second TCPSocket for communication. There is only one TCPSocket in your code. A lot of things are missing there. I would suggest you to check "C TCPSocket example" in your favourite search engine.
Sep 24 2015
parent holo <holosian gmail.comm> writes:
On Friday, 25 September 2015 at 04:38:06 UTC, tcak wrote:
 On Thursday, 24 September 2015 at 14:20:39 UTC, holo wrote:
 Hello

 I'm trying to connect to server and send data, with such 
 simple client:



 import std.stdio;
 import std.socket;
 import std.socketstream;
 import std.process;
 import std.conv;
 import core.time;

 void main()
 {
         char[1024] buffer = 0;

         Socket client = new TcpSocket();
         auto addrServer = new InternetAddress("localhost", 
 8080);
         client.connect(addrServer);

         while(1)
         {

                 client.send(readln());
                 client.receive(buffer);
                 writeln(buffer);
                 buffer = 0;
          }
 }

 It is working but only one time, when I'm trying to send again 
 (second loop of while) it's stooping on readln() but not 
 sending input data.

 What am i missing here?

 Thanks in advance for any help.
Where is the other side of this client? There must be a TCP Server to listen connections, and create a second TCPSocket for communication. There is only one TCPSocket in your code. A lot of things are missing there. I would suggest you to check "C TCPSocket example" in your favourite search engine.
Sorry i forgot to paste server side. After your post i look on that side of my simple socket program and i find out the problem. This is example from other forum topic: 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); while(true) { Socket client = server.accept(); 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); client.shutdown(SocketShutdown.BOTH); client.close(); } } Lie we see after each data recive server is closing connection. I rewrite it to something like it: 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); auto command = to!string(buffer[0.. received]); if(command == "exit") { break; } } client.shutdown(SocketShutdown.BOTH); client.close(); } Now everything is working like i needed.
Sep 27 2015