Example #1
0
int
recv_handler(struct request *req)
{
    int cc;
    int base = req->rank * req->len;

    set_sock_blocking(req->fd, 0);
    while (req->count < req->len) {
	cc = read(req->fd, &recvbuf[base + req->count],
		  req->len - req->count);
	nr_read++;

	if (cc < 0) {
	    if (errno == EAGAIN || errno == EWOULDBLOCK)
		return 0;
	    else
		perror_exit("read", 1);
	} else if (cc == 0) {
	    fprintf(stderr, "recv: connection closed\n");
	    exit(1);
	}
	req->count += cc;
	rcnt += cc;
    }

    return 0;
}
Example #2
0
/* request queue management functions */
int
passive_open(const int port)
{
    struct sockaddr_in sa;
    socklen_t salen;
    int fd, cc;
    int one = 1;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
	perror("socket");
	return -1;
    }

    cc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
    if (cc < 0) {
	perror("WARN: setsockopt(SO_REUSEADDR)");
	/* Ignore this error. */
    }

    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
    sa.sin_port = htons(port);
    salen = sizeof(sa);

    cc = bind(fd, (struct sockaddr *)&sa, salen);
    if (cc < 0) {
	perror("bind");
	return -1;
    }

    set_sock_opt(fd);
    set_sock_blocking(fd, 1);

    cc = listen(fd, 5);
    if (cc < 0) {
	perror("listen");
	return -1;
    }

    return fd;
}
int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int timeout)
{
	//WSADATA wsaData;
	
	struct addrinfo *result = NULL,
					*ptr = NULL,
					hints;
	int iResult=0,valopt=0;
	/*SOCKET ConnectSocket = INVALID_SOCKET; */
	struct timeval tv; 
	fd_set myset; 
	socklen_t lon;
	
	int nbTryConnect=0;
	char sport[50];
	sprintf_s(sport,50,"%d",port);

	/*
	Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        Printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }
	*/

	/* initialize Hints */
	ZeroMemory( &hints, sizeof(hints) );
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	/* Resolve the server address and port */
	iResult = getaddrinfo(host, sport, &hints, &result);
	if ( iResult != 0 ) {
		Printf("getaddrinfo failed: %d : %s\n", iResult,gai_strerror(iResult));
		cnx->error_code=-iResult;
		strncpy_s(cnx->error_string,2048,gai_strerror(iResult),2048);
		return 1;
	}
	/* Printf("getaddrinfo ok\n"); */

		
	/*Attempt to connect to the first address returned by
	 the call to getaddrinfo */
	ptr=result;

	/* Create a SOCKET for connecting to server */
	cnx->socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
	if (cnx->socket == INVALID_SOCKET) {
		Printf("Error at socket(): %ld\n", WSAGetLastError());
		cnx->error_code=-WSAGetLastError();
		strncpy_s(cnx->error_string,2048,"Unable to create socket",2048);
		freeaddrinfo(result);
		return 1;
	}
	/* Printf("Socket Ok\n"); */
	/*set Non blocking socket */
	set_sock_blocking(cnx->socket,0);
	/* Connect to server. */
	iResult = connect( cnx->socket, ptr->ai_addr, (int)ptr->ai_addrlen);
	if(iResult<0){
		if (WSAGetLastError() == EINPROGRESS) { 
        tv.tv_sec = timeout; 
        tv.tv_usec = 0; 
        FD_ZERO(&myset); 
        FD_SET(cnx->socket, &myset); 
        if (select(cnx->socket+1, NULL, &myset, NULL, &tv) > 0) { 
					lon = sizeof(int); 
					getsockopt(cnx->socket, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon); 
					if (valopt) { 
						fprintf(stderr, "Error in connection() %d - %s\n", valopt, strerror(valopt));
						cnx->error_code=valopt;
						strncpy_s(cnx->error_string,2048,strerror(valopt),2048);
						freeaddrinfo(result);
						closesocket(cnx->socket);
						cnx->socket = INVALID_SOCKET;
						return 1;
					} 
					/*connection ok*/
        } 
        else { 
			/*fprintf(stderr, "Timeout or error() %d - %s\n", valopt, strerror(valopt)); */
			cnx->error_code=3011;
			strncpy_s(cnx->error_string,2048,"Connect timed out",2048);
			freeaddrinfo(result);
			closesocket(cnx->socket);
			cnx->socket = INVALID_SOCKET;
			return 1;
        } 
     } 
     else { 
        /*fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); */
        cnx->error_code=-WSAGetLastError();
			strncpy_s(cnx->error_string,2048,"Error connecting",2048);
			freeaddrinfo(result);
			closesocket(cnx->socket);
			cnx->socket = INVALID_SOCKET;
        return 1;
     } 

		
	}
		
	/* Printf("Connexion ok\n"); */


	/*set blocking socket */
	set_sock_blocking(cnx->socket,1);

	
	/* Should really try the next address returned by getaddrinfo
	   if the connect call failed
	   But for this simple example we just free the resources
	   returned by getaddrinfo and print an error message */

	freeaddrinfo(result);

	if (cnx->socket == INVALID_SOCKET) {
		Printf("Unable to connect to server!\n");
		cnx->error_code=-1;
		strncpy_s(cnx->error_string,2048,"Unable to connect to server",2048);
		return 1;
	}
	/* Printf("fin de la fonction\n"); */

	return 0;
}