当前位置: 首页>>代码示例>>C++>>正文


C++ DieWithError函数代码示例

本文整理汇总了C++中DieWithError函数的典型用法代码示例。如果您正苦于以下问题:C++ DieWithError函数的具体用法?C++ DieWithError怎么用?C++ DieWithError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了DieWithError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int
main (int argc,
		char *argv[])
{
	int sd     = -1;
	int sndbuf = 0;
	int rcvbuf = 0;
	socklen_t optlen;

	if ((sd = socket (PF_INET, SOCK_STREAM, 0)) < 0)
		DieWithError("socket");

	optlen = sizeof(sndbuf);
	if (getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0)
		DieWithError("getsockopt");
	optlen = sizeof (rcvbuf);
	if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen) < 0)
		DieWithError("getsockopt");

	printf("Socket Descriptor : %d\n", sd);
	printf(" Send buf: %d bytes\n", sndbuf);
	printf(" Recv buf: %d bytes\n", rcvbuf);
	close(sd);

	return 0;
}
开发者ID:rumidier,项目名称:Bit,代码行数:26,代码来源:so_rcvbuf.c

示例2: main

int main(int argc, char *argv[]) {
  int sock;
  struct sockaddr_in echoServAddr;
  unsigned short echoServPort;
  char *servIP;
  char *echoString;
  char echoBuffer[RCVBUFSIZE];
  unsigned int echoStringLen;
  int bytesRcvd, totalBytesRcvd;

  if ((argc < 3) || (argc > 4)) {
    fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]);
    exit(1);
  }

  servIP = argv[1];
  echoString = argv[2];

  if (argc == 4) {
    echoServPort = atoi(argv[3]);
  } else {
    echoServPort = 7;
  }

  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    DieWithError("socket() failed");
  }

  memset(&echoServAddr, 0, sizeof(echoServAddr));
  echoServAddr.sin_family = AF_INET;
  echoServAddr.sin_addr.s_addr = inet_addr(servIP);
  echoServAddr.sin_port = htons(echoServPort);

  if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) {
    DieWithError("connect() failed");
  }

  echoStringLen = strlen(echoString);

  if (send(sock, echoString, echoStringLen, 0) != echoStringLen) {
    DieWithError("send() sent a different number of bytes than expected");
  }

  totalBytesRcvd = 0;
  printf("Received: ");
  while (totalBytesRcvd < echoStringLen) {

    if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0) {
      DieWithError("recv() failed or connection closed prematurely");
    }

    totalBytesRcvd += bytesRcvd;

    echoBuffer[bytesRcvd] = '\0';
    printf("%s\n", echoBuffer);
  }

  close(sock);
  exit(0);
}
开发者ID:alessandroste,项目名称:testBSP,代码行数:60,代码来源:TCPEchoClient.c

示例3: CreateTCPServerSocket

int CreateTCPServerSocket (unsigned short port)
{
    int                 sock;         /* socket to create */
    struct sockaddr_in  echoServAddr; /* Local address */

    /* Create socket for incoming connections */
    if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        DieWithError ("socket() failed");
    }
    info ("socket");
      
    /* Construct local address structure */
    memset( &echoServAddr, 0, sizeof(echoServAddr));  /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                /* Internet address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    echoServAddr.sin_port = htons(port);              /* Local port */

    /* Bind to the local address */
    if (bind (sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
    {
        DieWithError ("bind() failed");
    }
    info_d ("bind", port);

    /* Mark the socket so it will listen for incoming connections */
    if (listen (sock, MAXPENDING) < 0)
    {
        DieWithError ("listen() failed");
    }
    info ("listen");

    return (sock);
}
开发者ID:TomHartogs,项目名称:School,代码行数:34,代码来源:CreateTCPServerSocket.c

示例4: SIGIOHandler

void SIGIOHandler(int signalType)
{
    struct sockaddr_in echoClntAddr;  /* Address of datagram source */
    unsigned int clntLen;             /* Address length */
    int recvMsgSize;                  /* Size of datagram */
    char echoBuffer[ECHOMAX];         /* Datagram buffer */

    do  /* As long as there is input... */
    {
        /* Set the size of the in-out parameter */
        clntLen = sizeof(echoClntAddr);

        if ((recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0,
               (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
        {
            /* Only acceptable error: recvfrom() would have blocked */
            if (errno != EWOULDBLOCK)  
                DieWithError("recvfrom() failed");
        }
        else
        {
            printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));

            if (sendto(sock, echoBuffer, recvMsgSize, 0, (struct sockaddr *) 
                  &echoClntAddr, sizeof(echoClntAddr)) != recvMsgSize)
                DieWithError("sendto() failed");
        }
    }  while (recvMsgSize >= 0);
    /* Nothing left to receive */
}
开发者ID:dbase,项目名称:Sockets,代码行数:30,代码来源:UDPEchoServer-SIGIO.c

示例5: SIGIOHandler

void SIGIOHandler (int signalType)
{
  struct sockaddr_in echoClntAddr;
  unsigned int clntLen;
  int recvMsgSize;
  char echoBuffer[ECHOMAX];

  do
    {
      clntLen = sizeof (echoClntAddr);
      if ((recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *)&echoClntAddr, &clntLen)) < 0)
        {
          if (errno != EWOULDBLOCK)
            DieWithError("recvfrom() failed");
        }
      else 
        {
          printf("Handler client %s\n", inet_ntoa(echoClntAddr.sin_addr));
          echoBuffer[recvMsgSize] = '\0';
          printf("buf = %s\n", echoBuffer);

          if (sendto(sock, echoBuffer, recvMsgSize, 0, (struct sockaddr *)&echoClntAddr, sizeof(echoClntAddr)) != recvMsgSize)
            DieWithError("sendto() failed");
        }
    }
  while (recvMsgSize >= 0);
}
开发者ID:rumidier,项目名称:Bit,代码行数:27,代码来源:udp_sigio_s_explain.c

示例6: CreateTCPServerSocket

int CreateTCPServerSocket(unsigned short port)
{
	int sock;
	struct sockaddr_in servAddr;

	// Create socket for incomming connection
	if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
		DieWithError("socket() failed!");
	
	// Construct local addr structure
	memset(&servAddr, 0, sizeof(servAddr));			// Zero out structure
	servAddr.sin_family = AF_INET;						// Internet addr family
	servAddr.sin_addr.s_addr = htonl(INADDR_ANY);	// Any incomming interface
	servAddr.sin_port = htons(port);						//	Local port

	// Bind to local addr
	if(bind(sock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
		DieWithError("bind() failed!");

	// Mark the socket to listen for incomming connections
	if(listen(sock, MAXPENDING) < 0)
		DieWithError("listen() failed!");

	return sock;
}
开发者ID:SergeyBondarenko,项目名称:easy-share,代码行数:25,代码来源:CreateTCPServerSocket.c

示例7: main

int main(int argc, char *argv[]) {
	struct sockaddr_in echoServAddr;     // Local address
	unsigned short echoServPort;     // Server port

	WSADATA wsaData;     // Structure for WinSock setup communication
	pthread_t pth;     //Structure for threads

	echoServPort = 21;     //Local port

	if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)     // Load Winsock DLL
			{
		fprintf(stderr, "WSAStartup() failed");
		exit(1);
	}

	if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)     // Create socket for incoming connections
		DieWithError("socket() failed");

	// Construct local address structure
	memset(&echoServAddr, 0, sizeof(echoServAddr));     // Zero out structure
	echoServAddr.sin_family = AF_INET;     // Internet address family
	echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);     // Any incoming interface
	echoServAddr.sin_port = htons(echoServPort);     // Local port

	if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))     // Bind to the local address
	< 0) DieWithError("bind() failed");

	for (;;) {
		if (listen(servSock, MAXPENDING) < 0) DieWithError("listen() failed");     // Mark the socket so it will listen for incoming connections
		pthread_create(&pth, NULL, threadFunc, NULL);     //if connection is found create a thread
		usleep(1);     //lets see what our theads are doing
	}

	return EXIT_SUCCESS;
}
开发者ID:athy91,项目名称:game,代码行数:35,代码来源:testserver.c

示例8: main

int main(int argc, char *argv[])
{
    int servSock;                    /* Socket descriptor for server */
    int clntSock;                    /* Socket descriptor for client */
    unsigned short echoServPort;     /* Server port */
    pthread_t threadID;              /* Thread ID from pthread_create() */
    struct ThreadArgs *threadArgs;   /* Pointer to argument structure for thread */

    if (argc != 2)     /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage:  %s <SERVER PORT>\n", argv[0]);
        exit(1);
    }

    echoServPort = atoi(argv[1]);  /* First arg:  local port */

    servSock = CreateTCPServerSocket(echoServPort);

    for (;;) /* run forever */
    {
	clntSock = AcceptTCPConnection(servSock);

        /* Create separate memory for client argument */
        if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) 
               == NULL)
            DieWithError("malloc() failed");
        threadArgs -> clntSock = clntSock;

        /* Create client thread */
        if (pthread_create(&threadID, NULL, ThreadMain, (void *) threadArgs) != 0)
            DieWithError("pthread_create() failed");
        printf("with thread %ld\n", (long int) threadID);
    }
    /* NOT REACHED */
}
开发者ID:dbase,项目名称:Sockets,代码行数:35,代码来源:TCPEchoServer-Thread.c

示例9: ClientEchoHandle

void ClientEchoHandle(int clntSock, char *echoString){
    
    int echoStringLen;         // length of string to echo
    int bytesRcvd, totalBytesRcvd;      // bytes read in single recv() and total bytes read
    
    //fgets(echoString, RCVBUFSIZE, stdin);   // standard input to send msg
    echoStringLen = strlen(echoString);     // determine input length

    // Send the string to the server
    if(send(clntSock, echoString, echoStringLen, 0) != echoStringLen){
        DieWithError("send() sent a different number of bytes than expected");
    }
    
    // Receive the same string back from the server
    totalBytesRcvd = 0;
    while(totalBytesRcvd < echoStringLen){
        /* Receive up to the buffer size
         * (-1 to leave space for a null terminator)
         * bytes from the sender */
         if((bytesRcvd = recv(clntSock, echoBuffer, RCVBUFSIZE-1, 0))<=0){
             DieWithError("recv() failed or connection closed prematurely");
         }
         totalBytesRcvd += bytesRcvd;   // keep tally of total bytes
         echoBuffer[bytesRcvd]='\0';    // terminate the string!
         printf("%s\n",echoBuffer);
    }
}
开发者ID:lastone9182,项目名称:NetworkProgramming,代码行数:27,代码来源:ClientEchoHandle.c

示例10: info_user

static void
info_user (const char * prefix, const char * s)
{
    struct tm   ti;
    time_t      t;
    
    if (tty_fptr == NULL)
    {
        tty_fptr = stdout;
    }
    
    if (argv_userprefix == true)
    {
        
        if (time (&t) == -1)
        {
            DieWithError ("time()");
        }
        if (localtime_r (&t, &ti) == NULL)
        {
            DieWithError ("localtime()");
        }
        
        fprintf (tty_fptr, "%02d:%02d:%02d %s:> ",
            ti.tm_hour, 
            ti.tm_min, 
            ti.tm_sec, 
            prefix);
    }
    fprintf (tty_fptr, "%s", s);
}
开发者ID:TomHartogs,项目名称:School,代码行数:31,代码来源:Auxiliary.c

示例11: read_file_process

/*
 * Metodo que lee un archivo
 * @param buffer Contiene la información leída del archivo
 * @param file Nombre del archivo
 */
void read_file_process(char* buffer, char* file)
{
  char* s;

  if((s = strrchr(file, '.')) == NULL) DieWithError("ERROR FATAL: El archivo no es del tipo texto");
  else if(strcmp(s, ".txt") != 0) DieWithError("ERROR FATAL: El archivo no es del tipo texto"); 

  FILE *fp = fopen(file, "r");
  char c;
  int i;
      
  if(fp == NULL) DieWithError("ERROR FATAL: Ocurrio un error abriendo el archivo");

  i = 0;
  while((c = getc(fp)) != EOF)
  {
    if (i > MES_SIZE-1) DieWithError("ERROR 500: El mensaje que se intenta cifrar es muy grande");
    buffer[i++] = c;
  }
  
  if(i == 0) DieWithError("ERROR 501: El mensaje que se intenta cifrar está vacío");
  else buffer[i] = '\0';

  printf(buffer);

  fclose(fp);
}
开发者ID:joseluisjb1990,项目名称:ci4831-miniproyecto1,代码行数:32,代码来源:scdax_cli.c

示例12: openSocket

int openSocket() {
  int sock;                        /* Socket descriptor */
  WSADATA wsaData;                 /* Structure for WinSock setup communication */
  struct sockaddr_in echoServAddr; /* Echo server address */

  if(WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) /* Load Winsock 2.0 DLL */
  {
    fprintf(stderr, "WSAStartup() failed");
    exit(1);
  }

  /* Construct the server address structure */
  memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
  echoServAddr.sin_family      = AF_INET;             /* Internet address family */
  echoServAddr.sin_addr.s_addr = inet_addr("127.0.0.1");   /* Server IP address */
  echoServAddr.sin_port        = htons(MOTECOM_PORT_ADDR); /* Server port */

  /* Create a reliable, stream socket using TCP */
  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    DieWithError("socket() failed");

  /* Establish the connection to the echo server */
  if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("connect() failed");

  printf("socket %d\n", sock);
  return sock;
}
开发者ID:nesl,项目名称:ragobots,代码行数:28,代码来源:MoteCom.c

示例13: main

int main(int argc, char *argv[]) {
    // connect to supplied address and send the greeting message to server
    int sock = Bootstrap(argv[1]);

    // process requests infinitely (we will be killed when done)
    int* nameLength;
    char status[3];

    while(1) {
        int nameLength;
        if (scanf("%d", &nameLength) != 1)
            DieWithError("reading a filename length failed");

        __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Length is %d", nameLength);

        char* filename;
        if ((filename = (char*) calloc(nameLength + 1, 1)) == NULL)
            DieWithError("calloc() failed");

        __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Allocated %d - sized buffer", nameLength + 1);

        if (fgets(filename, nameLength + 1, stdin) == NULL)
            DieWithError("reading filename failed");

        __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Attempting to open %s", filename);

        int mode;
        if (scanf("%d", &mode) != 1)
            DieWithError("reading a mode failed");

        // freaking MIPS...
        if (mode&0x400) {
            mode ^= 0x400;
            mode |= O_APPEND;
        }

        if (mode&0x40) {
            mode ^= 0x40;
            mode |= O_CREAT;
        }

        __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Mode is %d", mode);

        int targetFd = open(filename, mode, S_IRWXU|S_IRWXG);

        if (targetFd > 0) {
            if (ancil_send_fds_with_buffer(sock, targetFd))
                DieWithError("sending file descriptor failed");
        } else {
            fprintf(stderr, "Error: failed to open a file - %s\n", strerror(errno));
        }

        free(filename);
        close(targetFd);
    }

    return -1;
}
开发者ID:chdir,项目名称:fdshare,代码行数:58,代码来源:fdhelper.c

示例14: stringBufferLength

int stringBufferLength(stringBuffer* L){
	if (L==NULL)
		DieWithError("Function stringBufferLength: Non-NULL parameter expected.\n");
	else
		return L->length;

	DieWithError("Function stringBufferLength: This line should never be executed.\n");
	return -10;
}
开发者ID:Bakkhos,项目名称:compilers,代码行数:9,代码来源:stringbuffer.c

示例15: main

int main(int argc, char *argv[])
{
    int servSock;                    /* Socket descriptor for server */
    int clntSock;                    /* Socket descriptor for client */
    struct sockaddr_in echoServAddr; /* Local address */
    struct sockaddr_in echoClntAddr; /* Client address */
    unsigned short echoServPort;     /* Server port */
    unsigned char  modbusAddr;       /* 8-bit modbus addr */
    unsigned int clntLen;            /* Length of client address data structure */

    if (argc != 3)     /* Test for correct number of arguments */
    {
        fprintf(stderr, "Usage:  %s <Server Port> <Modbus Addr>\n", argv[0]);
        exit(1);
    }

    echoServPort = atoi(argv[1]);  /* First arg:  local port */
    modbusAddr   = (unsigned char) atoi(argv[2]);  /* Second arg: modbus addr */

    /* Create socket for incoming connections */
    if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");
      
    /* Construct local address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                /* Internet address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    echoServAddr.sin_port = htons(echoServPort);      /* Local port */

    /* Bind to the local address */
    if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError("bind() failed");

    /* Mark the socket so it will listen for incoming connections */
    if (listen(servSock, MAXPENDING) < 0)
        DieWithError("listen() failed");

    for (;;) /* Run forever */
    {
        /* Set the size of the in-out parameter */
        clntLen = sizeof(echoClntAddr);

        /* Wait for a client to connect */
        if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, 
                               &clntLen)) < 0)
            DieWithError("accept() failed");

        /* clntSock is connected to a client! */

        printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));

        HandleTCPClient(clntSock, modbusAddr);
    }
    /* NOT REACHED */
}
开发者ID:SoftwareDefinedBuildings,项目名称:BSE,代码行数:55,代码来源:TCPModbusServer.c


注:本文中的DieWithError函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。