Can't work with sockets

david ta-nospam-zz at gmx.at
Wed May 9 14:59:04 PDT 2007


/*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 at 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...");

}



More information about the Digitalmars-d mailing list