Hello everybody!
I’m trying to make a simple blocking socket connection between only 2 computers running QNX 6.0.
My problem is that the SO_RCVTIMEO receive timeout option that I need on the server does not work.
If the client sends messages to the server, they are received on the server properly.
Unfortunately, if the client no longer sends messages, the server remains blocked in read even it must exit by timeout after 5 seconds!
Maybe SO_RCVTIMEO is not working in QNX 6.0?
Any help will be highly appreciated.
Best regards,
Theodor
My C code is:
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdbool.h>
#include <sys/neutrino.h>
#include <sys/time.h>
#define MAX 80
#define PORT 42222
#define SA struct sockaddr
int sockfd;
// Function designed for chat between client and server.
void func(int connfd)
{
char buff[MAX];
int n;
int contor_read;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client
contor_read = read(connfd, buff, sizeof(buff));
if ( contor_read < 0 )
{
printf("Read timeout!\n");
}
// print buffer which contains the client contents
printf("From client: %s \n", buff);
}
}
}
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
int rcode;
struct timeval tv;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, , &tv, sizeof(tv));
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
}
else
printf("server accept the client...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd);
}