CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Apr 2017
    Posts
    3

    Question receive in tcp client c++

    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:
    Code:
    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);
        }
    }
    Last edited by 2kaud; April 9th, 2017 at 05:22 AM. Reason: Formatting for clarity

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured