www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - socket applications

I'm stuck. I haven't been able to find a tutorial that outlines the
creation of client programs using the phobos library. The following 
program is my best guess as to how such a program could be written. 
I'm trying to send data to a program similar to the listner app that 
came with the d compiler. I'm still not certain about what steps are 
to be taken when creating a network connection, and I don't know what
functions to implement from the library. The code that follows below
doesn't work, and I would like to know why. What's wrong with it? How
could it be fixed? And, How could I achieve my goal with a different

import std.conv;
import std.socket;

Socket create_socket (Socket client_socket)

client_socket = new TcpSocket;

printf ("creating socket... \n");
assert (client_socket.isAlive);

return client_socket;

Socket initialize_socket (Socket client_socket)

client_socket.blocking	= false;
client_socket.listen (10);

printf ("initialized socket... \n");

return client_socket;
Socket bind_socket (Socket client_socket, ushort port)

printf ("binding socket to port... \n");
client_socket.bind (new InternetAddress (port));

return client_socket;
int form_connection (Socket client_socket, Address host_address_a)
uint 		connection_status;

connection_status = 1;

printf ("connecting to host... \n");
client_socket.connect (host_address_a);

return connection_status;
int transmit_information (Socket client_socket, Address host_address_a,
char[1024] message)
uint connection_status;

connection_status = 2;

printf ("transmitting message... \n");
client_socket.sendTo (message, host_address_a);

return connection_status;
int close_connection	(Socket client_socket)
SocketShutdown	BOTH;
uint 			connection_status;

connection_status = 0;

printf ("shutting down connection... \n");
client_socket.shutdown 	(BOTH); 		

printf ("closing socket... \n");
client_socket.close	();

return connection_status;
int main ()
uint		connection_status;
uint		host_address;
short		host_port;
Address	host_address_a;
ushort	port_address;
Socket	client_socket;
char[1024] 	message	= "transmission from client.\n";;

host_port		=	4444;	// adjust for real values
host_address	=	1223001;	// adjust for real values 

host_address_a.addr	= host_address;
host_address_a.port	= host_port;

printf ("beginning network operations... \n");

client_socket	= create_socket 	  	(client_socket);
client_socket	= initialize_socket 	(client_socket);
client_socket	= bind_socket	  	(client_socket, port);
connection_status	= form_connection 
( client_socket, host_address_a);
connection_status = transmit_information	
(client_socket, host_address_a, message);
connection_status	= close_connection	(client_socket);

printf ("concluded network operations... \n");

return 1;
Feb 21 2006