tcpsock.c

来自「用c/c++实现的一个CMPP API」· C语言 代码 · 共 252 行

C
252
字号
/******************************************************************** * File: TcpSock.c                                                  * * Description:                                                     * *      1、封装TCP函数                                              * * Functions:                                                       * *      int nTcpServer(int nPort)                                   * *      int nTcpClose(int nSock)                                    * *      int nTcpAccept(int nSock, char *pCltIp)                     * *      int nTcpSend(int nSock, const char *pBuf, int nLength)      * *      int nTcpRecv(int nSock, char *pBuf, int nLength,            * *                   int nTimeout)                                  * ******************************************************************** * Modify Log                                                       * * Date        Author            Description                        * * 2002-11-14  tangwn                                               * ********************************************************************///#include "../general.h"#include "../os.h"#include "TcpSock.h"/******************************************************************** * Function: nTcpServer                                             * * Description:                                                     * *      1、创建TCP服务器                                            * *      2、配置SOCKET属性                                           * * Input Param:                                                     * *      int       nPort                                             * * Output Param:                                                    * *      无                                                          * * Return:                                                          * *      >0        socket句柄                                        * *      <=0       错误                                              * ********************************************************************/int nTcpServer(int nPort){	int nSock = -1;	struct linger l;	struct sockaddr_in addr;	nSock = socket(AF_INET, SOCK_STREAM, 0);	if (nSock == -1)		return -1;	l.l_onoff = 1;	l.l_linger = 0;	if (setsockopt(nSock, SOL_SOCKET, SO_LINGER, (const char *)&l, sizeof(struct linger)) == -1)	{		nTcpClose(nSock);		return -1;	}	memset(&addr, 0, sizeof(struct sockaddr_in));	addr.sin_family = AF_INET;	addr.sin_port = htons((u_short)nPort);	addr.sin_addr.s_addr = INADDR_ANY;	if (bind(nSock, (const struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1)	{		nTcpClose(nSock);		return -1;	}	if (listen(nSock, DefBackLog) == -1)	{		nTcpClose(nSock);		return -1;	}	return nSock;}/******************************************************************** * Function: nTcpConnect                                            * * Description:                                                     * *      1、连接TCP服务器                                              * * Input Param:                                                     * *      const char *pServIp                                         * *      int       nPort                                             * * Output Param:                                                    * *      无                                                          * * Return:                                                          * *      >0        socket句柄                                         * *      <=0       错误                                               * ********************************************************************/int nTcpConnect(const char *pServIp, int nPort){	int nSock;	struct linger l;	struct sockaddr_in addr;	nSock = socket(AF_INET, SOCK_STREAM, 0);	if (nSock == -1)		return -1;	l.l_onoff = 1;	l.l_linger = 0;	if (setsockopt(nSock, SOL_SOCKET, SO_LINGER, (const char *)&l, sizeof(struct linger)) == -1)	{		nTcpClose(nSock);		return -1;	}	memset(&addr, 0, sizeof(struct sockaddr_in));	addr.sin_family = AF_INET;	addr.sin_port = htons((u_short)nPort);	addr.sin_addr.s_addr = inet_addr(pServIp);	if (connect(nSock, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)))	{		nTcpClose(nSock);		return -1;	}	return nSock;}/******************************************************************** * Function: nTcpClose                                              * * Description:                                                     * *      1、关闭连接                                                 * * Input Param:                                                     * *      int      nSock                                              * * Output Param:                                                    * *      无                                                          * * Return:                                                          * *      >=0        成功                                             * *      <0         错误                                             * ********************************************************************/int nTcpClose(int nSock){#ifdef WIN32	closesocket(nSock);#endif#ifdef _HPUX_SOURCE	close(nSock);#endif	return 0;}/******************************************************************** * Function: nTcpAccept                                             * * Description:                                                     * *      1、接收连接                                                 * * Input Param:                                                     * *      char     *pCltIp                                            * * Output Param:                                                    * *      char     *pCltIp                                            * * Return:                                                          * *      >0        socket句柄                                        * *      <=0       错误                                              * ********************************************************************/int nTcpAccept(int nSock, char *pCltIp){	int nCltSock = -1;	struct sockaddr_in addr;	int addlen = sizeof(struct sockaddr_in);	char *ip;	memset(&addr, 0, sizeof(struct sockaddr_in));	nCltSock = accept(nSock, (struct sockaddr *)&addr, &addlen);	if (nCltSock <= 0)		return -1;	ip = inet_ntoa(addr.sin_addr);	strcpy(pCltIp, ip);	return nCltSock;}/******************************************************************** * Function: nTcpSend                                               * * Description:                                                     * *      1、发送数据                                                 * * Input Param:                                                     * *      int        nSock                                            * *      const char *pBuf                                            * *      int        nLength                                          * * Output Param:                                                    * *      无                                                          * * Return:                                                          * *      >=0       发送的数据数目                                    * *      <         错误                                              * ********************************************************************/int nTcpSend(int nSock, const char *pBuf, int nLength){	char *ptr = (char *)pBuf;	int nWriteBytes = 0;	int nRetCode = 0;	while (nLength > 0)	{		nRetCode = send(nSock, ptr, nLength, 0);		if (nRetCode <= 0)			return -1;		nLength -= nRetCode;		ptr += nRetCode;		nWriteBytes += nRetCode;	}	return nWriteBytes;}/******************************************************************** * Function: nTcpRecv                                               * * Description:                                                     * *      1、接收数据                                                 * * Input Param:                                                     * *      int        nSock                                            * *      char       *pBuf                                            * *      int        nLength                                          * * Output Param:                                                    * *      char       *pBuf                                            * * Return:                                                          * *      >0        接收的数据数目                                    * *      0         超时                                              * *      <         错误                                              * ********************************************************************/int nTcpRecv(int nSock, char *pBuf, int nLength, int nTimeout){	int nReadBytes = 0;	struct timeval t_out;	fd_set fds;	int nRetCode = 0;	FD_ZERO(&fds);	FD_SET(nSock, &fds);	t_out.tv_sec = nTimeout;	t_out.tv_usec = 0;	nRetCode = select(nSock+1, &fds, NULL, NULL, &t_out);	if (nRetCode == -1)		return -1;	if (nRetCode == 0)		return 0;	while (nLength > 0)	{		nRetCode = recv(nSock, pBuf, nLength, 0);		if (nRetCode <= 0)			return -1;		nLength -= nRetCode;		pBuf += nRetCode;		nReadBytes += nRetCode;	}	return nReadBytes;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?