kylin-printer/common/connect.cpp

45 lines
1.1 KiB
C++
Raw Normal View History

2022-10-13 11:16:48 +08:00
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <QDebug>
#include "connect.h"
// return : - 0 on success or -1 on error
// addrname: - Hostname or IP address
// port - Port number
int try_connect(const char *addrname, int port)
{
int fd; /* Socket */
int status; /* Connection status */
struct sockaddr_in ipv4;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
qDebug("ERROR: Unable to create socket: %s\n",
strerror(errno));
return (-1);
}
bzero(&ipv4, sizeof(ipv4));
ipv4.sin_family = AF_INET;
ipv4.sin_addr.s_addr = inet_addr(addrname);
ipv4.sin_port = htons(port);
struct timeval timeout={5,0};
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
status = connect(fd, (struct sockaddr *)&ipv4, (socklen_t)sizeof(ipv4));
close(fd);
return (status);
}