Object | +---DSocket
The DSocket class implements a number of methods for using network sockets.
#include <stdio.h> #include "ofc/DSocket.h" #include "ofc/DSocketAddress.h" #include "ofc/DData.h" int main(int argc, char *argv[]) { DSocket *client = [DSocket new]; DInetSocketAddress *addr = [DInetSocketAddress new]; // Setup a client [addr loopback :7000]; // Setup an address to localhost, port 7000 printf("Open the socket ..\n"); [client open :[addr family] :DSK_STREAM :[DSocket protocol :"tcp"]]; // Open the socket for tcp [client reuseAddr :YES]; // Socketoption: Reuse the socket printf("Wait for connection with the server..\n"); if ([client connect :addr]) // Try to connect to the server { DData *received; printf("Send data to the server..\n"); if (![client send :"Hello there .." :14 :0]) // Send data to server printf("Error sending data to server:%d\n", [client error]); printf("Receive data from the server..\n"); received = [client recv :256 :0]; // Receive data from server if (received != nil) { printf("Length of received data:%ld\n", [received length]); [received free]; } else printf("Error receiving data from server:%d\n", [client error]); printf("Close the connection..\n"); [client shutdown :DSK_SHUT_RDWR]; // Shutdown connection [client close]; // Close socket } else printf("No connection to the server:%d\n", [client error]); [client free]; // Cleanup [addr free]; return 0; }