www.digitalmars.com         C & C++   DMDScript  

D - Socket library for Win32 and Linux

reply Vathix <vathix dprogramming.com> writes:
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Attached my code. I tried to make it work for linux but was unable to 
test it, I don't know much about linux. I get this error when running 
dmd on debian:
./dmd: error while loading shared libraries: libstdc++.so.5: cannot open 
shared object file: No such file or directory
I tried to install libstdc++ libs but the error remains.

I'd appreciate it if someone could help by getting the code to work for 
linux or helping me fix my problem :)


-- 
Christopher E. Miller
www.dprogramming.com
irc.dprogramming.com #D
Feb 27 2004
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
Vathix wrote:
 Attached my code. I tried to make it work for linux but was unable to
 test it, I don't know much about linux. I get this error when running
 dmd on debian:
 ./dmd: error while loading shared libraries: libstdc++.so.5: cannot
 open shared object file: No such file or directory
 I tried to install libstdc++ libs but the error remains.

From my own experience, that means you have to update a lot of things, not only that one, and with very recent versions.
 I'd appreciate it if someone could help by getting the code to work
 for linux or helping me fix my problem :)


 --
 Christopher E. Miller
 www.dprogramming.com
 irc.dprogramming.com #D

I've attached a slightly modified socket.d. From what I can remember: - version(linux) instead of Linux - declared fd_set for BsdSockets - blocking in Socket class was only defined for Windows, although being used in both versions. Commented out the line - errno declared as extern(C) I think that's it. ----------------------- Carlos Santander Bernal8
Feb 27 2004
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
Carlos Santander B. wrote:
 Vathix wrote:
 Attached my code. I tried to make it work for linux but was unable
 to test it, I don't know much about linux. I get this error when
 running dmd on debian:
 ./dmd: error while loading shared libraries: libstdc++.so.5: cannot
 open shared object file: No such file or directory
 I tried to install libstdc++ libs but the error remains.

From my own experience, that means you have to update a lot of things, not only that one, and with very recent versions.
 I'd appreciate it if someone could help by getting the code to work
 for linux or helping me fix my problem :)


 --
 Christopher E. Miller
 www.dprogramming.com
 irc.dprogramming.com #D

I've attached a slightly modified socket.d. From what I can remember: - version(linux) instead of Linux - declared fd_set for BsdSockets - blocking in Socket class was only defined for Windows, although being used in both versions. Commented out the line - errno declared as extern(C) I think that's it. ----------------------- Carlos Santander Bernal

I tested it on RedHat 9, and it compiled without a problem, but when testing it, I got "Socket select error". Any ideas? ----------------------- Carlos Santander Bernal
Feb 28 2004
next sibling parent reply Vathix <vathix dprogramming.com> writes:
 I tested it on RedHat 9, and it compiled without a problem, but when testing
 it, I got "Socket select error". Any ideas?
 
 -----------------------
 Carlos Santander Bernal
 

Yes, fd_set is handled differently for linux, I'm working on it now. I got DMD to work on linux. Thanks for your help. -- Christopher E. Miller
Feb 28 2004
parent reply Vathix <vathix dprogramming.com> writes:
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Here we go, Socket.select() is working on linux! Seems to be all good now.

-- 
Christopher E. Miller
Feb 28 2004
parent reply =?ISO-8859-1?Q?Julio_Jim=E9nez?= <jujibo inicia.es> writes:
Vathix wrote:
 Here we go, Socket.select() is working on linux! Seems to be all good now.
 

Only change in socket.d extern int errno; to -> extern (C) int errno; at line 144 regards. Julio Jiménez
Feb 29 2004
parent reply Vathix <vathix dprogramming.com> writes:
Julio Jiménez wrote:

 Vathix wrote:
 
 Here we go, Socket.select() is working on linux! Seems to be all good 
 now.

Only change in socket.d extern int errno; to -> extern (C) int errno; at line 144 regards. Julio Jiménez

It's already in an extern(C) block, and I think extern(C) is different from just extern.. -- Christopher E. Miller
Feb 29 2004
parent reply Vathix <vathix dprogramming.com> writes:
Vathix wrote:

 Julio Jiménez wrote:
 
 Vathix wrote:

 Here we go, Socket.select() is working on linux! Seems to be all good 
 now.

Only change in socket.d extern int errno; to -> extern (C) int errno; at line 144 regards. Julio Jiménez

It's already in an extern(C) block, and I think extern(C) is different from just extern..

Sorry, I'm wrong. I didn't realize extern was short for extern(D). -- Christopher E. Miller
Feb 29 2004
parent reply Vathix <vathix dprogramming.com> writes:
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

I guess this one could be considered socket.d 1.0. I've tested it pretty 
well, even made a test server that accepts multiple connections, 
listener.d included. It works good for windows and linux.

Still needed:
- Host name resolving
- Socket stream class


-- 
Christopher E. Miller
Feb 29 2004
next sibling parent =?ISO-8859-1?Q?Julio_Jim=E9nez?= <jujibo inicia.es> writes:
Vathix wrote:
 I guess this one could be considered socket.d 1.0. I've tested it pretty 
 well, even made a test server that accepts multiple connections, 
 listener.d included. It works good for windows and linux.
 
 Still needed:
 - Host name resolving
 - Socket stream class
 
 

Good work! i have tested in linux and writen some code for connecting to listener.d and run fine. thanks for the work... regards Julio Jiménez
Mar 01 2004
prev sibling next sibling parent reply "=?iso-8859-1?Q?Robert_M._M=FCnch?=" <robert.muench robertmuench.de> writes:
On Sun, 29 Feb 2004 18:38:35 -0500, Vathix <vathix dprogramming.com> wrote:

 I guess this one could be considered socket.d 1.0. I've tested it pretty
 well, even made a test server that accepts multiple connections,
 listener.d included. It works good for windows and linux.

 Still needed:
 - Host name resolving
 - Socket stream class

Will this code be added to the standard distribution? Robert
Mar 02 2004
parent Vathix <vathix dprogramming.com> writes:
Robert M. Münch wrote:

 On Sun, 29 Feb 2004 18:38:35 -0500, Vathix <vathix dprogramming.com> wrote:
 
 I guess this one could be considered socket.d 1.0. I've tested it pretty
 well, even made a test server that accepts multiple connections,
 listener.d included. It works good for windows and linux.

 Still needed:
 - Host name resolving
 - Socket stream class

Will this code be added to the standard distribution? Robert

I hope so. -- Christopher E. Miller
Mar 02 2004
prev sibling parent reply Sean Kelly <sean ffwd.cx> writes:
Vathix wrote:
 I guess this one could be considered socket.d 1.0. I've tested it pretty 
 well, even made a test server that accepts multiple connections, 
 listener.d included. It works good for windows and linux.

Just gave it a quick scan. It's not bad so far. A few things could probably be simplified (like toAddrString could be a single line calling inet_ntoa instead of all that string stuff), and you might want to consider using the newer functions that support IPv6, or at least making sure your interface will work seamlessly with the new connection methods. I say let people use it and push for Phobos inclusion once it's been used a bit and you've gotten some feedback. Sean
Mar 02 2004
parent Vathix <vathix dprogramming.com> writes:
Sean Kelly wrote:

 Vathix wrote:
 
 I guess this one could be considered socket.d 1.0. I've tested it 
 pretty well, even made a test server that accepts multiple 
 connections, listener.d included. It works good for windows and linux.

Just gave it a quick scan. It's not bad so far. A few things could probably be simplified (like toAddrString could be a single line calling inet_ntoa instead of all that string stuff), and you might want to consider using the newer functions that support IPv6, or at least making sure your interface will work seamlessly with the new connection methods. I say let people use it and push for Phobos inclusion once it's been used a bit and you've gotten some feedback. Sean

I have to say that I don't know much about IPv6. I would think all you need to do is set the AddressFamily to INET6 in Socket's constructor and make a new Address class such as IPv6Address that uses C's struct sockaddr differently; but I don't know so I'll look into it. Thanks. -- Christopher E. Miller
Mar 03 2004
prev sibling parent =?ISO-8859-1?Q?Julio_Jim=E9nez?= <jujibo inicia.es> writes:
Carlos Santander B. wrote:
 Carlos Santander B. wrote:
 
Vathix wrote:

Attached my code. I tried to make it work for linux but was unable
to test it, I don't know much about linux. I get this error when
running dmd on debian:
./dmd: error while loading shared libraries: libstdc++.so.5: cannot
open shared object file: No such file or directory
I tried to install libstdc++ libs but the error remains.

From my own experience, that means you have to update a lot of things, not only that one, and with very recent versions.
I'd appreciate it if someone could help by getting the code to work
for linux or helping me fix my problem :)


--
Christopher E. Miller
www.dprogramming.com
irc.dprogramming.com #D

I've attached a slightly modified socket.d. From what I can remember: - version(linux) instead of Linux - declared fd_set for BsdSockets - blocking in Socket class was only defined for Windows, although being used in both versions. Commented out the line - errno declared as extern(C) I think that's it. ----------------------- Carlos Santander Bernal

I tested it on RedHat 9, and it compiled without a problem, but when testing it, I got "Socket select error". Any ideas? ----------------------- Carlos Santander Bernal

I have tested it on Slackware and run fine with some changes.... At first, the "Socket select error" is right, because you haven't a http server listening at 127.0.0.1 port 80 The second is that Socket.select is not right implemented for linux (at first reading of socked.d) comment it on and will run ok. try this modified version for sockettest.d (watch the commented lines, real IP number and s.send string with a true http site.... ;-) : import socket; // dmd sockettest socket ws2_32.lib int main() { /+ //regular blocking Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.connect(new InternetAddress("127.0.0.1", 444)); s.send("Foo!"); printf("Local=%.*s, Remote=%.*s\r\n", s.localAddress().toString(), s.remoteAddress().toString()); s.close(); +/ /+ //nonblocking with select() Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.blocking = false; assert(s.blocking == false); SocketSet sset = new SocketSet(1); sset.add(s); s.connect(new InternetAddress("127.0.0.1", 444)); printf("select=%d\r\n", Socket.select(null, sset, null, 1000)); //wait for connection s.send(cast(dchar[])"Foo!"); sset.reset(1);import socket; // dmd sockettest socket ws2_32.lib int main() { /+ //regular blocking Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.connect(new InternetAddress("127.0.0.1", 444)); s.send("Foo!"); printf("Local=%.*s, Remote=%.*s\r\n", s.localAddress().toString(), s.remoteAddress().toString()); s.close(); +/ /+ //nonblocking with select() Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.blocking = false; assert(s.blocking == false); SocketSet sset = new SocketSet(1); sset.add(s); s.connect(new InternetAddress("127.0.0.1", 444)); printf("select=%d\r\n", Socket.select(null, sset, null, 1000)); //wait for connection s.send(cast(dchar[])"Foo!"); sset.reset(1); sset.add(s); printf("select=%d\r\n", Socket.select(null, sset, null, 1000)); //wait for send printf("Local=%.*s, Remote=%.*s\r\n", s.localAddress().toString(), s.remoteAddress().toString()); s.close(); +/ /+ //nonblocking listen() with select() Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.blocking = false; SocketSet sset = new SocketSet(1); sset.add(s); s.bind(new InternetAddress(444)); s.listen(10); printf("select=%d\r\n", Socket.select(sset, null, null)); //wait for accept with(s.accept()) { printf("Accepted connection from %.*s\r\n", remoteAddress().toString()); close(); } s.close(); +/ //nonblocking connect() to 127.0.0.1 port 80 with select() and display web page Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.blocking = false; SocketSet sset = new SocketSet(1); sset.add(s); s.connect(new InternetAddress("66.35.250.210", 80)); //if(!Socket.select(null, sset, null, 3000000)) //3secs // throw new Exception("Connection timed out."); // else printf("ok select 1\n"); s.send("GET / HTTP/1.1\r\nHost: bzflag.org\r\n\r\n"); char[1024] buf; int read; for(;;) { sset.reset(1); sset.add(s); //if(Socket.select(sset, null, null, 1000000)) //{ read = s.receive(buf); if(read) { printf("%.*s", buf[0 .. read]); } else { printf("\r\n\r\nConnection closed.\n"); break; } //} //else //{ // printf("\r\n\r\nRead timeout."); // break; //} } s.close(); return 0; } sset.add(s); printf("select=%d\r\n", Socket.select(null, sset, null, 1000)); //wait for send printf("Local=%.*s, Remote=%.*s\r\n", s.localAddress().toString(), s.remoteAddress().toString()); s.close(); +/ /+ //nonblocking listen() with select() Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.blocking = false; SocketSet sset = new SocketSet(1); sset.add(s); s.bind(new InternetAddress(444)); s.listen(10); printf("select=%d\r\n", Socket.select(sset, null, null)); //wait for accept with(s.accept()) { printf("Accepted connection from %.*s\r\n", remoteAddress().toString()); close(); } s.close(); +/ //nonblocking connect() to 127.0.0.1 port 80 with select() and display web page Socket s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.IP); s.blocking = false; SocketSet sset = new SocketSet(1); sset.add(s); s.connect(new InternetAddress("66.35.250.210", 80)); //if(!Socket.select(null, sset, null, 3000000)) //3secs // throw new Exception("Connection timed out."); // else printf("ok select 1\n"); s.send("GET / HTTP/1.1\r\nHost: bzflag.org\r\n\r\n"); char[1024] buf; int read; for(;;) { sset.reset(1); sset.add(s); //if(Socket.select(sset, null, null, 1000000)) //{ read = s.receive(buf); if(read) { printf("%.*s", buf[0 .. read]); } else { printf("\r\n\r\nConnection closed.\n"); break; } //} //else //{ // printf("\r\n\r\nRead timeout."); // break; //} } s.close(); return 0; }
Feb 28 2004