TCP socket utility functions, it provides simple functions that helps to build TCP client/server.
History: Date Author Comment 08-01-2005 AceLan Kao. Create it.
Definition in file socket.h.
#include <stdio.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <resolv.h>
#include <fcntl.h>
Include dependency graph for socket.h:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Defines | |
#define | MAX_CONNECTION 20 |
Functions | |
int | TCPBlockRead (int clientfd, char *buf, int size) |
block read from TCP socket | |
void | TCPClientClose (int sockfd) |
close the client socket | |
int | TCPClientConnect (const int clientfd, const char *addr, int port) |
connect to TCP server | |
int | TCPClientInit (int *clientfd) |
initialize TCP client | |
int | TCPNonBlockRead (int clientfd, char *buf, int size) |
non-block read from TCP socket | |
void | TCPServerClose (int sockfd) |
close the server socket | |
int | TCPServerInit (int port, int *serverfd) |
initialize TCP server | |
int | TCPServerWaitConnection (int serverfd, int *clientfd, char *clientaddr) |
wait client connect | |
int | TCPWrite (int clientfd, char *buf, int size) |
write to TCP socket |
|
|
|
block read from TCP socket
Definition at line 138 of file socket.c. Referenced by MBTCPBlockRead(). 00139 { 00140 int opts; 00141 opts = fcntl(clientfd, F_GETFL); 00142 opts = (opts & ~O_NONBLOCK); 00143 fcntl(clientfd, F_SETFL, opts); 00144 00145 return recv( clientfd, buf, size, 0); 00146 }
|
|
close the client socket
Definition at line 172 of file socket.c.
|
|
connect to TCP server
Connecting to server Definition at line 96 of file socket.c. Referenced by MBTCPClientConnect(). 00097 { 00098 struct sockaddr_in dest; 00099 00101 bzero(&dest, sizeof(dest)); 00102 dest.sin_family = PF_INET; 00103 dest.sin_port = htons( port); 00104 inet_aton( addr, &dest.sin_addr); 00105 00107 return connect(clientfd, (struct sockaddr*)&dest, sizeof(dest)); 00108 }
|
|
initialize TCP client
Definition at line 80 of file socket.c. Referenced by MBTCPClientInit().
|
|
non-block read from TCP socket
Definition at line 119 of file socket.c. Referenced by MBTCPNonBlockRead(). 00120 { 00121 int opts; 00122 opts = fcntl(clientfd, F_GETFL); 00123 opts = (opts | O_NONBLOCK); 00124 fcntl(clientfd, F_SETFL, opts); 00125 00126 return recv( clientfd, buf, size, 0); 00127 }
|
|
close the server socket
Definition at line 184 of file socket.c.
|
|
initialize TCP server
initialize structure dest Assign a port number to socket Definition at line 28 of file socket.c. Referenced by MBTCPServerInit(). 00029 { 00030 struct sockaddr_in dest; 00031 00033 *serverfd = socket(PF_INET, SOCK_STREAM, 0); 00034 00036 bzero((void*)&dest, sizeof(dest)); 00037 dest.sin_family = PF_INET; 00038 dest.sin_port = htons( port); 00039 00040 dest.sin_addr.s_addr = INADDR_ANY; 00041 00043 bind( *serverfd, (struct sockaddr*)&dest, sizeof(dest)); 00044 00045 return *serverfd; 00046 }
|
|
wait client connect
Wait and Accept connection Definition at line 57 of file socket.c. Referenced by MBTCPServerWaitConnection(). 00058 { 00059 struct sockaddr_in client_addr; 00060 socklen_t addrlen = sizeof(client_addr); 00061 00063 listen( serverfd, 20); 00064 00066 *clientfd = accept(serverfd, (struct sockaddr*)&client_addr, &addrlen); 00067 00068 strcpy( clientaddr, ( const char *)inet_ntoa( client_addr.sin_addr)); 00069 00070 return *clientfd; 00071 }
|
|
write to TCP socket
Definition at line 157 of file socket.c. Referenced by MBTCPWrite().
|