📄 uici.c
字号:
/******************************************************************** * name: uici.c * desc: SOCKETS'S UICI * date: 2002.8.17 * author:wu qian ********************************************************************/#include <stdio.h>#include <string.h>#include <unistd.h>#include <signal.h>#include <netdb.h>#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <errno.h>#include <arpa/inet.h>#include "uici.h"#define MAXBACKLOG 10/* return 1 if error, 0 if ok */int u_ignore_sigpipe(){ struct sigaction act; if (sigaction(SIGPIPE, (struct sigaction *)NULL, &act) < 0) return 1; if (act.sa_handler == SIG_DFL) { act.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &act, (struct sigaction *)NULL) < 0) return 1; } return 0;}/* * u_open * parameter: * s = number of port to bind to * returns:file descriptor if successful and -1 on error */int u_open(u_port_t port){ int sock; struct sockaddr_in server; if ( (u_ignore_sigpipe() != 0) || ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) ) return -1; server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons ((short)port); if ( (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) || (listen(sock,MAXBACKLOG) < 0)) return -1; return sock;}/* * u_listen * Listen for a request from a particular host on a specified port. * * parameters: fd, hostn */int u_listen(int fd, char *hostn){ struct sockaddr_in net_client; int len = sizeof(struct sockaddr); int retval; struct hostent *hostptr; while ( ((retval = accept(fd, (struct sockaddr *) (&net_client), &len)) == -1 ) && (errno == EINTR)); if (retval == -1) return retval; hostptr = gethostbyaddr((char *)&(net_client.sin_addr.s_addr), 4, AF_INET); if (hostptr == NULL) ; //strcpy(hostn, "unknow"); //strcpy(hostn, inet_ntoa(*((struct in_addr *)((*hostptr).h_addr_list[0]))) ); else ; //strcpy(hostn, (*hostptr).h_name); return retval;}/* * u_connect * Initiate communication with a remote server. * parameters: port, inept */int u_connect(u_port_t port, char *hostn){ struct sockaddr_in server; struct hostent *hp; int sock; int retval; if ( (u_ignore_sigpipe() != 0) || !(hp = gethostbyname(hostn)) || ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) ) return -1; memcpy((char *)&server.sin_addr, hp->h_addr_list[0], hp->h_length); server.sin_port = htons((short)port); server.sin_family = AF_INET; while ( ((retval = connect(sock, (struct sockaddr *)&server, sizeof(server))) == -1) && (errno == EINTR) ); if (retval == -1) { close(sock); return -1; } return sock;}/* * u_close * Close communication for the given file descriptor. * parameters: fd * returns: a negative value indicates an error occurred */int u_close(int fd){ return close(fd);}/* * u_read * Retrieve information from a file descriptor opened by u_open. * parameters: fd, buf, nbyte */ssize_t u_read(int fd, void *buf, size_t size){ ssize_t retval; while (retval = read(fd, buf, size), retval == -1 && errno == EINTR); return retval;}/* * u_write * Send information on a file descriptor opened by u_open * parameters: fd, buf, nbyte */ssize_t u_write(int fd, void *buf, size_t size){ ssize_t retval; while (retval = write(fd,buf,size),retval == -1 && errno == EINTR); return retval;}/* * u_error * Display an error message in the manner of perror or t_error. * parameters: s = string to be prepended to system error message * returns: 0 */void u_error(char *s){ perror(s);}/* * u_sync * This function must be called after an exec or a dup to attach an opened file descriptor * */int u_sync(int fd){ return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -