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

📄 wigserver.c

📁 tornado2.0 for PPC VxWorks socket 源代码
💻 C
字号:

#ifdef	__cplusplus
extern "C"{
#endif
 
#include <vxWorks.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysLib.h>
#include "wigtest.h"
#include <sockLib.h>
#include <socklib.h>
#include <ioLib.h >
#include <socket.h>
#include <sysLib.h>
#include <logLib.h>
#include <errno.h>
#include <string.h>
#include "wdLib.h"
#include "in.h"		
#include "ioLib.h"
#include "inetlib.h"



int nSocketFd = 0 ;


int nClientAddrLength = 0;
int nSew = 0;
int nSblastNum = 0;
struct sockaddr_in  stServerAddr; /* server's address */
struct sockaddr_in  stClientAddr; /* client's address */

int numRead = 0;
int nReadSum = 0;

int socketservice
    (
    int nPort, /* the port number to read from */
    int nReadlenth,/* size of the meesage */
    int nBlen /* maximum size of socket-level receive szpBuffer */
    )
{
	char *szpBuffer = NULL; 
	szpBuffer = (char *) malloc (sizeof(char) * 10);
	if (szpBuffer == NULL)
	{
		perror ("cannot allocate szpBuffer of size ");
		exit (1);
	}

/* Set up our internet address, and bind it so the client can connect. */
	    stServerAddr.sin_family = AF_INET;
	    stServerAddr.sin_port = htons (nPort);

	/* Zero out the sock_addr structures.
	  * This MUST be done before the socket calls.
	  */
	bzero ((char *) &stServerAddr, sizeof (stServerAddr));
	bzero ((char *) &stClientAddr, sizeof (stClientAddr));
	 
	/* Open the socket. Use ARPA Internet address format and stream sockets. */
	    nSocketFd = socket (AF_INET, SOCK_STREAM, 0);

	if(nSocketFd == ERROR)
	{
		perror("server socket creat fail!\n");
		return -1;
	}
	else
	{
		printf("server socket creat succes!\n");
		return 0;
	}
	
	  

	  if (bind (nSocketFd, (struct sockaddr *)&stServerAddr, sizeof (stServerAddr)) < 0)
		{
	    perror ("server bind error");
			free (szpBuffer);
			exit (1);
		}
	
	    /* Listen, for the client to connect to us. */
	    if (listen (nSocketFd, 2) < 0)
		{
	    perror ("server listen failed");
			free (szpBuffer);
			exit (1);
		}

	nClientAddrLength = sizeof (stClientAddr);/*客户端地址长度*/
	nSew = accept (nSocketFd, (struct sockaddr *) &stClientAddr, &nClientAddrLength);
	if (nSew == ERROR)
	{
		perror ("server accept failed");
		close (nSocketFd);
		exit (1);
	 }
	 
	
	
	    /* maximum size of socket-level receive szpBuffer */
	    if (setsockopt (nSew, SOL_SOCKET, SO_RCVBUF,(char *) &nBlen, sizeof (nBlen)) < 0)
		{
			perror ("server setsockopt SO_SNDBUF failed");
			free (szpBuffer);
			exit (1);
		}
		
	
		
	 while (1)
	{	  
	  	while (1)
	  	{			
			if ((numRead = read (nSew, szpBuffer,1 )) < 0)
		    	{
		    		perror ("blastee read error");
		    		break;
		   	 }
		}
		nReadSum += numRead;
		
		if(nReadSum == nReadlenth)
			break;
	}
		
	close (nSocketFd);
	close (nSew);
	    
	free (szpBuffer);
	printf ("blastee end.\n");
	return 0xAA;/*succes*/
		
}

#ifdef	__cplusplus
}
#endif	/* end of __cplusplus */

⌨️ 快捷键说明

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