Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

socket.h File Reference


Detailed Description

Socket API header file.

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.

Author:
AceLan Kao.(acelan_kao@moxa.com.tw)

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


Define Documentation

#define MAX_CONNECTION   20
 

Definition at line 29 of file socket.h.


Function Documentation

int TCPBlockRead int  clientfd,
char *  buf,
int  size
 

block read from TCP socket

Parameters:
clientfd socket fd
buf input buffer
size buffer size
Returns:
the length of read data

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 }

void TCPClientClose int  sockfd  ) 
 

close the client socket

Parameters:
sockfd client socket fd
Returns:
none

Definition at line 172 of file socket.c.

00173 {
00174         close( sockfd);
00175 }

int TCPClientConnect const int  clientfd,
const char *  addr,
int  port
 

connect to TCP server

Parameters:
clientfd client socket fd
addr server address
port server port number
Returns:
return zero for success, on error -1 is returned
initialize value in dest

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 }

int TCPClientInit int *  clientfd  ) 
 

initialize TCP client

Parameters:
clientfd client socket fd
Returns:
return client socked fd for success, on error return error code

Definition at line 80 of file socket.c.

Referenced by MBTCPClientInit().

00081 {
00082         *clientfd = socket(PF_INET, SOCK_STREAM, 0);
00083 
00084         return *clientfd;
00085 }

int TCPNonBlockRead int  clientfd,
char *  buf,
int  size
 

non-block read from TCP socket

Parameters:
clientfd socket fd
buf input buffer
size buffer size
Returns:
the length of read data

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 }

void TCPServerClose int  sockfd  ) 
 

close the server socket

Parameters:
sockfd server socket fd
Returns:
none

Definition at line 184 of file socket.c.

00185 {
00186         close( sockfd);
00187 }

int TCPServerInit int  port,
int *  serverfd
 

initialize TCP server

Parameters:
port port number for socket
serverfd server socket fd
Returns:
return server socked fd for success, on error return error code
create socket , same as client

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 }

int TCPServerWaitConnection int  serverfd,
int *  clientfd,
char *  clientaddr
 

wait client connect

Parameters:
serverfd server socket fd
clientfd client socket fd
clientaddr client address which connect to server
Returns:
return client fd, on error return error code
make it listen to socket

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 }

int TCPWrite int  clientfd,
char *  buf,
int  size
 

write to TCP socket

Parameters:
clientfd socket fd
buf output buffer
size output string length
Returns:
the length of the actual written data

Definition at line 157 of file socket.c.

Referenced by MBTCPWrite().

00158 {
00159         int len= 0;
00160         len= send( clientfd, buf, size, 0);
00161 
00162         return len;
00163 }


Generated on Thu Oct 6 09:14:09 2005 for Example Modbus Library by  doxygen 1.4.4