Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Internet Connectivity Tutorial

Tango provides classes for performing multiple types of socket activities. The basic types of activities include connected socket communications, connectionless socket communications, and multicast socket communications.

Connected communications require one side to open a port and the other side to connect to it. In Tango these two functionalities are in two separate classes, both inhereting from the Socket class.

Tcp Server

The ServerSocket class implements the functionality needed to open a socket for inbound connections. Opening a TCP/IP socket using Tango is very easy, all you have to do is to import

import tango.net.device.Socket;
import tango.net.InternetAddress;

The first one imports the ServerSocket class, the second one imports the class to generate a TCP/IP Internet address. To use you can do the following:

//create a tcp/ip server on port 1234 tcp
auto server = new ServerSocket(new InternetAddress("localhost", 1234));
//enter into an infinite loop to process all the clients who will connect to us	
while(1){
	auto client = server.accept();
//client is a Socket class and it is the socket with the accepted client
// to know how to use it read the following paragraph			
}

Tcp Client

The Socket class implements the functionality needed to connect to an open socket. As with ServerSocket, using socket is very easy, just import:

import tango.net.device.Socket;
import tango.net.InternetAddress;

The first line import the Socket class which we are going to use, the second one imports the class to generate a valid TCP/IP address. To connect to a server you can do the following:

auto s = new Socket();  

//connect to localhost port 1234 
s.connect(new InternetAddress ("127.0.0.1", 1234));

//set a string to send
char[] command = "hello world\n";

//send the string via the created socket
s.write(command);

//create a buffer and an int variable to store received data and bytes received
char[1024] buf;
int bytes;

//receive data from the socket, put it in buf and trace how many characters have been written
bytes = s.read(buf);

//display received data
Stdout("Received: ", buf[0..bytes]).newline;

Client and Server all together

In Tango there is an example file showing all the needed steps for a client + server application, such as the following example:

/*******************************************************************************

        Shows how to create a basic socket server, and how to talk to
        it from a socket client. Note that both the server and client
        are entirely simplistic, and therefore this is for illustration
        purposes only. See HttpServer for something more robust.

*******************************************************************************/

private import  tango.core.Thread;

private import  tango.io.Console;

private import  tango.net.device.Socket;

/*******************************************************************************

        Create a socket server, and have it respond to a request

*******************************************************************************/

void main()
{
        const int port = 8080;
 
        // thread body for socket-listener
        void run()
        {       
                auto server = new ServerSocket (port);
                
                // wait for requests
                auto request = server.accept;

                // write a response 
                request.output.write ("server replies 'hello'");
        }

        // start server in a separate thread, and wait for it to start
        (new Thread (&run)).start;
        Thread.sleep (0.250);

        // make a connection request to the server
        auto request = new Socket;
        request.connect ("localhost", port);

        // wait for and display response (there is an optional timeout)
        char[64] response;
        auto len = request.input.read (response);
        Cout (response[0..len]).newline;

        request.close;
}

Connectionless socket communications are performed with the DatagramConduit class.

Multicast socket communications are performed with the MulticastSocket class.