 |
- Author:
- larsivi (IP: 84.48.63.70)
- Timestamp:
- 08/26/09 14:50:48 (4 years ago)
- Comment:
Some adjustments
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TutInternetConnectivity
| v10 |
v11 |
|
| 1 | | [wiki:TutInternetConnectivityComments Leave Comments, Critiques, and Suggestions Here] |
|---|
| 2 | | |
|---|
| 3 | 1 | = Internet Connectivity Tutorial = |
|---|
| 4 | 2 | |
|---|
| 5 | | Tango provides classes for performing with multiple types of socket activities. The basic types of activities include connected socket communications, connectionless socket communications, and multicast socket communications. |
|---|
| | 3 | 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. |
|---|
| 6 | 4 | |
|---|
| 7 | | Connected communications require one side to open a port and the other side to connect to it. |
|---|
| 8 | | In Tango these two functionalities are in two separate classes. |
|---|
| 9 | | Both of these classes inheret from the [http://tango.dsource.org/docs/current/tango.net.Socket.html Socket] class. |
|---|
| | 5 | 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 [http://tango.dsource.org/docs/current/tango.net.Socket.html Socket] class. |
|---|
| 10 | 6 | |
|---|
| 11 | 7 | == Tcp Server == |
|---|
| 12 | | The [http://tango.dsource.org/docs/current/tango.net.ServerSocket.html ServerSocket] class implements the functionality needed to open a socket to inbound connections. |
|---|
| 13 | | Open a tcp/ip socket with tango is very easy, all you have to do is to import |
|---|
| | 8 | The [http://tango.dsource.org/docs/current/tango.net.ServerSocket.html ServerSocket] class implements the functionality needed to open a socket for inbound connections. |
|---|
| | 9 | Opening a TCP/IP socket using Tango is very easy, all you have to do is to import |
|---|
| 14 | 10 | |
|---|
| 15 | 11 | {{{ |
|---|
| | 12 | #!d |
|---|
| 16 | 13 | import tango.net.device.Socket; |
|---|
| 17 | 14 | import tango.net.InternetAddress; |
|---|
| 18 | 15 | }}} |
|---|
| 19 | 16 | |
|---|
| 20 | | The first one imports the ServerSocket class, the second one imports the class to generate a tcp/ip internet address. |
|---|
| 21 | | To create you can do the following: |
|---|
| | 17 | The first one imports the !ServerSocket class, the second one imports the class to generate a TCP/IP Internet address. |
|---|
| | 18 | To use you can do the following: |
|---|
| 22 | 19 | |
|---|
| 23 | 20 | {{{ |
|---|
| | 21 | #!d |
|---|
| 24 | 22 | //create a tcp/ip server on port 1234 tcp |
|---|
| 25 | 23 | auto server = new ServerSocket(new InternetAddress("localhost", 1234)); |
|---|
| 36 | 34 | |
|---|
| 37 | 35 | The [http://tango.dsource.org/docs/current/tango.net.Socket.html Socket] class implements the functionality needed to connect to an open socket. |
|---|
| 38 | | As with ServerSocket, using socket is very easy, just import: |
|---|
| | 36 | As with !ServerSocket, using socket is very easy, just import: |
|---|
| 39 | 37 | |
|---|
| 40 | 38 | {{{ |
|---|
| | 39 | #!d |
|---|
| 41 | 40 | import tango.net.device.Socket; |
|---|
| 42 | 41 | import tango.net.InternetAddress; |
|---|
| 43 | 42 | }}} |
|---|
| 44 | 43 | |
|---|
| 45 | | 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: |
|---|
| | 44 | 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: |
|---|
| 46 | 45 | |
|---|
| 47 | 46 | {{{ |
|---|
| | 47 | #!d |
|---|
| 48 | 48 | auto s = new Socket(); |
|---|
| 49 | 49 | |
|---|
| 57 | 57 | s.write(command); |
|---|
| 58 | 58 | |
|---|
| 59 | | //crate a buffer and an int variable to store received data and bytes received |
|---|
| | 59 | //create a buffer and an int variable to store received data and bytes received |
|---|
| 60 | 60 | char[1024] buf; |
|---|
| 61 | 61 | int bytes; |
|---|
| 66 | 66 | //display received data |
|---|
| 67 | 67 | Stdout("Received: ", buf[0..bytes]).newline; |
|---|
| 68 | | |
|---|
| 69 | 68 | }}} |
|---|
| 70 | 69 | |
|---|
| 71 | 70 | == Client and Server all together == |
|---|
| 72 | 71 | |
|---|
| 73 | | In [http://www.dsource.org/projects/tango/browser/trunk/example/networking/socketserver.d tango there is an example file] showing all the needed steps for a client + server application This is the file |
|---|
| | 72 | In [http://www.dsource.org/projects/tango/browser/trunk/example/networking/socketserver.d Tango there is an example file] showing all the needed steps for a client + server application, such as the following example: |
|---|
| 74 | 73 | |
|---|
| 75 | 74 | {{{ |
|---|
| | 75 | #!d |
|---|
| 76 | 76 | /******************************************************************************* |
|---|
| 77 | 77 | |
|---|
| 129 | 129 | }}} |
|---|
| 130 | 130 | |
|---|
| 131 | | Connectionless socket communications are preformed with the [http://tango.dsource.org/docs/current/tango.net.DatagramSocket.html DatagramConduit] class. |
|---|
| | 131 | Connectionless socket communications are performed with the [http://tango.dsource.org/docs/current/tango.net.DatagramSocket.html DatagramConduit] class. |
|---|
| 132 | 132 | |
|---|
| 133 | | Multicast socket communications are preformed with the [http://tango.dsource.org/docs/current/tango.net.MulticastSocket.html MulticastSocket] class. |
|---|
| | 133 | Multicast socket communications are performed with the [http://tango.dsource.org/docs/current/tango.net.MulticastSocket.html MulticastSocket] class. |
|
 |
 |
|
 |
Copyright © 2006-2013 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic