1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*@@
@header SocketUtils.h
@date 1991
@author John Shalf
@desc
@enddesc
@version $Header$
@@*/
#ifndef _SOCKETUTILS_H_
#define _SOCKETUTILS_H
#ifdef __cplusplus
extern "C"
{
#endif
/* #if defined(ANSI) || defined(__STDC__) */
#ifdef __STDC__
/*
TCP utilities library (1991)
Last modification Dec 1995
*/
/*==================================================================
** name: OpenClientSockTCP
** purpose: Given the hostname of the server and the portnumber of the
** service to connect to, it opens a TCP socket to the host and returns
** the file descriptor of the socket.
** parameters:
** hostname: the name of the server host as a character string (not ip number)
** port: The port number for the server.
** returns:
** integer file descriptor for low level io. Can convert to FILE *
** using the fdopen() system call.
**=================================================================*/
int Socket_TCPOpenClientSock(const char *hostname, int port);
int Socket_UDPOpenClientSock(const char *hostname, int port);
/*==================================================================
** name: OpenServerSockTCP
** purpose: Opens a passive TCP connection on "port". After opening
** the socket, the server is automatically set to listen on that socket.
** The programmer need only call accept() to allow connections by clients.
** parameters:
** port: The port number for the server.
** returns:
** integer file descriptor for low level io. Needs to remain a file
** descriptor to accept() client connections. Can convert the client
** connection file descriptor to a FILE * using fdopen();
**=================================================================*/
int Socket_TCPOpenServerSock(int port);
int Socket_UDPOpenServerSock(int port);
/*==================================================================
** name: TCPBlockingWrite
** purpose: Handles error conditions when writing to a file descriptor.
** If a buffer is too large to write in one block, this routine will
** divide the buffer into two segments and recursively call itself to
** send each of the halves.
** parameters:
** fd: file descriptor to write the buffer data to.
** buffer: an array of bytes write to the file stream.
** buflen: number of bytes in the buffer
** returns:
** either the number of characters written or constant describing
** why the write failed.
** notes: If you want the routine to print identifiable errors
** to stderr, define PERRORS.
**=================================================================*/
int Socket_TCPBlockingWrite(int fd, char *buffer, int buflen);
/*==================================================================
** name: TCPBlockingRead
** purpose: Handles error conditions when reading from a file descriptor.
** With TCP stream connections, the record size of the recieved stream
** may not coincide with the record written. This routine assembles
** a fragmented record into a single array by blocking until buflen
** characters are recieved, or write returns a premature EOF.
** parameters:
** fd: file descriptor to read the buffer data from.
** buffer: an array of bytes read from the file stream.
** buflen: number of bytes in the buffer
** returns:
** either the number of characters read into the buffer or constant describing
** why the read failed.
** notes: If you want the routine to print out identifiable errors
** to stderr, define PERRORS.
**=================================================================*/
int Socket_TCPBlockingRead(int fd,char *buffer,int buflen);
#else
int Socket_TCPOpenClientSock();
int Socket_TCPOpenServerSock();
int Socket_TCPBlockingWrite();
int Socket_TCPBlockingRead();
#endif
#define PERRORS
#ifndef __IN_HEADER
#include <sys/types.h>
#include <netinet/in.h>
#endif
#ifdef __cplusplus
}
#endif
#endif /* _SOCKETUTILS_H */
|