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 48 49 50 51 52 53 54 55 56 57 58 59 60
|
bool setup::conn() {
struct sockaddr_in serv_addr;
struct hostent *server;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1){
printf("ERROR opening socket");
fflush(stdout);
}
server = gethostbyname("192.168.1.113");
if (server <= 0) {
fprintf(stderr, "ERROR, no such host\n");
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 (true) {
fflush(stdout);
csock = (int*) malloc(sizeof(int));
*csock = connect(sock, (struct sockaddr *) &serv_addr,
sizeof(serv_addr));
if (*csock == -1) {
perror("error");
if (errno == EISCONN )
break;
}
thread thread1;
thread1 = thread(&setup::SocketHandler, this);
thread1.join();
}
}
void setup::SocketHandler() {
while (1) {
if ((bytecount = recv(*csock, buffer, buffer_len, 0)) == -1)
fprintf(stderr, "Error receiving data %d\n", errno);
if (bytecount > 0) {
printf("\n Received bytes: %d ", bytecount);
}
fflush(stdout);
usleep(100);
}
pthread_exit(0);
}
|