Can't connect to a specified port@address


I’m trying to get my application up and running but I have some trouble
starting. I am using some code that I found on openQNX to get the connection.
I have read this topic: … pic&t=8140
I was hoping that I will manage to connect to a 23 port from my QNX
but unfortunately the connect() returns -1;
HELP! I would like to know how to connect to remote hosts.

I got such an environment

Windows Vista
(+ Telnet Server ) @SH7760 with QNX

QNX application code:

[i]char dest_ip[]=“”;
int dest_port=23;
char *packet_ptr = NULL;
int n;
int sfd;
struct sockaddr_in sa_in;
int send_bytes = 0;
int recv_bytes = 0;

if ( (sfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
// Code for sending error message
return 0;
//Code for sending info message

sa_in.sin_family = AF_INET;
sa_in.sin_addr.s_addr = inet_addr(dest_ip);
sa_in.sin_port = htons(dest_port);
if ( (connect(sfd, (struct sockaddr *) &sa_in, sizeof(sa_in))) == SOCKET_ERROR)
//Code for sending error message
return 0;
} else {
//Code for sending information message

connects returns -1
The port on Vista is open ( can telnet to it), firewall is off
Can ping form vista to sh and form sh to vista
Wireshark shows no activity to 23 port

what can be wrong?

Is this a typo: The source addresses in your code don’t match. @SH7760 with QNX

char dest_ip[]=“”;

the code is running on SH7760 with QNX
source ip is and the dest ip

so its correct?

Can you connect in any way to the qnx machine?

So telnet, ftp similar?
Since they belong to other subnets they would need to have a router in between.

Are you SURE it’s and not Either you description is wrong or the code is wrong.

Micro: if the netmask is they can be on the same subnet. Plus the OP mentionned it would ping and telnet to the vista machine, I guess it’s safe to assume everything is ok config wise.



good point - need to check it on Monday 8) thx

thanks for picking up my mistake!
beside that the code works :slight_smile:


Oh ^^ i did not see that ping works.
Read carefully again ^^

I need some more help :confused:
There are many pieces of code that I found on the internet but most of them
are not working :confused:

I need to listen to a specific tcp port and throw what I receive to a buffer:

int sock;
struct sockaddr_in server;
int msgsock;
char buf[1024];
int rval;

sock = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_port = 1666;
bind(sock, (struct sockaddr *)&server, sizeof(server));

listen(sock, 5);
do {
    msgsock = accept(sock, 0, 0);
    if (msgsock == -1) 
        return EXIT_FAILURE;
    } else 
    do {
        memset(buf, 0, sizeof(buf));
        rval  = read(msgsock, buf,  1024);
            PtTextModifyText( ABW_PtText1, 0, 0, -1, buf,sizeof(buf));
    } while (rval > 0);
} while (1);

Could anyone have a quick look at this and tell me why its wrong?
Thanks! :slight_smile:

you need to use hton on the assignement to sin_port otherwise I thing you end up with using port 6616

memset(&server, 0, sizeof(sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(1666);
server.sin_addr = INADDR_ANY;

memset(&server, 0, sizeof(sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(1666);
server.sin_addr = INADDR_ANY;

I change the first line to memset(&server, 0, sizeof(server)); because I was not compiling.
I got an error on the 4th line - incompatible types in assignment :confused:

yea, sorry, i meant
memset(&server, 0, sizeof(struct sockaddr_in));
server.sin_addr.s_addr = INADDR_ANY;

Thank you mezek and mario :slight_smile:

As I’m developing under windows I had to think a way to test it.
I found this code in C# tcp server and client in c#

I still got a problem that my server on qnx receives only first message but I will get to the bottom of this.

Both the client and the server written for qnx works - if anyone will
in the future run into this post.

I wonder - do You think its a good idea to put a loop in which the connections are
accepted and the messages received in a separate thread?
( I’m using PhAB If anyone asks)


That’s up to you whether or not you want to split it up like that.

Here is a nice sample program that I think does what you want (accept new connections and handle incoming messages) using just 1 loop (look for the cheesy multi-person chat server code sample). … tml#select


If you are into C++ check the popo library. They have a nice set of object for handling TCP/UDP.

poco library is a C++ lib
I need to use photon which is C like.

Photon can be used from C++ program.

how? 8)