Created
July 10, 2021 17:22
-
-
Save king-11/a23157da71b01d1cf24a6ee8afc1fd7f to your computer and use it in GitHub Desktop.
Qthread socket debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> //printf | |
#include <string.h> //strcpy | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <netinet/tcp.h> | |
#include <netdb.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <qthread/io.h> | |
typedef struct sockaddr_storage sys_sockaddr_storage_t; | |
int main(int argc, char *argv[]) | |
{ | |
int socket_desc; | |
int iport = 8000; | |
//Create socket | |
const int val_sock = AF_INET; | |
socket_desc = socket(val_sock, SOCK_STREAM|SOCK_NONBLOCK, 0); | |
if (socket_desc == -1) | |
{ | |
printf("Could not create socket"); | |
} | |
int flags = fcntl(socket_desc, F_GETFL, 0); | |
fcntl(socket_desc, F_SETFL, flags | O_NONBLOCK); | |
// fill in address | |
sys_sockaddr_storage_t x; | |
memset(&x, 0, sizeof(x)); | |
if(val_sock == AF_INET){ | |
struct sockaddr_in *x1 = (struct sockaddr_in *)&x; | |
x1->sin_family = AF_INET; | |
x1->sin_port = htons(iport); | |
x1->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
} | |
else { | |
struct sockaddr_in6 *x1 = (struct sockaddr_in6 *)&x; | |
x1->sin6_family = AF_INET6; | |
x1->sin6_port = htons(iport); | |
x1->sin6_addr = in6addr_loopback; | |
} | |
// extract out data | |
char address[NI_MAXHOST], port[NI_MAXSERV]; | |
int err = getnameinfo((struct sockaddr*)&x, sizeof(sys_sockaddr_storage_t), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); | |
puts(address); | |
puts(port); | |
int n; | |
qt_begin_blocking_action(); | |
if ((n = connect(socket_desc, (struct sockaddr *)&x, sizeof(x))) < 0) | |
{ | |
if(errno != EINPROGRESS){ | |
puts(strerror(errno)); | |
exit(0); | |
} | |
} | |
qt_end_blocking_action(); | |
puts("connect tried"); | |
if(n == 0){ | |
printf("connect success"); | |
exit(0); | |
} | |
fd_set rset, wset; | |
FD_ZERO(&wset); | |
FD_ZERO(&rset); | |
FD_SET(socket_desc, &rset); | |
FD_SET(socket_desc, &wset); | |
struct timeval tval; | |
tval.tv_sec = 10; | |
tval.tv_usec = 0; | |
qt_begin_blocking_action(); | |
if((n = select(socket_desc + 1, NULL, &wset, NULL, &tval)) == 0){ | |
puts(strerror(ETIMEDOUT)); | |
exit(0); | |
} | |
qt_end_blocking_action(); | |
puts("select tried"); | |
if(n < 0){ | |
puts(strerror(errno)); | |
exit(0); | |
} | |
puts("select error checked"); | |
if(FD_ISSET(socket_desc,&rset) || FD_ISSET(socket_desc, &wset)) { | |
socklen_t len; | |
if(getpeername(socket_desc, (struct sockaddr *)&x, &len) < 0){ | |
puts("peername fail"); | |
puts(strerror(errno)); | |
exit(0); | |
if(errno == ENOTCONN){ | |
int err_t; | |
socklen_t len = sizeof(err_t); | |
if(getsockopt(socket_desc, SOL_SOCKET, SO_ERROR, &err_t, &len) < 0) { | |
puts("getsockopt fail"); | |
close(socket_desc); | |
puts(strerror(errno)); | |
exit(0); | |
} | |
if(err_t){ | |
close(socket_desc); | |
puts(strerror(err_t)); | |
exit(0); | |
} | |
} | |
} | |
} | |
else { | |
close(socket_desc); | |
puts("Select error: socketFd not set"); | |
exit(0); | |
} | |
puts("rset wset pass"); | |
printf("connect success"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> //printf | |
#include <string.h> //strcpy | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <netinet/tcp.h> | |
#include <netdb.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
typedef struct sockaddr_storage sys_sockaddr_storage_t; | |
int main(int argc, char *argv[]) | |
{ | |
int socket_desc; | |
int iport = 8000; | |
//Create socket | |
const int val_sock = AF_INET; | |
socket_desc = socket(val_sock, SOCK_STREAM|SOCK_NONBLOCK, 0); | |
if (socket_desc == -1) | |
{ | |
printf("Could not create socket"); | |
} | |
int flags = fcntl(socket_desc, F_GETFL, 0); | |
fcntl(socket_desc, F_SETFL, flags | O_NONBLOCK); | |
// fill in address | |
sys_sockaddr_storage_t x; | |
memset(&x, 0, sizeof(x)); | |
if(val_sock == AF_INET){ | |
struct sockaddr_in *x1 = (struct sockaddr_in *)&x; | |
x1->sin_family = AF_INET; | |
x1->sin_port = htons(iport); | |
x1->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
} | |
else { | |
struct sockaddr_in6 *x1 = (struct sockaddr_in6 *)&x; | |
x1->sin6_family = AF_INET6; | |
x1->sin6_port = htons(iport); | |
x1->sin6_addr = in6addr_loopback; | |
} | |
// extract out data | |
char address[NI_MAXHOST], port[NI_MAXSERV]; | |
int err = getnameinfo((struct sockaddr*)&x, sizeof(sys_sockaddr_storage_t), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); | |
puts(address); | |
puts(port); | |
int n; | |
if ((n = connect(socket_desc, (struct sockaddr *)&x, sizeof(x))) < 0) | |
{ | |
if(errno != EINPROGRESS){ | |
puts(strerror(errno)); | |
exit(0); | |
} | |
} | |
puts("connect tried"); | |
if(n == 0){ | |
printf("connect success"); | |
exit(0); | |
} | |
fd_set rset, wset; | |
FD_ZERO(&wset); | |
FD_ZERO(&rset); | |
FD_SET(socket_desc, &rset); | |
FD_SET(socket_desc, &wset); | |
struct timeval tval; | |
tval.tv_sec = 10; | |
tval.tv_usec = 0; | |
if((n = select(socket_desc + 1, NULL, &wset, NULL, &tval)) == 0){ | |
puts(strerror(ETIMEDOUT)); | |
exit(0); | |
} | |
puts("select tried"); | |
if(n < 0){ | |
puts(strerror(errno)); | |
exit(0); | |
} | |
puts("select error checked"); | |
if(FD_ISSET(socket_desc,&rset) || FD_ISSET(socket_desc, &wset)) { | |
socklen_t len; | |
if(getpeername(socket_desc, (struct sockaddr *)&x, &len) < 0){ | |
puts("peername fail"); | |
puts(strerror(errno)); | |
exit(0); | |
if(errno == ENOTCONN){ | |
int err_t; | |
socklen_t len = sizeof(err_t); | |
if(getsockopt(socket_desc, SOL_SOCKET, SO_ERROR, &err_t, &len) < 0) { | |
puts("getsockopt fail"); | |
close(socket_desc); | |
puts(strerror(errno)); | |
exit(0); | |
} | |
if(err_t){ | |
close(socket_desc); | |
puts(strerror(err_t)); | |
exit(0); | |
} | |
} | |
} | |
} | |
else { | |
close(socket_desc); | |
puts("Select error: socketFd not set"); | |
exit(0); | |
} | |
puts("rset wset pass"); | |
printf("connect success"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> //printf | |
#include <string.h> //strcpy | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <netinet/tcp.h> | |
#include <netdb.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <qthread/io.h> | |
#include <qthread/qt_syscalls.h> | |
#include <qthread.h> | |
typedef struct sockaddr_storage sys_sockaddr_storage_t; | |
int main(int argc, char *argv[]) | |
{ | |
int socket_desc; | |
int iport = 8000; | |
//Create socket | |
const int val_sock = AF_INET; | |
socket_desc = socket(val_sock, SOCK_STREAM|SOCK_NONBLOCK, 0); | |
if (socket_desc == -1) | |
{ | |
printf("Could not create socket"); | |
} | |
int flags = fcntl(socket_desc, F_GETFL, 0); | |
fcntl(socket_desc, F_SETFL, flags | O_NONBLOCK); | |
// fill in address | |
sys_sockaddr_storage_t x; | |
memset(&x, 0, sizeof(x)); | |
if(val_sock == AF_INET){ | |
struct sockaddr_in *x1 = (struct sockaddr_in *)&x; | |
x1->sin_family = AF_INET; | |
x1->sin_port = htons(iport); | |
x1->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
} | |
else { | |
struct sockaddr_in6 *x1 = (struct sockaddr_in6 *)&x; | |
x1->sin6_family = AF_INET6; | |
x1->sin6_port = htons(iport); | |
x1->sin6_addr = in6addr_loopback; | |
} | |
// extract out data | |
char address[NI_MAXHOST], port[NI_MAXSERV]; | |
int err = getnameinfo((struct sockaddr*)&x, sizeof(sys_sockaddr_storage_t), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); | |
puts(address); | |
puts(port); | |
int n; | |
if ((n = qt_connect(socket_desc, (struct sockaddr *)&x, sizeof(x))) < 0) | |
{ | |
if(errno != EINPROGRESS){ | |
puts(strerror(errno)); | |
exit(0); | |
} | |
} | |
puts("connect tried"); | |
if(n == 0){ | |
printf("connect success"); | |
exit(0); | |
} | |
fd_set rset, wset; | |
FD_ZERO(&wset); | |
FD_ZERO(&rset); | |
FD_SET(socket_desc, &rset); | |
FD_SET(socket_desc, &wset); | |
struct timeval tval; | |
tval.tv_sec = 10; | |
tval.tv_usec = 0; | |
if((n = qt_select(socket_desc + 1, NULL, &wset, NULL, &tval)) == 0){ | |
puts(strerror(ETIMEDOUT)); | |
exit(0); | |
} | |
puts("select tried"); | |
if(n < 0){ | |
puts(strerror(errno)); | |
exit(0); | |
} | |
puts("select error checked"); | |
if(FD_ISSET(socket_desc,&rset) || FD_ISSET(socket_desc, &wset)) { | |
socklen_t len; | |
if(getpeername(socket_desc, (struct sockaddr *)&x, &len) < 0){ | |
puts("peername fail"); | |
puts(strerror(errno)); | |
exit(0); | |
if(errno == ENOTCONN){ | |
int err_t; | |
socklen_t len = sizeof(err_t); | |
if(getsockopt(socket_desc, SOL_SOCKET, SO_ERROR, &err_t, &len) < 0) { | |
puts("getsockopt fail"); | |
close(socket_desc); | |
puts(strerror(errno)); | |
exit(0); | |
} | |
if(err_t){ | |
close(socket_desc); | |
puts(strerror(err_t)); | |
exit(0); | |
} | |
} | |
} | |
} | |
else { | |
close(socket_desc); | |
puts("Select error: socketFd not set"); | |
exit(0); | |
} | |
puts("rset wset pass"); | |
printf("connect success"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample server code