R
Robert Smith
using redhat 8.0 and I have the following code....
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define DEST_IP "64.12.161.153"
// #define DEST_IP "127.0.0.1"
#define DEST_PORT 235
int main()
{
int sockfd;
int connectOK;
struct sockaddr_in dest_addr; // will holdthe destination addr
sockfd = socket(AF_INET, SOCK_STREAM, 0); //do some error checking!
dest_addr.sin_family = AF_INET; //host byte order
dest_addr.sin_port = htons(DEST_PORT); //short, network byte order
dest_addr.sin_addr.s_addr =inet_addr(DEST_IP);
memset(&(dest_addr.sin_zero), '\0', 8); //zero the rest of the
struct
// don't forget to error check the connect()!
connectOK = connect(sockfd, (struct sockaddr*)&dest_addr,
sizeof(struct sockaddr));
if(connectOK==-1)
printf("not connected");
else
printf("connected");
return 0;
}
When i use the ip address 64.12.161.153 which is the correct server it says
its connected.
When i use the ip address 127.0.0.1 which is not the correct server is says
not connected.
When i use any other IP address it just hangs there and doesnt print
anything, why is this?
Also after it connects how can i test to see how long it will hold the
connection open for untill the remote server closes it?
thanks
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define DEST_IP "64.12.161.153"
// #define DEST_IP "127.0.0.1"
#define DEST_PORT 235
int main()
{
int sockfd;
int connectOK;
struct sockaddr_in dest_addr; // will holdthe destination addr
sockfd = socket(AF_INET, SOCK_STREAM, 0); //do some error checking!
dest_addr.sin_family = AF_INET; //host byte order
dest_addr.sin_port = htons(DEST_PORT); //short, network byte order
dest_addr.sin_addr.s_addr =inet_addr(DEST_IP);
memset(&(dest_addr.sin_zero), '\0', 8); //zero the rest of the
struct
// don't forget to error check the connect()!
connectOK = connect(sockfd, (struct sockaddr*)&dest_addr,
sizeof(struct sockaddr));
if(connectOK==-1)
printf("not connected");
else
printf("connected");
return 0;
}
When i use the ip address 64.12.161.153 which is the correct server it says
its connected.
When i use the ip address 127.0.0.1 which is not the correct server is says
not connected.
When i use any other IP address it just hangs there and doesnt print
anything, why is this?
Also after it connects how can i test to see how long it will hold the
connection open for untill the remote server closes it?
thanks