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

Thread: receive in tcp client c++

  1. #1
    Join Date
    Apr 2017
    Posts
    2

    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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: receive in tcp client c++

    Code:
           while (c < 13) {	
                digitalWrite(pin2, HIGH);
                digitalWrite(pin1, LOW);
    	}
    How does c get initialised and changed?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  3. #3
    Join Date
    Apr 2017
    Posts
    2

    Re: receive in tcp client c++

    c is:

    d= clock();
    c=double(clock() - d) / CLOCKS_PER_SEC;

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,700

    Re: receive in tcp client c++

    But in the loop, either the loop is not executed at all (c >= 13) or if c < 13 then the loop is infinite as c isn't changed in the loop
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)