Send and receive in TCP/IP



  • in my client side in receive function if (buffer[i] == work[i] )write 1 in pin until (c < 13)
    and when program in while,if socket receive stop to write 0 in pin,it doesn't work until while ends ,I want to socket any time receive stop it's write 0 in pin ,how can handle this?
    Client:

    bool client::conn(int num_pin_1, int num_pin_2) {
    	struct sockaddr_in serv_addr;
    	struct hostent *server;
    
    	sock = socket(AF_INET, SOCK_STREAM, 0);
    	if (sock < 0)
    		printf("ERROR opening socket");
    	else {
    		fflush(stdout);
    	}
    
    	server = gethostbyname("192.168.1.129");
    	if (server == 0) {
    		fprintf(stderr, "ERROR, no such host\n");
    		exit(0);
    	} else {
    		printf("connected");
    		fflush(stdout);
    	}
    	bzero((char *) &serv_addr, sizeof(serv_addr));
    	serv_addr.sin_family = AF_INET;
    	bcopy((char *) server->h_addr,
    	(char *)&serv_addr.sin_addr.s_addr,
    	server->h_length);
    	serv_addr.sin_port = htons(MYPORT);
    	while (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
    		perror("ERROR connecting");
    	}
    
    }
    
    bool client::rec_data(int num_pin_1, int num_pin_2) {
    
    rc=recv(sock, buffer, sizeof(buffer), 0);
    	if ( rc < 0) {
    		perror("receive failed");
    		return false;
    	}
    for (int i = 0; i < 2; i++) {
    if (buffer[i] == work[i] ){	
    	while (c < 13) {	
     digitalWrite(pin2, HIGH);
     digitalWrite(pin1, LOW);
    		}
    		   }
    if (buffer[i] == Stop[i]) {
     digitalWrite(pin2,  LOW);
     digitalWrite(pin1, LOW);
    }
      }
    

  • Lifetime Qt Champion

    Hi,

    Why not use QTcpSocket ?



  • @SGaist Hi, I'm use orange pi board and QT not compile on it


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.