예제 #1
0
int main(int argc, char *argv[]) {

    if (argc != 3) // Test for correct number of arguments
        DieWithUserMessage("Parameter(s)", "<Address/Name> <Port/Service>");

    char *addrString = argv[1];   // Server address/name
    char *portString = argv[2];   // Server port/service

    // Tell the system what kind(s) of address info we want
    struct addrinfo addrCriteria;                   // Criteria for address match
    memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
    addrCriteria.ai_family = AF_UNSPEC;             // Any address family
    addrCriteria.ai_socktype = SOCK_STREAM;         // Only stream sockets
    addrCriteria.ai_protocol = IPPROTO_TCP;         // Only TCP protocol

    // Get address(es) associated with the specified name/service
    struct addrinfo *addrList; // Holder for list of addresses returned
    // Modify servAddr contents to reference linked list of addresses
    int rtnVal = getaddrinfo(addrString, portString, &addrCriteria, &addrList);
    if (rtnVal != 0)
        DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));

    // Display returned addresses
    for (struct addrinfo *addr = addrList; addr != NULL; addr = addr->ai_next) {
        PrintSocketAddress(addr->ai_addr, stdout);
        fputc('\n', stdout);
    }

    freeaddrinfo(addrList); // Free addrinfo allocated in getaddrinfo()

    exit(0);
}
예제 #2
0
int SetupTCPServerSocket(const char *service) {
  // Construct the server address structure
  struct addrinfo addrCriteria;                   // Criteria for address match
  memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
  addrCriteria.ai_family = AF_UNSPEC;             // Any address family
  addrCriteria.ai_flags = AI_PASSIVE;             // Accept on any address/port
  addrCriteria.ai_socktype = SOCK_STREAM;         // Only stream sockets
  addrCriteria.ai_protocol = IPPROTO_TCP;         // Only TCP protocol



  struct addrinfo *servAddr; // List of server addresses
  int rtnVal = getaddrinfo(NULL, service, &addrCriteria, &servAddr);
  if (rtnVal != 0)
    DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));



  int servSock = -1;
  for (struct addrinfo *addr = servAddr; addr != NULL; addr = addr->ai_next) {
    // Create a TCP socket
    servSock = socket(addr->ai_family, addr->ai_socktype,
        addr->ai_protocol);
    if (servSock < 0)
      continue;       // Socket creation failed; try next address



    // Bind to the local address and set socket to listen
    if ((bind(servSock, addr->ai_addr, addr->ai_addrlen) == 0) &&
        (listen(servSock, MAXPENDING) == 0))
    {
      // Print local address of socket
      struct sockaddr_storage localAddr;
      socklen_t addrSize = sizeof(localAddr);


      if (getsockname(servSock, (struct sockaddr *) &localAddr, &addrSize) < 0)
        DieWithSystemMessage("getsockname() failed");


      fputs("Binding to ", stdout);
      PrintSocketAddress((struct sockaddr *) &localAddr, stdout);
      fputc('\n', stdout);

      break;       // Bind and listen successful
    }

    close(servSock);  // Close and try again
    servSock = -1;
  }

  // Free address list allocated by getaddrinfo()
  freeaddrinfo(servAddr);

  return servSock;
}
int SetupTCPServerSocket(const char *service)
{
	/* Construct the server address structure */
	struct addrinfo addrCriteria;
	memset(&addrCriteria, 0, sizeof(addrCriteria));	/* Zero out all the unused fields */
	addrCriteria.ai_family = AF_UNSPEC;				/* Accept both v4 and v6 */
	addrCriteria.ai_socktype = SOCK_STREAM;			/* Only Stream type */
	addrCriteria.ai_flags = AI_PASSIVE;				/* Accept on any address */
	addrCriteria.ai_protocol = IPPROTO_TCP;			/* Accept only TCP protocol */
	
	struct addrinfo *servAddr;						/* List of server addresses */
	int rtnVal = getaddrinfo(NULL, service, &addrCriteria, &servAddr);
	if(rtnVal != SUCCESS)
	{
		DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));
	}	
	int servSock = -1;								/* Initialize descriptor for server socket */
	struct addrinfo *addr;
	for(addr = servAddr; addr != NULL; addr = addr->ai_next)
	{
		servSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
		if(servSock < 0)
		{
			/* Socket creation failed, retry with next entry */
			continue;
		}
		/* Bind to local address and set socket to listen */
		if((bind(servSock, addr->ai_addr, addr->ai_addrlen) == SUCCESS) &&
				(listen(servSock, MAXPENDING) == SUCCESS))
		{
			/* Print the local address of the socket */
			struct sockaddr_storage localAddr;
			socklen_t addrSize = sizeof(localAddr);
			if(getsockname(servSock, (struct sockaddr *)&localAddr, &addrSize) < 0)
			{
				DieWithSystemMessage("getsockname() failed");
			}
			fputs("Binding to ", stdout);
			PrintSocketAddress((struct sockaddr *)&localAddr, stdout);
			fputc(NEW_LINE, stdout);
			break;
		}
		close(servSock);
		servSock = -1;
	}

	/* Free address list allocated by getaddrinfo() */
	freeaddrinfo(servAddr);

	return servSock;
}
int AcceptTCPConnection(int servSock) {
  struct sockaddr_storage clntAddr; // Client address
  // Set length of client address structure (in-out parameter)
  socklen_t clntAddrLen = sizeof(clntAddr);

  // Wait for a client to connect
  int clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntAddrLen);
  if (clntSock < 0)
    DieWithSystemMessage("accept() failed");

  // clntSock is connected to a client!

  fputs("Handling client ", stdout);
  PrintSocketAddress((struct sockaddr *) &clntAddr, stdout);
  fputc('\n', stdout);

  return clntSock;
}
int AcceptTCPConnection(int servSock)
{
	struct sockaddr_storage clntAddr;			/* Cliend address structure */
	socklen_t clntAddrLen = sizeof(clntAddr);	/* Store size of structure */
	
	/* Wait for client to connect */
	int clntSock = accept(servSock, (struct sockaddr *)&clntAddr, &clntAddrLen);
	if(clntSock < 0)
	{
		DieWithSystemMessage("accept() failed");
	}
	/* Client is connected */
	fputs("Handling client ", stdout);
	PrintSocketAddress((struct sockaddr *)&clntAddr, stdout);
	fputc(NEW_LINE, stdout);

	return clntSock;
}
예제 #6
0
int SetupTCPServerSocket(const char *service){
    struct addrinfo addrSetting;    //接受标准
    memset(&addrSetting, 0, sizeof(addrSetting));   //全置0
    addrSetting.ai_family = AF_UNSPEC;              //v4 或者 v6地址
    addrSetting.ai_flags = AI_PASSIVE;              //接受所有地址和端口
    addrSetting.ai_socktype = SOCK_STREAM;          //只接受流式
    addrSetting.ai_protocol = IPPROTO_TCP;          //只接受tcp

    struct addrinfo *servAddr;      //服务地址
    /*使用getaddrinfo获得套接字设置,接受第一个可以使用的
      int getaddrinfo(const char *node, const char *service,
                      const struct addrinfo *hints,
                      struct addrinfo **res)
    */

    int rtnVal = getaddrinfo(NULL, service, &addrSetting, &servAddr);
    if(rtnVal != 0)
        UserMessageHandler("getaddrinfo() failed",gai_strerror(rtnVal));

    int servSock = -1;
    struct addrinfo *addr;
    for(addr = servAddr; addr != NULL; addr = addr->ai_next){
        servSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if(servSock < 0)
            continue;

        if((bind(servSock, addr->ai_addr, addr->ai_addrlen) == 0) && (listen(servSock, MAXPENDING) == 0)){
            //打印地址
            struct sockaddr_storage localAddr;
            socklen_t addrSize = sizeof(localAddr);
            if(getsockname(servSock, (struct sockaddr *) &localAddr, &addrSize) < 0)
                SystemMessageHandler("getsockname() failed");
            fputs("current addr binding to sock: ",stdout);
            PrintSocketAddress((struct sockaddr *)&localAddr, stdout);
            fputc('\n',stdout);
            break;
        }
        close(servSock);
        servSock = -1;
    }
    freeaddrinfo(servAddr);
    return servSock;
}
예제 #7
0
 int main(int argc, char *argv[])
 {
 	if(argc != 3)
 	{
 		printf("Usage : GetAddrInfo <Address/Name> <Port/Service>\n");
 		return -1;
 	}

 	char *addrString = argv[1];
 	char *portString = argv[2];

 	/* Set criteria of address search */
 	struct addrinfo addCriteria;
 	memset(&addCriteria, 0, sizeof(addCriteria));
 	addCriteria.ai_family = AF_INET;
 	addCriteria.ai_socktype = SOCK_STREAM;
 	addCriteria.ai_protocol = IPPROTO_TCP;

 	/* Get Address associated with specified name and port */
 	struct addrinfo *addrList;
 	struct addrinfo *addr;
 	int rtnVal = getaddrinfo(addrString, portString, &addCriteria, &addrList);
 	if(rtnVal != 0)
 	{
 		printf("getaddrinfo() : failed !!!\n");
 		return -1;
 	}


 	for(addr = addrList; addr != NULL; addr = addr->ai_next)
 	{
 		PrintSocketAddress(addr->ai_addr);
 		printf("\n");
 	}

 	/* Free the addrinfo Linked list */
 	freeaddrinfo(addrList);

 	return 0;
 }
예제 #8
0
void HandleTCPClient(int clntSocket, char *trigAcptPort, int PrintStatus)
{

  unsigned char sndBuffer[SNDBUFSIZE];/* Buffer for data string to be sent */
  memset(sndBuffer,73,sizeof(sndBuffer));
  /***************************************/
  //  Signal receiver port(SERVER) 
  /***************************************/
  // Definition
  if(PrintStatus==1)
    printf("FakeCluster1 : Trigger receive port is set to # %s\n",trigAcptPort);
  //tell the system what kinds of address we want
  //**************
  // ai_family  
  //  AF_UNSPEC :Any address family
  //  AF_INET   :IPv4 address family
  //  
  // ai_flags
  //  AI_PASSIVE    :Accept on any address/port */ 
  //  AI_NUMERICHOST:Accept only on numeric address/port
  //  
  //  
  //**************
  struct addrinfo addrCriteria;                     // Criteria for address
  memset(&addrCriteria, 0, sizeof(addrCriteria));   // Zero out structure
  addrCriteria.ai_family   = AF_INET;               // IPv4 address family
  addrCriteria.ai_flags    = AI_PASSIVE;            // Accept on any address/port */
  addrCriteria.ai_socktype = SOCK_DGRAM;            // Only datagram socket
  addrCriteria.ai_protocol = IPPROTO_UDP;           // Only UDP socket
  /***GETADDRINFO***/
  struct addrinfo *servAddr;                        // List of server addresses
  int rtnVal = getaddrinfo(NULL, trigAcptPort, &addrCriteria, &servAddr);
  if (rtnVal != 0)
    DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));
  /***SOCKET***/
  int trigSock = socket(servAddr->ai_family, servAddr->ai_socktype,
      servAddr->ai_protocol);
  if (trigSock < 0)
    DieWithSystemMessage("socket() failed");

  int yes =1;
  if(setsockopt(trigSock, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes,
		sizeof(yes))<0)
    DieWithSystemMessage("setsockopt() failed");
  /***BIND***/
  if (bind(trigSock, servAddr->ai_addr, servAddr->ai_addrlen) < 0)
    DieWithSystemMessage("bind() failed");
  
  // Free address list allocated by getaddrinfo()
  freeaddrinfo(servAddr);


  /***************************************/
  //  Handling process
  /***************************************/
  for(;;)
    {
      /***preparation for trigger acception***/
      // Client address
      struct sockaddr_storage clntAddr; 
      // Set Length of client address structure (in-out parameter)
      socklen_t clntAddrLen = sizeof(clntAddr);
      // Size of trigger signal message
      char buffer[MAXSTRINGLENGTH]; // I/O buffer

      /***trigger acception***/
      // (Block until receive message from a client)
      ssize_t numBytesRcvd = recvfrom(trigSock, buffer, MAXSTRINGLENGTH, 0,
				      (struct sockaddr *) &clntAddr, &clntAddrLen);
      if (numBytesRcvd < 0)
	DieWithSystemMessage("recvfrom() failed");
      
      /***trigger acception message***/
      if(PrintStatus==1)
	{
	  fputs("Trigger signal is accepted from ", stdout);
	  PrintSocketAddress((struct sockaddr *) &clntAddr, stdout);
	  fputc('\n', stdout);
	}
      /***sending data***/   
      if(send(clntSocket, sndBuffer, sizeof(sndBuffer), 0)<0)
	break;//DieWithError("Send error \n");
    }
  /* Close client socket */
  close(trigSock);      
  close(clntSocket);    
}