Summary: Making network with the std.stdio.File interface
           Product: D
           Version: 2.041
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos

--- Comment #0 from Adam D. Ruppe <> 2010-03-28 
15:51:43 PDT ---
I've written a small module that opens a network connection, then wraps it in
the File struct, allowing you to use the byLine, etc., ranges on it, as well as

It is Linux only, but should be pretty easy to port to other operating systems.
Ideally, I'd like to eventually be able to use File for talking
to processes too, on all D platforms. 

Here's the code I have for now:


public import std.stdio;
import std.string;

import std.conv;


import std.c.linux.linux;
import std.c.linux.socket;

alias std.c.linux.socket sock;
alias std.c.linux.linux linux;

enum int PF_INET = 2;
enum int AF_INET = PF_INET;

extern(C) FILE* fdopen(int, const(char)*);

File openNetwork(string host, ushort port) {
    hostent* h;
    sockaddr_in addr;

    h = gethostbyname(std.string.toStringz(host));
    if(h is null)
        throw new StdioException("gethostbyname");

    int s = socket(PF_INET, SOCK_STREAM, 0);
    if(s == -1)
        throw new StdioException("socket");


    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    std.c.string.memcpy(&addr.sin_addr.s_addr, h.h_addr, h.h_length);

    if(sock.connect(s, cast(sockaddr*) &addr, addr.sizeof) == -1)
        throw new StdioException("Connect failed");

    FILE* fp = fdopen(s, "w+".ptr);

    File f;

    auto imp = new File.Impl(fp, 1, host ~ ":" ~ to!string(port));

    f.p = imp;

    return f;

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to