www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - socket applications

reply lee <lee_member pathlink.com> writes:
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 takken 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
design?

Thanks for all the help so far;
Lee

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
parent reply BCS <BCS_member pathlink.com> writes:
here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

         InternetAddress toAdd =  new InternetAddress(Address, port);

         Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

         toSoc.connect(toAdd);

         SocketStream st = new SocketStream(toSoc);

		// what to send
         char[] req = "Show me the money!!";

         st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
         while(true)
                 writef(st.getc);

         st.close();
}

---------
and here's a quick sever side
------------
import std.socketstream;
import std.socket;
import std.stream;
import std.stdio;


void main()
{
	int Port = GetPort();

	InternetAddress fromAdd = new InternetAddress(
			InternetAddress.ADDR_ANY, Port);

	Socket fromSoc = new Socket(
			AddressFamily.INET,
			SocketType.STREAM,
			ProtocolType.IP);
	Socket to;

	fromSoc.bind(fromAdd);

	writef("waiting...\n");
	fromSoc.listen(1);

	to = fromSoc.accept();
	writef("\nconnecting to %s\n", to.remoteAddress.toString);

	t1 = new Thread(cast(int function(void*))&Connect, cast(void*)to);
	t1.start();

	SocketStream st = new SocketStream(to);

	char[] buf;

	st.readBlock(buf.ptr, buf.length);

	// echo the input
	writef(buf, buf, \n);

	// end it back out
	st.writeBlock(buf.ptr, buf.length);
}


I haven't tested them but they come from somthing that did work

lee wrote:
 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 takken 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
 design?
 
 Thanks for all the help so far;
 Lee

Feb 21 2006
next sibling parent lee <lee_member pathlink.com> writes:
In article <dtgbki$2omn$1 digitaldaemon.com>, BCS says...
here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

         InternetAddress toAdd =  new InternetAddress(Address, port);

         Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

         toSoc.connect(toAdd);

         SocketStream st = new SocketStream(toSoc);

		// what to send
         char[] req = "Show me the money!!";

         st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
         while(true)
                 writef(st.getc);

         st.close();
}

---------
and here's a quick sever side
------------
import std.socketstream;
import std.socket;
import std.stream;
import std.stdio;


void main()
{
	int Port = GetPort();

	InternetAddress fromAdd = new InternetAddress(
			InternetAddress.ADDR_ANY, Port);

	Socket fromSoc = new Socket(
			AddressFamily.INET,
			SocketType.STREAM,
			ProtocolType.IP);
	Socket to;

	fromSoc.bind(fromAdd);

	writef("waiting...\n");
	fromSoc.listen(1);

	to = fromSoc.accept();
	writef("\nconnecting to %s\n", to.remoteAddress.toString);

	t1 = new Thread(cast(int function(void*))&Connect, cast(void*)to);
	t1.start();

	SocketStream st = new SocketStream(to);

	char[] buf;

	st.readBlock(buf.ptr, buf.length);

	// echo the input
	writef(buf, buf, \n);

	// end it back out
	st.writeBlock(buf.ptr, buf.length);
}


I haven't tested them but they come from somthing that did work

I managed to compile the segments above, and they look promising. I was wondering if anyone could outline the steps needed to implement a socket application that used RAW socket types. The documentation that came with my version of the D compiler doesn't go into detail about their use. Thanks Lee
Feb 22 2006
prev sibling parent reply lee <lee_member pathlink.com> writes:
In article <dtgbki$2omn$1 digitaldaemon.com>, BCS says...
here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

         InternetAddress toAdd =  new InternetAddress(Address, port);

         Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

         toSoc.connect(toAdd);

         SocketStream st = new SocketStream(toSoc);

		// what to send
         char[] req = "Show me the money!!";

         st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
         while(true)
                 writef(st.getc);

         st.close();
}

---------
and here's a quick sever side
------------
import std.socketstream;
import std.socket;
import std.stream;
import std.stdio;


void main()
{
	int Port = GetPort();

	InternetAddress fromAdd = new InternetAddress(
			InternetAddress.ADDR_ANY, Port);

	Socket fromSoc = new Socket(
			AddressFamily.INET,
			SocketType.STREAM,
			ProtocolType.IP);
	Socket to;

	fromSoc.bind(fromAdd);

	writef("waiting...\n");
	fromSoc.listen(1);

	to = fromSoc.accept();
	writef("\nconnecting to %s\n", to.remoteAddress.toString);

	t1 = new Thread(cast(int function(void*))&Connect, cast(void*)to);
	t1.start();

	SocketStream st = new SocketStream(to);

	char[] buf;

	st.readBlock(buf.ptr, buf.length);

	// echo the input
	writef(buf, buf, \n);

	// end it back out
	st.writeBlock(buf.ptr, buf.length);
}


I haven't tested them but they come from somthing that did work

I managed to compile the segments above, and they look promising. I was wondering if anyone could outline the steps needed to implement a socket application that used RAW socket types. The documentation that came with my version of the D compiler doesn't go into detail about their use. Thanks Lee
Feb 22 2006
parent reply lee <lee_member pathlink.com> writes:
here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

         InternetAddress toAdd =  new InternetAddress(Address, port);

         Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

         toSoc.connect(toAdd);

         SocketStream st = new SocketStream(toSoc);

		// what to send
         char[] req = "Show me the money!!";

         st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
         while(true)
                 writef(st.getc);

         st.close();
}


I've just tested the above code. the functions Getport and GetAddress are not defined in the included header files. I replaced them with constant values, and managed to get a succesfull compilation. Unfortunately however, I tried executing the client program on one of two machines, and it failed to connect successfully with the second. The second system was running the listener application that came with the D compiler. The program seems to run fine, and I'm sure that the correct address and port number was entered into the client program. If anyone could determine what's wrong with the example given above, and / or give a reference, I would appreciate it. Thanks, lee
Feb 22 2006
parent reply BCS <BCS_member pathlink.com> writes:
lee wrote:
here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

        InternetAddress toAdd =  new InternetAddress(Address, port);

        Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

        toSoc.connect(toAdd);

        SocketStream st = new SocketStream(toSoc);

		// what to send
        char[] req = "Show me the money!!";

        st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
        while(true)
                writef(st.getc);

        st.close();
}


I've just tested the above code. the functions Getport and GetAddress are not defined in the included header files. I replaced them with constant values, and managed to get a succesfull compilation. Unfortunately however, I tried executing the client program on one of two machines, and it failed to connect successfully with the second. The second system was running the listener application that came with the D compiler. The program seems to run fine, and I'm sure that the correct address and port number was entered into the client program. If anyone could determine what's wrong with the example given above, and / or give a reference, I would appreciate it. Thanks, lee

whatever. I known they worked a while ago... Hmm...
Feb 22 2006
parent Alex Kravetz <Alex_member pathlink.com> writes:
In article <dtiqnr$8e0$2 digitaldaemon.com>, BCS says...
lee wrote:
here's a quick client side program:
---------------
import std.socket;
import std.stream;
import std.socketstream;
import std.stdio;
import std.conv;

void main()
{
	char[] Address = GetAddress();
	int Port = GetPort();

        InternetAddress toAdd =  new InternetAddress(Address, port);

        Socket toSoc = new Socket(
		AddressFamily.INET,
		SocketType.STREAM,
		ProtocolType.IP);

        toSoc.connect(toAdd);

        SocketStream st = new SocketStream(toSoc);

		// what to send
        char[] req = "Show me the money!!";

        st.writeBlock(req.ptr, req.length);

		// echo everyting that you can
        while(true)
                writef(st.getc);

        st.close();
}


I've just tested the above code. the functions Getport and GetAddress are not defined in the included header files. I replaced them with constant values, and managed to get a succesfull compilation. Unfortunately however, I tried executing the client program on one of two machines, and it failed to connect successfully with the second. The second system was running the listener application that came with the D compiler. The program seems to run fine, and I'm sure that the correct address and port number was entered into the client program. If anyone could determine what's wrong with the example given above, and / or give a reference, I would appreciate it. Thanks, lee

whatever. I known they worked a while ago... Hmm...

Feb 22 2006