Home > Archive > Unix Programming > May 2004 > newbie socket question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
newbie socket question
|
|
| Robert Smith 2004-05-23, 2:31 am |
| 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
| |
| Barry Margolin 2004-05-23, 2:31 am |
| In article <UHWrc.76110$hY.21207@twister.nyroc.rr.com>,
"Robert Smith" <none@none.com> wrote:
> 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?
How long did you wait? Connect() will only return right away if it gets
a response from the destination address. If there's no machine with
that address, it will send the connection attempt, but not get a reply.
It assumes that this is because of a possibly temporary communication
problem (the packet may have been dropped due to congestion, for
instance) and keeps trying for several minutes. Eventually (about 3.5
minutes on many Unix systems) it will time out and report an error.
> 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?
It should stay open until the server closes the connection or there's a
long communication failure.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Viktor Lofgren 2004-05-23, 10:31 am |
| Robert Smith <none@none.com> wrote:
> 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
First off all, i would do things a bit different,
memset(&(dest_addr.sin_zero), '\0', 8); //zero the rest of the
is bad. In the future or on some other platform sin_zero may
be 4 bits long, or 25. Instead, do it like this:
memset(&dest_addr.sin_zero, '\0', sizeof(dest_addr.sin_zero));
You should never assume the size of datastructures.
Now, for your questions:
If you just type a random IP, it's a good chance that IP dosent exist (or
isn't up). Then your computer will try to find that computer (without
luck) and stall for quite some time. Some setups are also set to
ignore (drop) incomming packets to ports that are not open, that would
leave you with the same result.
IIRC You can check if the connection is up by using select(2) or poll(2).
If they fail (returns < 0) the connection is reset.
Good luck with your socket programming :-)
|
|
|
|
|