⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.c

📁 linux下串口的访问
💻 C
字号:
//#include "desay.h"
#include "isocket.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>



static int nSocketLocal( int nType );
static int nConnectLocal(int nSocketfd, char * IpAddr, int port );

static int nBindLocal( int nSocketfd, int port );
static int nListen(int nSocketfd, int nBacklog);


/*
tcp server
   nServerSocket: socket--->bind--->listen
   nAccept	: accept
   nSend/nRecv	: send/recv

tcp client
   nClientSocket: socket----------->connect
   nRecv/nRecv	: send/recv
*/


//	addr.sin_family=AF_INET;
//	addr.sin_addr.s_addr = inet_addr("10.6.194.60");
//	addr.sin_port=htons(1000); 
//	nSockfd = socket(AF_INET, nType, 0 );


/*****************************************************************************************  
Program ID:	nClientSocket                                    
                                                    
Description:                 
        create a socket, bind it and listen socket.  
	
Parameters:	
	

Return Value:                                                              
        a new socket fd
	
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/4/25		haorui		Initial Release                  
****************************************************************************************/
	
/* nType : SOCK_STREAM or SOCK_DGRAM  */
int nClientSocket( int nType, char * sIpAddr, int port )
{
	int nRet;
	int nNewSocket;

	if ( sIpAddr == NULL || 
	     (nType != SOCK_STREAM && nType != SOCK_DGRAM ) )
	{
		return -1;
	}
	
	/* create a socket */ 

	if ( ( nNewSocket=nSocketLocal( nType ) ) < 0 )
	{
		return -1;
	}
	//PrintInt( nNewSocket );
	
	if ( ( nRet=nConnectLocal( nNewSocket, sIpAddr, port ) ) < 0 )
	{
		close ( nNewSocket );
		return -2;
	}

	return nNewSocket;
}
/*****************************************************************************************  
Program ID:	nServerSocket	                                   
                                                    
Description:                 
	create a socket, bind it and listen socket.

Parameters:	
	
Return Value:                                                              
	
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/4/25		haorui		Initial Release                  
****************************************************************************************/

int nServerSocket( int nType,  int port )
{
	int nRet;
	int nNewSocket;

// 	if ( sIpAddr == NULL )
// 	{
// 		return -1 ;
// 	}

	//unlink( sPath );
	if ( ( nNewSocket=nSocketLocal( nType ) ) < 0 )
	{
		return nNewSocket-10;
	}
	if ( ( nRet=nBindLocal( nNewSocket, port ) ) < 0 )
	{
		close ( nNewSocket );
		return -2;
	}
	if ( ( nRet=nListen( nNewSocket, SOMAXCONN ) ) < 0 )
	{
		close ( nNewSocket );
		return -3;
	}
	return nNewSocket;
}

/*****************************************************************************************  
Program ID:	nAccept                                    
                                                    
Description:                 
        accept a new socket.  
	
Parameters:	
	int nSocketfd	:

Return Value:                                                              
        a new socket fd
	
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/4/25		haorui		Initial Release                  
****************************************************************************************/

int nAccept(int nSocketfd )
{
	struct sockaddr_in rAddr;
	int	nNewfd;
	int	nSize;

	if ( nSocketfd <0 )
	{
		return -1;
	}

	nSize= sizeof(struct sockaddr_in); 
	bzero(&rAddr, sizeof(struct sockaddr_in)); 

AGAIN:
	nNewfd	= accept( nSocketfd, (struct sockaddr *)(&rAddr), &nSize );
	
	if ( nNewfd < 0 )
	{
		if( errno==EINTR )	// 被信号中断,重新开始
			goto AGAIN;

		perror("accept error");
		return -2;
	}
	printf( "Comming ip address: %s\n", inet_ntoa(rAddr.sin_addr.s_addr) );

	return nNewfd;
}
/*****************************************************************************************  
Program ID: nSend	                                   
                                                    
Description:                 
	send data
Parameters:	

Return Value:                                                              
	return bytes of sending
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/4/25		haorui		Initial Release                  
****************************************************************************************/

int nSend(int nSocketfd, char* sText, int nBytes)
{
	if ( nSocketfd <0 || sText==NULL || nBytes<0 )
	{
		return FAILURE;
	}
	if ( send( nSocketfd, sText, nBytes, 0 ) != nBytes )
	{
		perror("send error");
		return FAILURE;
	}
	
	return nBytes;
}

/*****************************************************************************************  
Program ID: nSendn	                                   
                                                    
Description:                 
	Read "n" bytes from a descriptor. 
Parameters:	

Return Value:                                                              
	return bytes of sending
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/5/28		haorui		Initial Release                  
****************************************************************************************/
ssize_t	nReadn(int fd, void *vptr, size_t n)
{
	size_t	nleft;
	ssize_t	nread;
	char	*ptr=NULL;

	if ( vptr==NULL )
		return FAILURE;

	ptr = vptr;
	nleft = n;
	while (nleft > 0) 
	{
		if ( (nread = read(fd, ptr, nleft)) < 0) 
		{
			if (errno == EINTR)	
				nread = 0;		/* and call read() again */
			else
			{
				printf( "errno:%d, nread:%d\n", errno, nread );
				perror("read error");
				return FAILURE;
			}
		} 
		else if (nread == 0)	/* 连接被终断 */
		{
			//break;				/* EOF */
			return 0;
		}

		nleft -= nread;
		ptr   += nread;
	}
	//return(n - nleft);		/* return >= 0 */
	return n;
}

/*****************************************************************************************  
Program ID: nSendn	                                   
                                                    
Description:                 
	write "n" bytes from a descriptor. 
Parameters:	

Return Value:                                                              
	return bytes of sending
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/6/5		haorui		Initial Release                  
****************************************************************************************/
ssize_t	nWriten(int fd, const void *vptr, size_t n)
{
	size_t		nleft;
	ssize_t		nwritten;
	const char	*ptr = NULL;

	if (vptr==NULL || fd<0 )
		return FAILURE;

	ptr = vptr;
	nleft = n;
	while (nleft > 0) {
		//printf( "fd=%d, errno=%d, nwritten=%d\n", fd, errno, nwritten );
		if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
			if (errno == EINTR)
				nwritten = 0;		/* and call write() again */
			else
			{
				printf( "errno=%d, nwritten=%d\n", errno, nwritten );
				perror( "send error" );
				return FAILURE;		/* error */
			}
		}

		nleft -= nwritten;
		ptr   += nwritten;
	}
	return(n);
}

/*****************************************************************************************  
Program ID: nRecv	                                   
                                                    
Description:                 
	receive data
Parameters:	

Return Value:                                                              
	return bytes of receiving
Modification Log:                                         
	DATE			AUTHOR           DESCRIPTION          
 --------------------------------------------------------------------------------------
	2007/4/25		haorui		Initial Release                  
****************************************************************************************/

int nRecv(int nSocketfd, char* sText, int nBytes )
{
	int nSize;

	if ( nSocketfd <0 || sText==NULL || nBytes<0 )
	{
		return FAILURE;
	}
	nSize = recv( nSocketfd, sText, nBytes, 0 );
//	if ( nSize <= 0 )
//	{
//		return nSize;
//	}
	return nSize;  //size 有可能小于或等于零
}



/*==========================================================================================*/
/* inner function */

/* nType : SOCK_STREAM or SOCK_DGRAM  */
static int nSocketLocal( int nType )
{
	int nSockfd;

// 	if ( nType != SOCK_STREAM  )
// 	{
// 		return FAILURE;
// 	}
//#ifdef TCP_IP
	nSockfd = socket(AF_INET, nType, 0 );
	if ( nSockfd<=0 )
	{
		perror("socket error"); 
		return -1;
	}
//#else
//	nSockfd = socket(AF_LOCAL, nType, 0 );	
//#endif
	return nSockfd;
}


static int nBindLocal( int nSocketfd,  int port )
{
	struct sockaddr_in addr;
	int                nRet;
	int		   tr=1;
	
// 	if ( IpAddr==NULL  )
// 		return -1;
	
	bzero( &addr, sizeof(addr) );
	addr.sin_family		= AF_INET;
	//addr.sin_addr.s_addr	= inet_addr("10.6.194.253");
	//addr.sin_addr.s_addr	= inet_addr( IpAddr );
	//addr.sin_addr.S_un.S_addr = inet_addr("10.6.194.253");
	addr.sin_port		= htons( port ); 

	
	// kill "Address already in use" error message
	fcntl( nSocketfd, F_SETFD, 1 );	
	if (setsockopt(nSocketfd, SOL_SOCKET, SO_REUSEADDR, &tr,sizeof(int)) == -1) {
		perror("setsockopt");
		return -3;
	}

	/* bind nSockfd  */
	nRet = bind( nSocketfd, (struct sockaddr*)&addr, sizeof(addr) );
	if ( nRet < 0 )
	{
		perror("bind error");
		return -2; 
	}
	return 0;

// 	struct sockaddr_un rAddr;
// 	int nRet;
// 
// 	if ( nSocketfd <0 || sPath == NULL )
// 	{
// 		return FAILURE;
// 	}
// 
// 	bzero( &rAddr, sizeof(rAddr) );
// 	rAddr.sun_family	= AF_LOCAL;
// 	strncpy( rAddr.sun_path, sPath, sizeof( rAddr.sun_path )-1 );
// 	/* bind nSockfd  */
// 	nRet = bind( nSocketfd, (struct sockaddr*)&rAddr, SUN_LEN( &rAddr ) );
// 	if ( nRet < 0 )
// 	{
// 		perror("bind error");
// 		return FAILURE; 
// 	}
// 	return SUCCESS;
}

static int nListen(int nSocketfd, int nBacklog)
{
	int nRet;

	if ( nSocketfd <0 || nBacklog < 0 )
	{
		return -1;
	}

	if ( (nRet = listen(nSocketfd, nBacklog)) < 0)
	{
		perror("listen error");
		return -2;	
	}
	return 0;
}

static int nConnectLocal(int nSocketfd, char * IpAddr, int port )
{
// #ifdef TCP_IP
	struct sockaddr_in addr;

	if ( IpAddr==NULL  )
		return -1;
	
	bzero( &addr, sizeof(addr) );
	addr.sin_family		= AF_INET;
	//addr.sin_addr.s_addr	= inet_addr("10.6.194.253");
	addr.sin_addr.s_addr	= inet_addr( IpAddr );
	//addr.sin_addr.S_un.S_addr = inet_addr("10.6.194.253");
 	addr.sin_port		= htons( port ); 
	if ( connect( nSocketfd, (struct sockaddr *)(&addr), sizeof(addr) ) < 0 )
	{
		perror("connect error");
		return -2;
	}
	return 0;	
// #else
// 	struct sockaddr_un rAddr;
// 	int nRet;
// 
// 	printf( "nSocketfd:%d, spath: %s\n", nSocketfd, sPath );
// 	if ( nSocketfd <0 || sPath==NULL )
// 	{
// 		return FAILURE;
// 	}
// 
// 
// 	bzero( &rAddr, sizeof(rAddr) );
// 	rAddr.sun_family	= AF_LOCAL;
// 	strncpy( rAddr.sun_path, sPath, sizeof( rAddr.sun_path )-1 );
// 
// 	nRet = connect( nSocketfd, (struct sockaddr *)(&rAddr), sizeof(rAddr) );
// 	if ( nRet < 0 )
// 	{
// 		perror( "connect error: " );
// 		return FAILURE;
// 	}
// 	return SUCCESS;
// #endif
}









⌨️ 快捷键说明

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