Пример #1
0
int do_processing(SOCKET* sockfd)
{
    int result = 0;

    if (*sockfd != INVALID_SOCKET)
    {
        int read;
        char buffer[256] = {0};
		
		struct timeval tv = {0};
		tv.tv_usec = 100;
		
		FD_SET fdset = {0};
		FD_SET (*sockfd, &fdset);
		
		result = select (0, &fdset, NULL, NULL, &tv);
		
		if (result > 0)
		{	
			if ((read = recv_socket(sockfd, buffer, 255)) >= 0)
			{
				send_socket(sockfd, buffer, read);
				result = 1;
			}
		}
    }

    return result;
}
Пример #2
0
int do_processing(SOCKET *sockfd)
{
    int result = 0;

    if (*sockfd != INVALID_SOCKET)
    {
        int read;
        char buffer[256] = {0};
		struct fd_set testeset={0};
        int clilen,selection;
        struct sockaddr_in cli_addr;
		struct timeval tv = {0};
		tv.tv_usec = 100;
		
        listen(*sockfd, MAX_SOCKETS);
        clilen = sizeof(cli_addr);
		
		FD_SET(*sockfd,&testeset);
		selection = select(0,&testeset,NULL,NULL,&tv);
		if(selection>0)
		{
        if ((read = recv_socket(sockfd, buffer, 255)) > 0)
        {
            send_socket(sockfd, buffer, read);
            result = 1;
        }
		else	
			return -1;
		}
    }

    return result;
}
Пример #3
0
int main(int argc, char *argv[])
{
    SOCKET sock = create_socket();

    if (sock != INVALID_SOCKET)
    {
        if (connect_socket(&sock, IP_ADDR, PORT_NO, 10))
        {
            char buffer[256] = {0};
            int n;

            keyboard_read("Please enter the message: ", buffer, 255);

            n = send_socket(&sock, buffer, strlen(buffer));

            if (n >= 0)
            {
                memset((void*) buffer, 0, 256);
                n = recv_socket(&sock, buffer, 256);

                if (n >= 0)
                {
                    printf("%s\n", buffer);
                }
                else
                {
                    puts("ERROR reading from socket");
                }
            }
            else
            {
                puts("ERROR writing to socket");
            }
        }
        else
        {
            puts("ERROR connecting");
        }

        disconnect_socket(&sock);
    }
    else
    {
        puts("ERROR opening socket");
    }

    destroy_socket(&sock);

    return 0;
}
Пример #4
0
/**
 * write a buffer to socket with timeout
 * and receive an ack 0 || 1
 */
static int write_line(int sock, int top, const char* type)
{

   int bytes_read, bytes_sent;
   int fileid;
   char bf[512];


   memset(bf, '\0', sizeof(bf));
   
   if (top == 0) {
     if ( ((bytes_sent=send_socket(sock , nodelogger_buf , sizeof(nodelogger_buf) , SOCK_TIMEOUT_CLIENT)) <= 0)) {
        fprintf(stderr,"%%%%%%%%%%%% NODELOGGER: socket closed at send  %%%%%%%%%%%%%%\n");
        return(-1);
     }
   } else {
     /* create tolog file */
     if ((fileid = open(TOP_LOG_PATH,O_WRONLY|O_CREAT,0755)) < 1 ) {
                        fprintf(stderr,"Nodelogger: could not create toplog:%s\n",TOP_LOG_PATH);
			/* return something */
     }

     if ( ((bytes_sent=send_socket(sock , nodelogger_buf_top , sizeof(nodelogger_buf_top) , SOCK_TIMEOUT_CLIENT)) <= 0)) {
       fprintf(stderr,"%%%%%%%%%%%% NODELOGGER: socket closed at send  %%%%%%%%%%%%%%\n");
       return(-1);
     }
   }

   if ( (bytes_read=recv_socket (sock , bf , sizeof(bf) , SOCK_TIMEOUT_CLIENT)) <= 0 ) {
     fprintf(stderr,"%%%%%%%%%%%% NODELOGGER: socket closed at recv   %%%%%%%%%%%%%%\n");
     return(-1);
   }
   if ( bf[0] != '0' ) {
     bf[bytes_read > 0 ? bytes_read : 0] = '\0';  
     fprintf(stderr,"Nodelogger::write_line: Error=%s bf=%s nodelogger_buf=%s\n",strerror(errno),bf,nodelogger_buf);
     return(-1);
   }
   /* Notify user if he is using nfs mode, this will be done at begin and end of root node 
      Note : we notify user when using inter-dependencies
   if ( top != 0 && (strcmp(type,"begin") == 0 || strcmp(type,"endx") == 0 ) ) NotifyUser ( sock , top ,'S', _seq_exp_home  );  */
   

   return(0);
}
Пример #5
0
/** 
* Notify User if he is using nfs mode, This notification will happen twice a run :
* in the beginning and end of root Node
*/
static void NotifyUser (int sock , int top , char mode, const char * _seq_exp_home)
{
   char bf[512];
   char *rcfile, *lmech ;
   int bytes_sent, bytes_read, fileid, num;

   if ( top != 0 ) {
         if ( (rcfile=malloc( strlen (getenv("HOME")) + strlen("/.maestrorc") + 2 )) != NULL ) {
	     sprintf(rcfile, "%s/.maestrorc", getenv("HOME"));
        if ( (lmech=SeqUtil_getdef( rcfile, "SEQ_LOGGING_MECH", _seq_exp_home )) != NULL ) {
		  switch ( mode ) {
		        case 'S': /* mserver is up but logging mechanism is through NFS */
			         strcat(nodelogger_buf_notify,"Please initialize SEQ_LOGGING_MECH=server in ~/.maestrorc file\n");
	                         if ( strcmp(lmech,"nfs") == 0 ) {
                                     if ( ((bytes_sent=send_socket(sock , nodelogger_buf_notify , sizeof(nodelogger_buf_notify) , SOCK_TIMEOUT_CLIENT)) <= 0)) {
	                                     free(rcfile);free(lmech);
                                             return;
                                     }
                                     if ( (bytes_read=recv_socket (sock , bf , sizeof(bf) , SOCK_TIMEOUT_CLIENT)) <= 0 ) {
	                                     free(rcfile);free(lmech);
                                             return;
                                     }
		                  }
				  break;
		        case 'N': /* mserver is down and logging mech. is through NFS */
			         strcat(nodelogger_buf_notify_short,"Please start mserver and initialize SEQ_LOGGING_MECH=server in ~/.maestrorc file\n");
	                         if ( strcmp(lmech,"nfs") == 0 ) {
                                       if ( (fileid = open(LOG_PATH,O_WRONLY|O_CREAT,0755)) > 0 ) {
 	                                        off_t s_seek=lseek(fileid, 0, SEEK_END);
		                                num = write(fileid, nodelogger_buf_notify_short, strlen(nodelogger_buf_notify_short));
		                                fsync(fileid);
		                                close(fileid);
                                       } 
				 } 
				 break;
                  }
		  free(lmech);
	     }
	     free(rcfile);
	 }
   }

}
Пример #6
0
int do_processing(int sockfd)
{
    int result = 0;

    if (sockfd != INVALID_SOCKET)
    {
        int read;
        char buffer[256] = {0};

        if ((read = recv_socket(sockfd, buffer, 255)) > 0)
        {
            send_socket(sockfd, buffer, read);
            result = 1;
        }

        if (read == 0)
            errno = 0;
    }

    return result;
}
Пример #7
0
void redrobd_rc_net_server_thread::recv_client(void *data,
					       unsigned nbytes)
{
  long rc;
  unsigned actual_bytes;

  // Receive data from client
  rc = recv_socket(m_client_sd,
		   data,
		   nbytes,
		   true,  // Receive all
		   false, // No peek
		   &actual_bytes);
  
  if (rc != SOCKET_SUPPORT_SUCCESS) {
    throw client_com_error;
  }

  // Check if client closed connection
  if (!actual_bytes) {
    throw client_com_error;
  }
}