www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D Lang Socket Programming Example

reply "Savsak" <savsaktheteam gmail.com> writes:
Hi Friends,

Socket programming with the D programming language is the most 
simple way how to do it

For example, the sample with Tango, but not by phobos

How do I do this with a simple, but phobos


import tango.io.Stdout;

import tango.net.Socket;
import tango.net.ServerSocket;
import tango.net.SocketConduit;

int main() {
     Socket server = new Socket(AddressFamily.UNIX,
                                SocketType.STREAM,
                                ProtocolType.IP);

     while(true) {
         Socket client = server.accept();

         char[1024] buffer;
         client.receive(buffer);

         Stdout.format("The client said'{}'.", buffer);

         client.shutdown(SocketShutdown.BOTH);
         client.detach();
     }
     return 0;
}
Sep 06 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/06/2013 01:47 PM, Savsak wrote:

 Hi Friends,

 Socket programming with the D programming language is the most simple
 way how to do it

 For example, the sample with Tango, but not by phobos

 How do I do this with a simple, but phobos


 import tango.io.Stdout;

 import tango.net.Socket;
 import tango.net.ServerSocket;
 import tango.net.SocketConduit;

 int main() {
      Socket server = new Socket(AddressFamily.UNIX,
                                 SocketType.STREAM,
                                 ProtocolType.IP);

      while(true) {
          Socket client = server.accept();

          char[1024] buffer;
          client.receive(buffer);

          Stdout.format("The client said'{}'.", buffer);

          client.shutdown(SocketShutdown.BOTH);
          client.detach();
      }
      return 0;
 }
Here is a Phobos translation: import std.stdio; import std.socket; 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; 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(); } } Ali P.S. Note that there is also the D.learn newsgroup. P.P.S. This question came up on two separate Turkish D forums recently: http://ddili.org/forum/post/10063 http://forum.ceviz.net/d-dili/127048-ddili-socket-okuma-veri-gonderme-quotnon-blocking-i-oquot.html
Sep 06 2013
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 6 September 2013 at 21:02:09 UTC, Ali Çehreli wrote:
 Here is a Phobos translation:

     server.bind(new InternetAddress(8080));
Not quite the same - the original used a Unix domain socket. I *think* if you replace that line with a UnixAddress: http://dlang.org/phobos/std_socket.html#UnixAddress It will do the trick. (I say *think* because I've never actually used a unix socket with D, so I'm not entirely sure.)
Sep 06 2013
parent reply "Brian Schott" <briancschott gmail.com> writes:
On Friday, 6 September 2013 at 21:19:51 UTC, Adam D. Ruppe wrote:
 On Friday, 6 September 2013 at 21:02:09 UTC, Ali Çehreli wrote:
 Here is a Phobos translation:

    server.bind(new InternetAddress(8080));
Not quite the same - the original used a Unix domain socket. I *think* if you replace that line with a UnixAddress: http://dlang.org/phobos/std_socket.html#UnixAddress It will do the trick. (I say *think* because I've never actually used a unix socket with D, so I'm not entirely sure.)
http://d.puremagic.com/issues/show_bug.cgi?id=9384 It won't work. (This is why my project uses IP sockets for local communication, even on Linux)
Sep 06 2013
next sibling parent "Ramon" <spam thanks.no> writes:
On Friday, 6 September 2013 at 21:50:11 UTC, Brian Schott wrote:
   server.bind(new InternetAddress(8080));
Not quite the same - the original used a Unix domain socket. I *think* if you replace that line with a UnixAddress: http://dlang.org/phobos/std_socket.html#UnixAddress
http://d.puremagic.com/issues/show_bug.cgi?id=9384 It won't work. (This is why my project uses IP sockets for local communication, even on Linux)
Meaning that domain/Unix sockets aren't available at all in D (or need to be done through C calls directly)?
Sep 06 2013
prev sibling parent "jerro" <a a.com> writes:
 It won't work. (This is why my project uses IP sockets for 
 local communication, even on Linux)
That has already been fixed in the version of Phobos on Github (I've marked it as fixed now, didn't know about that issue on bugzilla before).
Sep 08 2013
prev sibling parent reply "Savsak" <savsaktheteam gmail.com> writes:
On Friday, 6 September 2013 at 21:02:09 UTC, Ali Çehreli wrote:
 On 09/06/2013 01:47 PM, Savsak wrote:

 Hi Friends,

 Socket programming with the D programming language is the
most simple
 way how to do it

 For example, the sample with Tango, but not by phobos

 How do I do this with a simple, but phobos


 import tango.io.Stdout;

 import tango.net.Socket;
 import tango.net.ServerSocket;
 import tango.net.SocketConduit;

 int main() {
      Socket server = new Socket(AddressFamily.UNIX,
                                 SocketType.STREAM,
                                 ProtocolType.IP);

      while(true) {
          Socket client = server.accept();

          char[1024] buffer;
          client.receive(buffer);

          Stdout.format("The client said'{}'.", buffer);

          client.shutdown(SocketShutdown.BOTH);
          client.detach();
      }
      return 0;
 }
Here is a Phobos translation: import std.stdio; import std.socket; 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; 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(); } } Ali P.S. Note that there is also the D.learn newsgroup. P.P.S. This question came up on two separate Turkish D forums recently: http://ddili.org/forum/post/10063 http://forum.ceviz.net/d-dili/127048-ddili-socket-okuma-veri-gonderme-quotnon-blocking-i-oquot.html
Thanks acehreli In the example you give, but I receive this error during compilation. savsak:~ savsak$ dmd /Users/savsak/Desktop/test.d ld: library not found for -lphobos2 collect2: ld returned 1 exit status --- errorlevel 1
Sep 08 2013
next sibling parent "Dicebot" <public dicebot.lv> writes:
On Sunday, 8 September 2013 at 10:42:22 UTC, Savsak wrote:
 Thanks acehreli

 In the example you give, but I receive this error during 
 compilation.

 savsak:~ savsak$ dmd /Users/savsak/Desktop/test.d
 ld: library not found for -lphobos2
 collect2: ld returned 1 exit status
 --- errorlevel 1
Your dmd installation is broken. Please tell how have you set it up to get any fixing suggestions ;)
Sep 08 2013
prev sibling parent "Ramon" <spam thanks.no> writes:
On Sunday, 8 September 2013 at 10:42:22 UTC, Savsak wrote:
 ...
 In the example you give, but I receive this error during 
 compilation.

 savsak:~ savsak$ dmd /Users/savsak/Desktop/test.d
 ld: library not found for -lphobos2
 collect2: ld returned 1 exit status
 --- errorlevel 1
To help dicebot and others to help you it would be useful to copy the output of the following two commands:
 cat /etc/dmd.conf
 find /usr -name 'libphobos2*'
(Please note the *single* ticks (rather than ") to not have your shell resolve it. Also note that probably /usr/lib would be more efficient as search path but using /usr can also catch exotic cases like /usr/share). If your installation is OK you will find the paths told by the find command to match those in /etc/dmd.conf. Here is an example of a working installation: $ find /usr -name 'libphobos2*' /usr/lib/x86_64-linux-gnu/libphobos2.so /usr/lib/x86_64-linux-gnu/libphobos2.a /usr/lib/i386-linux-gnu/libphobos2.so /usr/lib/i386-linux-gnu/libphobos2.a /usr/lib/i386-linux-gnu/libphobos2.so.0.2 --------------- $ cat /etc/dmd.conf // ... some comment lines .. [Environment] DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import -L-L/usr/lib/i386-linux-gnu -L-L/usr/lib/x86_64-linux-gnu -L--no-warn-search-mismatch -L--export-dynamic ---------------- HtH -R
Sep 08 2013
prev sibling parent reply vyarthrot <vyarthrot yahoo.com> writes:
On Friday, 6 September 2013 at 20:47:54 UTC, Savsak wrote:
 Hi Friends,

 Socket programming with the D programming language is the most 
 simple way how to do it

 For example, the sample with Tango, but not by phobos

 How do I do this with a simple, but phobos


 import tango.io.Stdout;

 import tango.net.Socket;
 import tango.net.ServerSocket;
 import tango.net.SocketConduit;

 int main() {
     Socket server = new Socket(AddressFamily.UNIX,
                                SocketType.STREAM,
                                ProtocolType.IP);

     while(true) {
         Socket client = server.accept();

         char[1024] buffer;
         client.receive(buffer);

         Stdout.format("The client said'{}'.", buffer);

         client.shutdown(SocketShutdown.BOTH);
         client.detach();
     }
     return 0;
 }
Try this simple socket programming.... http://csharp.net-informations.com/communications/csharp-socket-programming.htm
Sep 25 2015
parent reply bitwise <bitwise.pvt gmail.com> writes:
On Friday, 25 September 2015 at 07:42:25 UTC, vyarthrot wrote:
 On Friday, 6 September 2013 at 20:47:54 UTC, Savsak wrote:
 Hi Friends,

 Socket programming with the D programming language is the most 
 simple way how to do it

 For example, the sample with Tango, but not by phobos

 How do I do this with a simple, but phobos
 [ ... ]
Try this simple socket programming.... http://csharp.net-informations.com/communications/csharp-socket-programming.htm
Not sure if this was an accident, or whether you're trying to discourage the use of D, but as much as I love to nag about D, I no pro server programmer, but I'm writing a simple one right now, and D seems to be very capable in this area. Bit
Sep 25 2015
parent reply Kapps <opantm2+spam gmail.com> writes:
On Saturday, 26 September 2015 at 00:15:39 UTC, bitwise wrote:
 On Friday, 25 September 2015 at 07:42:25 UTC, vyarthrot wrote:
 On Friday, 6 September 2013 at 20:47:54 UTC, Savsak wrote:
 [...]
Try this simple socket programming.... http://csharp.net-informations.com/communications/csharp-socket-programming.htm
Not sure if this was an accident, or whether you're trying to discourage the use of D, but as much as I love to nag about D, I'm no pro server programmer, but I'm writing a simple one right now, and D seems to be very capable in this area. Bit
It's just a spambot scouring for keywords (socket programming).
Sep 25 2015
parent bitwise <bitwise.pvt gmail.com> writes:
On Saturday, 26 September 2015 at 02:00:42 UTC, Kapps wrote:
 On Saturday, 26 September 2015 at 00:15:39 UTC, bitwise wrote:
 On Friday, 25 September 2015 at 07:42:25 UTC, vyarthrot wrote:
 On Friday, 6 September 2013 at 20:47:54 UTC, Savsak wrote:
 [...]
Try this simple socket programming.... http://csharp.net-informations.com/communications/csharp-socket-programming.htm
Not sure if this was an accident, or whether you're trying to discourage the use of D, but as much as I love to nag about D, I'm no pro server programmer, but I'm writing a simple one right now, and D seems to be very capable in this area. Bit
It's just a spambot scouring for keywords (socket programming).
Hmm.. it appears I'm even less pro than I originally thought -_- Bit
Sep 25 2015