With the knowledge of the previous article, we will introduce the TCP protocol-based socket programming, because the socket needs to have the client and the server side, and now the implementation is about the server side of the simple program. The server is implemented using the Cfstream class.
This service side is the Command line Tool in Xcode as the service side, of course, you can also use the IPhone as a server, but to take advantage of other frameworks, such as Asyncsocket (https://github.com/ Roustem/asyncsocket), there is a UDP and TCP implementation of the Socket, the source program also has a lot of data flow operations, can be used as an in-depth understanding, of course, is more complex.
The following is a simple implementation of the TCP server Terminal program: (TCPSERVER.M)
#import <CoreFoundation/CoreFoundation.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT 9000void acceptcallback (Cfsocketref, Cfsocketcallbacktype, cfdataref, const void *, void *); void Writestreamclientcallback (Cfwritestreamref stream, Cfstreameventtype eventtype, void *); void Readstreamclientcallback (Cfreadstreamref stream, Cfstreameventtype eventtype,void *); int main (int argc, const char * arg) V[]) {/* Define a Server socket reference */Cfsocketref sserver; /* Create SOCKET Context */Cfsocketcontext CTX = {0, NULL, NULL, NULL, NULL}; /* Create the server socket TCP IPV4 setting callback function */sserver = Cfsocketcreate (NULL, Pf_inet, Sock_stream, Ipproto_tcp, Kcfsocketacceptcallback, (Cfsocketcallback) acceptcallback, &ctx); if (sserver = = NULL) return-1; /* Set whether to rebind flag */int yes = 1; /* Set the SOCKET property Sol_socket is the set TCP SO_REUSEADDR is Rebinding, yes is rebind */setsockopt (cfsocketgetnative (sserver), Sol_socket, So_ ReuseADDR, (void *) &yes, sizeof (yes)); /* Set port and address */struct sockaddr_in addr; memset (&addr, 0, sizeof (addr)); The Memset function makes a memory copy of the specified address Addr.sin_len = sizeof (addr); addr.sin_family = af_inet; Af_inet is set IPv4 Addr.sin_port = htons (port); htons function unsigned short integer converted to "network byte order" ADDR.SIN_ADDR.S_ADDR = Htonl (Inaddr_any); Inaddr_any has kernel allocation, htonl function unsigned long integer converted to "network byte order"/* copied from the specified byte buffer, an immutable Cfdata object */cfdataref address = cfdatacreate ( Kcfallocatordefault, (uint8*) &addr, sizeof (addr)); /* Set socket*/if (Cfsocketsetaddress (Sserver, (CFDATAREF) address)! = kcfsocketsuccess) {fprintf (stderr, "Socket Bind failed \ n "); Cfrelease (sserver); return-1; }/* Create a run Loop socket source */cfrunloopsourceref Sourceref = Cfsocketcreaterunloopsource (Kcfallocatordefault, SSE RVer, 0); /* The socket source is added to the run loop */Cfrunloopaddsource (Cfrunloopgetcurrent (), Sourceref, kcfrunloopcommonmodes); Cfrelease (SOURCEREF); printf ("Socket listening on port%d\n", port); /* Run loop */Cfrunlooprun (); After the}/* receives the client request, the callback function */void acceptcallback (cfsocketref socket, Cfsocketcallbacktype t ype, cfdataref address, const void *data, void *info) {cfreads Treamref readstream = NULL; Cfwritestreamref writestream = NULL; /* The data parameter implies that if it is a kcfsocketacceptcallback type, data is a pointer to the Cfsocketnativehandle type */Cfsocketnativehandle sock = * ( Cfsocketnativehandle *) data; /* Create read/write socket stream */Cfstreamcreatepairwithsocket (Kcfallocatordefault, sock, &readstre AM, &writestream); if (!readstream | |!writestream) {close (sock); fprintf (stderr, "cfstreamcreatepairwithsocket () failed \ n"); Return } cfstreamclientcontext Streamctxt = {0, NULL, NULL, NULL, NULL}; Register two callback functions Cfreadstreamsetclient (Readstream, KCfstreameventhasbytesavailable, Readstreamclientcallback, &streamctxt); Cfwritestreamsetclient (Writestream, Kcfstreameventcanacceptbytes, Writestreamclientcallback, &streamCtxt); Added to the Loop Cfreadstreamschedulewithrunloop (Readstream, Cfrunloopgetcurrent (), kcfrunloopcommonmodes); Cfwritestreamschedulewithrunloop (Writestream, Cfrunloopgetcurrent (), kcfrunloopcommonmodes); Cfreadstreamopen (Readstream); Cfwritestreamopen (Writestream); }/* read stream Operations client has data coming up when calling */void Readstreamclientcallback (Cfreadstreamref stream, Cfstreameventtype EventType, void* Clientcallbackinfo) {UInt8 buff[255]; Cfreadstreamref InputStream = stream; if (NULL! = InputStream) {cfreadstreamread (stream, buff, 255); printf ("Received data:%s\n", buff); Cfreadstreamclose (InputStream); Cfreadstreamunschedulefromrunloop (InputStream, Cfrunloopgetcurrent (), kcfrunloopcommonmodes); InputStream = NULL; }}/* Write stream operation client calls when reading data */void Writestreamclientcallback (Cfwritestreamref stream, Cfstreameventtype eventtype, void* clientCallBackInfo) {CFW Ritestreamref outputstream = stream; Output UInt8 buff[] = "Hello client!"; if (NULL! = outputstream) {cfwritestreamwrite (OutputStream, Buff, strlen ((const char*) buff) +1); Turn off the output stream Cfwritestreamclose (OutputStream); Cfwritestreamunschedulefromrunloop (OutputStream, Cfrunloopgetcurrent (), kcfrunloopcommonmodes); OutputStream = NULL; }}This program only involves relatively simple data flow operations, detailed data flow operation please refer to the asyncsocket source; At this point, a server has been implemented.
To be continued ...
Socket-based C/s structure network communication in iOS (medium)