📄 conntcp.c
字号:
#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <fcntl.h>#include <sys/file.h>#ifndef INADDR_NONE#define INADDR_NONE 0xffffffff#endif/*********************************************************Function:int conntcp(host, service)Narrative: Connect to a specified TCP service on a specified host.Param: char *host - a pointer to host IP or host name char *service - a pointer to service port Return: NO return.*********************************************************/int conntcp(host, service) char *host; /* name of host to which connection is desired */ char *service; /* service associated with the desired port */{ int fd; fd = connsock(host, service, "tcp");}/* end of conntcp() *//*********************************************************Function:int connsock(host, service, protocol)Narrative: Allocate & connect a socket using TCP or UDP.Param: char *host - a pointer to host IP or host name char *service - a pointer to service port char *protocol - a pointer to protocolReturn: -1 Fail. >0 FD of socket.*********************************************************/static int connsock(host, service, protocol) char *host; /* name of host to which connection is desired */ char *service; /* service associated with the desired port */ char *protocol; /* name of protocol to use ("tcp" or "udp") */{ struct hostent *phe; /* pointer to host information entry */ struct servent *pse; /* pointer to service information entry */ struct protoent *ppe; /* pointer to protocol information entry */ struct sockaddr_in sin; /* an Internet endpoint address */ int s, type; /* socket descriptor and socket type */ memset((char *) &sin, 0, sizeof(sin)); sin.sin_family = AF_INET; /* Map service name to port number */ if (pse = getservbyname(service, protocol)) sin.sin_port = pse->s_port; else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) return (-1); /* Map host name to IP address, allowing for dotted decimal */ if (phe = gethostbyname(host)) memcpy((char *) &sin.sin_addr, phe->h_addr, phe->h_length); else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) return (-1); /* Map protocol name to protocol number */ if ((ppe = getprotobyname(protocol)) == 0) return (-1); /* Use protocol to choose a socket type */ if (strcmp(protocol, "udp") == 0) type = SOCK_DGRAM; else type = SOCK_STREAM; /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s < 0) return (-1); /* Connect the socket */ if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { shutdown(s, 2); close(s); return (-1); } /* fcntl(s,F_SETFL,O_NONBLOCK|O_NDELAY); */ return (s);}/* end of connsock() *//*********************************************************Function:int readsock(int sockfd, char *msg, int msglen)Narrative: Read message from socket.Param: int sockfd - socket id char *msg - a pointer to message to be read int msglen - length of messageReturn: 0 Fail. >0 Number of charactors to read.********************************************************/int readsock(int sockfd, char *msg, int msglen){ int n, rc; n = 0; while (n < msglen) { rc = read(sockfd, msg + n, msglen - n); if (rc == 0) return (0); if (rc > 0) n += rc; } return (n);}/*********************************************************Function:int writesock(int sockfd, char *msg, int msglen)Narrative: Allocate & connect a socket using TCP or UDP.Param: int sockfd - socket id char *msg - a pointer to message to be sent int msglen - length of messageReturn: 0 Fail. >0 Number of charactors to sent.*********************************************************/int writesock(int sockfd, char *msg, int msglen){ int n, wc; n = 0; while (n < msglen) { wc = write(sockfd, msg + n, msglen - n); if (wc == 0) return (0); if (wc > 0) n += wc; } return (n);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -