TCP Socket Client Example

Kingsley via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 14 09:02:28 PDT 2015


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?


More information about the Digitalmars-d mailing list