www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't work with sockets

reply niovol <niovol gmail.com> writes:
I tried to make a program which can simply send messages and receive ones
between 2 programs. I used DFL to make a form, buttons etc. I had 2 socket
variables - remote_sock for server and sock for both server and client; i think
the connection goes right, because of after executing remote_sock =
sock.accept()   remote_sock.remoteAddress() has a right address. But I can't
send or receive any message. I deal with sock variable in the client program
and remote_sock in the server program. To send I used send(void[]) method, to
receive - receive(void[]). After sending I don't receive anything. Can anybody
tell me where I was wrong? Are there some examples on using sockets with DFL?
May 06 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Niovol,

 I tried to make a program which can simply send messages and receive
 ones between 2 programs. I used DFL to make a form, buttons etc. I had
 2 socket variables - remote_sock for server and sock for both server
 and client; i think the connection goes right, because of after
 executing remote_sock = sock.accept()   remote_sock.remoteAddress()
 has a right address. But I can't send or receive any message. I deal
 with sock variable in the client program and remote_sock in the server
 program. To send I used send(void[]) method, to receive -
 receive(void[]). After sending I don't receive anything. Can anybody
 tell me where I was wrong? Are there some examples on using sockets
 with DFL?
 
if you are working under phobos this is what I would use client side Stream str = new SocketStream(new TcpSocket((new InternetAddress("whoever", port))); server side auto soc = new TcpSocket(); soc.bind(new InternetAddress("whoever", port)); soc.listen(1); Stream str = new SocketStream(soc.accept()); If you point those at each other you should get a pair of I/O streams that can talk to each other. (code not tested, working from memory)
May 07 2007
parent reply Niovol <niovol gmail.com> writes:
Thanks. But I don't know how to receive messages real-time. And how to read all
the stream. Can anybody help me?

BCS Wrote:

 Reply to Niovol,
 
 I tried to make a program which can simply send messages and receive
 ones between 2 programs. I used DFL to make a form, buttons etc. I had
 2 socket variables - remote_sock for server and sock for both server
 and client; i think the connection goes right, because of after
 executing remote_sock = sock.accept()   remote_sock.remoteAddress()
 has a right address. But I can't send or receive any message. I deal
 with sock variable in the client program and remote_sock in the server
 program. To send I used send(void[]) method, to receive -
 receive(void[]). After sending I don't receive anything. Can anybody
 tell me where I was wrong? Are there some examples on using sockets
 with DFL?
 
if you are working under phobos this is what I would use client side Stream str = new SocketStream(new TcpSocket((new InternetAddress("whoever", port))); server side auto soc = new TcpSocket(); soc.bind(new InternetAddress("whoever", port)); soc.listen(1); Stream str = new SocketStream(soc.accept()); If you point those at each other you should get a pair of I/O streams that can talk to each other. (code not tested, working from memory)
May 09 2007
next sibling parent BCS <BCS pathlink.com> writes:
Niovol wrote:
 Thanks. But I don't know how to receive messages real-time. And how to read
all the stream. Can anybody help me?
 
you can use both str's like files streams*. they operate in real-time. Think of them as data-pipes and you will be mostly correct. Data that is written to one stream can be read from the other. The only potential gotcha I known of is that it is all treated as one stream (no "messages" or sections or whatever) so you need to find where things end your self. If that doesn't cover it, I'll need to know more about what you are doing to be of more help. *http://digitalmars.com/d/phobos/std_stream.html
 BCS Wrote:
 
client side
Stream str=new SocketStream(new TcpSocket((new
InternetAddress("whoever",port)));

server side

auto soc = new TcpSocket();
soc.bind(new InternetAddress("whoever", port));
soc.listen(1);
Stream str = new SocketStream(soc.accept());
May 09 2007
prev sibling parent david <ta-nospam-zz gmx.at> writes:
/*Niovol:
 Thanks. But I don't know how to receive messages real-time. And how 
to read all the stream. Can anybody help me?
 BCS Wrote:

 Reply to Niovol,

 I tried to make a program which can simply send messages and receive
 ones between 2 programs. I used DFL to make a form, buttons etc. I had
 2 socket variables - remote_sock for server and sock for both server
 and client; i think the connection goes right, because of after
 executing remote_sock = sock.accept()   remote_sock.remoteAddress()
 has a right address. But I can't send or receive any message. I deal
 with sock variable in the client program and remote_sock in the server
 program. To send I used send(void[]) method, to receive -
 receive(void[]). After sending I don't receive anything. Can anybody
 tell me where I was wrong? Are there some examples on using sockets
 with DFL?
if you are working under phobos this is what I would use client side Stream str = new SocketStream(new TcpSocket((new
InternetAddress("whoever",
 port)));


 server side

 auto soc = new TcpSocket();
 soc.bind(new InternetAddress("whoever", port));
 soc.listen(1);
 Stream str = new SocketStream(soc.accept());

 If you point those at each other you should get a pair of I/O 
streams that
 can talk to each other.

 (code not tested, working from memory)
Since I just wrote some irc stuff, here's a guide (just some trial&error, so if someone sees mistakes or improvements, please feel free to comment). If you're on windows, you compile by dmd mysource.d ws2_32.lib on linux, just dmd mysource.d (haven't tested it with this prototype but some advanced version, so it should be fine) Since I played only with the client side I can't comment on the server part, but this should be enough to get you started. -> Actually, this whole message is a .d file, just copy it, compile&run! (it will connect to chat.freenode.net (takes a few seconds), display MOTD and join #d but you won't be able to do anything but see the raw messages, type CTRL-C to quit) */ //--- Well, as an example for the i/o (don't rely on the here display irc protocol): //--- first, you need a bunch of imports import std.stream, std.socket, std.socketstream, std.stdio, std.thread; //--- some global variables bool quit = false; Stream ircstream; //--- for the real-time receiving of messages, use a thread (here the function): int myOutputThreadFunction(void* notused) { while(!quit) // use some intern bool to check { char[] line = ircstream.readLine(); writefln("%s", line); } return 0; } //--- then in main: void main() { Socket ircsock = new TcpSocket(new InternetAddress("chat.freenode.net", 6667)); ircstream = new SocketStream(ircsock); char[] msg = "USER buf any one.com IDbuf IDbuf \n"; ircstream.writeString(msg); msg = "NICK buf \n"; ircstream.writeString(msg); msg = "JOIN #d \n"; ircstream.writeString(msg); //--- then for the message receiving, you start up a thread Thread myOutputThread = new Thread(&myOutputThreadFunction, null); myOutputThread.start(); // put your user interface here and set quit=true on user's wish while(!quit) { } myOutputThread.wait(500); ircstream.close(); ircsock.shutdown(SocketShutdown.BOTH); ircsock.close(); writefln("bye..."); }
May 09 2007