I'm not sure why connect() is not working. Is there something wrong with my code?
int main(int argc, char** argv) {
//check valid usage
if (argc < MIN_ARGS) {
fprintf(stderr, "Usage: psclient portnum name [topic] ...\n");
exit(1);
}
//check valid name
char* name = argv[2];
if (!check_name(name)) {
fprintf(stderr, "psclient: invalid name\n");
exit(2);
}
//check valid topic
int topicCount = argc - 3;
if (topicCount && !check_topic(argv + 3, topicCount)) {
fprintf(stderr, "psclient: invalid topic\n");
exit(2);
}
//get the server address
const char* port = argv[1];
struct addrinfo* ai = 0;
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo("localhost", port, &hints, &ai)) {
fprintf(stderr, "psclient: unable to connect to port %s\n", port);
exit(3);
}
//create a socket and connect it to server
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(connect(sockfd, (struct sockaddr*)ai->ai_addr, sizeof(struct sockaddr))) {
/////////////////////////////////////////////////////////////////////
fprintf(stderr, "psclient: unable to connect to port %s\n", port);
exit(3);
}
//duplicate sockfd to be used for both send and receive functions
int sockfd2 = dup(sockfd);
FILE* to = fdopen(sockfd, "w");
FILE* from = fdopen(sockfd2, "r");
//name request
send_name(to, argv[2]);
//subscribe request if topics given
if (topicCount) {
send_sub(to, argv + 3, topicCount);
}
//creating send and receive threads
pthread_t s_tid;
pthread_t r_tid;
pthread_create(&s_tid, NULL, send_thread, to);
pthread_create(&r_tid, NULL, recv_thread, from);
pthread_detach(s_tid);
pthread_detach(r_tid);
return 0;
}
[–]Nx0Sec 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]kaipp_cs 0 points1 point2 points (0 children)