Connect with server and return results

Hello i am beginner and i try to programming.I want to create a program that send in a server the question about time and the server return to client the result.But i have ths below problem in code in the line 12 .The error is ||=== Build: Release in test client (compiler: GNU GCC Compiler) ===|
/home/server/Desktop/C++/TestClient/test client/main.cpp|1|warning: character constant too long for its type [enabled by default]|
/home/server/Desktop/C++/TestClient/test client/main.cpp|12|error: too many decimal points in number|
/home/server/Desktop/C++/TestClient/test client/main.cpp|1|error: expected unqualified-id before '\x702e6822'|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   '#include "unp.h"'
2 int
3 main(int argc, char **argv)
4 {
5 int sockfd, n;
6 char recvline[MAXLINE + 1];
7 struct sockaddr_in servaddr;
8 if (argc != 2)
9 err_quit("usage: a.out <192.168.2.5>");
10 if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
11 err_sys("socket error");
12 bzero(&servaddr, sizeof(192.168.2.6));
13 servaddr.sin_family = AF_INET;
14 servaddr.sin_port = htons(13); /* daytime server */
15 if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
16 err_quit("inet_pton error for %s", argv[1]);
17 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
18 err_sys("connect error");
19 while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
20 recvline[n] = 0; /* null terminate */
21 if (fputs(recvline, stdout) == EOF)
22 err_sys("fputs error");
23 }
24 if (n < 0)
25 err_sys("read error");
26 exit(0);
27 }
.
-.- please remember your formatting when you post something.
Every time you don't another polar bear drinks a coke and now we have an obesity problem here in Antarctica.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "unp.h" //im assuming this has all the socket includes

int main(int argc, char *argv[]) {
    if (argc != 2) {
        err_quit("usage: a.out <server-ipv4-address>");
        return 1;
    }    
    
    int sockfd; //The file descriptor of socket() call
    int n; //The number of bytes read from recv()
    char recvline[MAXLINE + 1]; //The buffer where you'll be recieving messages to

    addrinfo hints; //hints that the DNS service will use to find the host you're connecting to.
    memset(&hints, 0, sizeof(hints)); //clear out hints
    hints.ai_family = AF_INET; //IPV4 address
    hints.ai_socktype = SOCK_STREAM; //The socket will be streaming data
    hints.ai_protocol = IPPROTO_TCP; //The packets are guaranteed to reach their destination in order

    addrinfo *result; //will be the results of a DNS request through getaddrinfo()
    
    //http://linux.die.net/man/3/getaddrinfo
    if (getaddrinfo(*argv[2], "13"/*port*/, &hints, &result) != 0) {
        err_sys("Could not find server");
        return 2;
    }

    sockfd = socket(hints.ai_family, hints.ai_socktype, 0);
    if(sockfd == -1) { //unsuccessful
        err_sys("socket() failed");
        return 3;
    }

    int success = -1;
    for (addrinfo *i = result; (i != nullptr) && (success != 0); i=i->ai_next)
        connect( sockfd, (sockaddr*)result, sizeof(result) );
        //connect to each returned address until you reach the end of the list or connect successfull

    do {
        n = recv(sockfd, recvline, MAXLINE, 0); //wait to recieve a message
        recvline[n] = 0; /* null terminate */ 
        fputs(recvline, stdout); //print the message to the screen
    } while (n > 0)
    if (n < 0)
        err_sys("read error");
    close(sockfd);
    return 0;
}
Thanks for your reply but i want to say that now with your code i have the problem with

#include "unp.h" thar appears the below error :

fatal error:unp.h no such file or directory
Topic archived. No new replies allowed.