www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TCP Socket Client Example

reply "Kingsley" <kingsley.hendrickse gmail.com> writes:
Hi

Does anyone have some examples of making a client socket 
connection to a host on a port and parsing the incoming data in 
some kind of loop.

--K
Aug 14 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 14 August 2015 at 14:06:03 UTC, Kingsley wrote:
 Does anyone have some examples of making a client socket 
 connection to a host on a port and parsing the incoming data in 
 some kind of loop.
I put one in chapter 2 of my book: https://www.packtpub.com/application-development/d-cookbook Here's the code sample alone: http://arsdnet.net/dcode/book/chapter_02/03/ The client just takes lines from the user and forwards them to the server. Not really useful alone but maybe it will show you how to get started. The text in the book itself goes into a little more detail and you can look up the functions here http://dlang.org/phobos/std_socket.html for a lil more info.
Aug 14 2015
prev sibling next sibling parent reply "Alex Parrill" <initrd.gz gmail.com> writes:
On Friday, 14 August 2015 at 14:06:03 UTC, Kingsley wrote:
 Hi

 Does anyone have some examples of making a client socket 
 connection to a host on a port and parsing the incoming data in 
 some kind of loop.

 --K
auto addresses = getAddress("localhost", 8085); auto socket = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP); scope(exit) socket.close(); socket.connect(addresses[0]); auto buffer = new ubyte[2056]; ptrdiff_t amountRead; while((amountRead = socket.receive(buffer)) != 0) { enforce(amountRead > 0, lastSocketError); // Do stuff with buffer }
Aug 14 2015
parent reply "Kingsley" <kingsley.hendrickse gmail.com> writes:
On Friday, 14 August 2015 at 14:12:14 UTC, Alex Parrill wrote:
 On Friday, 14 August 2015 at 14:06:03 UTC, Kingsley wrote:
 Hi

 Does anyone have some examples of making a client socket 
 connection to a host on a port and parsing the incoming data 
 in some kind of loop.

 --K
auto addresses = getAddress("localhost", 8085); auto socket = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP); scope(exit) socket.close(); socket.connect(addresses[0]); auto buffer = new ubyte[2056]; ptrdiff_t amountRead; while((amountRead = socket.receive(buffer)) != 0) { enforce(amountRead > 0, lastSocketError); // Do stuff with buffer }
Great thanks. How would I send stuff back to the server without blocking? Is there some kind of IO reactor / event machine library for D that could help me?
Aug 14 2015
next sibling parent "y" <dlang srtnwz.com> writes:
On Friday, 14 August 2015 at 16:02:30 UTC, Kingsley wrote:

 How would I send stuff back to the server without blocking? Is 
 there some kind of IO reactor / event machine library for D 
 that could help me?
https://github.com/rejectedsoftware/vibe.d can be used with https://github.com/etcimon/libasync
Aug 14 2015
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 14 August 2015 at 16:02:30 UTC, Kingsley wrote:
 How would I send stuff back to the server without blocking? Is 
 there some kind of IO reactor / event machine library for D 
 that could help me?
you could also just use Socket.select and not worry about the whole blocking thing as much then to handle multiple ones at once. If you have like < 20 connections, select works really well and is easy.
Aug 14 2015
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/14/2015 07:06 AM, Kingsley wrote:
 Hi

 Does anyone have some examples of making a client socket connection to a
 host on a port and parsing the incoming data in some kind of loop.

 --K
A recent thread with yet another example: http://forum.dlang.org/post/mmhlsp$2p4e$1 digitalmars.com Ali
Aug 14 2015
prev sibling parent "Laeeth Isharc" <spamnolaeeth nospamlaeeth.com> writes:
On Friday, 14 August 2015 at 14:06:03 UTC, Kingsley wrote:
 Hi

 Does anyone have some examples of making a client socket 
 connection to a host on a port and parsing the incoming data in 
 some kind of loop.

 --K
Nanomsg might be one option if you control the host - by one of the main authors of Zeromq. He decided to rewrite in C and it addresses some architectural flaws of Zeromq. (At least one smart guy in London D community agrees it's better). http://www.infoq.com/news/2012/03/Crossroads-IO http://nanomsg.org/ Nanomsg itself is at an early stage, and I think the author has a lot on his plate (if I recall right he is now at Google) so progress isn't as fast as one would like. But it's usable, and I have some primitive D bindings/wrapper here. Nanomsg seems stable enough for me, and one or two glitches in bindings/wrappers but I am using them for my stuff. I know that if nanomsg ends up being a disaster I can always switch to zeromq, which is similar enough to do so easily. https://github.com/Laeeth/d-nanomsg.git D written for C style bindings for pipeline here: int node0 (string xurl) { int sock = nn_socket (AF_SP, NN_PULL); auto url=xurl.toStringz; assert(sock >= 0); assert(nn_bind (sock, url) >= 0); while (1) { char* buf = cast(char*)0; int bytes = nn_recv (sock, &buf, NN_MSG, 0); assert (bytes >= 0); writefln("NODE0: RECEIVED %s bytes: \"%s\"", bytes,to!string(buf)); nn_freemsg (buf); } return 0; }
Aug 15 2015