tags:

views:

426

answers:

8

I wrote this program in C++ and on Linux platform.

I wrote a client and server socket program.

In that client program, I wrote socket function and immediately after that I am doing some other actions not at all depending on socket (I wrote 2 for loops for some other logic).

After that I prepared the structures required for the socket and I wrote the connect function...in that I am getting error as unable to connect because connect is returning -1..

But for the same program, if I write that for loop's logic above the socket function and immediately after that structures, and connect function, then it is working fine..

What might be the reason I am not able to get? Please help me in this aspect. Here is my code


here index1 and index 2 are simple integer variables..The configstring is a char array contains 127.0.0.1:7005(address and port number)...address and port are char array variables to store address and port number..

struct sockaddr_in s1;

for(index1=0;configstring[index1]!=':';index1++)
{
    address[index1] = configstring[index1];
}
address[index1++]='\0';

for(index2=0;configstring[index1]!='\0';index1++,index2++)
{
    port[index2] = configstring[index1];
}
port[index2++]='\0';

int port_num = changeto_int(port);

if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
    printf("unable to create a socket\n");
    return 0;
}

s1.sin_family=AF_INET;
s1.sin_port=htons(port_num);
s1.sin_addr.s_addr=inet_addr(address);
memset(s1.sin_zero, '\0', sizeof s1.sin_zero);
int errno;

if(connect(sock_fd,(struct sockaddr *)&s1,sizeof(s1)) == -1)
{
    printf("error:unable to connect\n");
    printf("Error in connect(): %s\n", strerror( errno));
    return -1;
}
A: 

Have you tried calling strerror on errno? connect() returning -1 would mean that errno has been set and should have more information about your error.

printf("Error in connect(): %s\n", strerror(errno));
Suroot
This is equivalent to calling:perror("Error in connect()");
vladr
Almost - perror writes to stderr, while printf to stdout.
Adam Hawes
i tried with strerror..it is showing :"not a directory"
A: 

Have you considered simply that your receiver is not listening properly for connections?

As others said, use perror to check errno and print some usable debug to the console.

Without your sample code there is no way to help you. There could be a million reasons. Perhaps there's a firewall on your machine blocking connections? Perhaps the server isn't listening, or is on an incorrect port (you did convert to network byte order didn't you?). Perhaps the client is connecting to a wrong address or port. Maybe you haven't set up your structures correctly.

I recommend reading Beej's Socket Programming Doo-Daa for a good introduction to sockets on Unix (and it follows on to Windows).

Adam Hawes
+1  A: 

Once in a while you run into a book that stands head, shoulders and torso above all of the other books out there.

If you REALLY want to understand networking at the OS level (which is where networking "happens"), get a copy of Advanced Programming in the UNIX(R) Environment. It gives you the "guts", "history", and "howto" in regards to programming on UNIX, with much content dedicated to networking and IPC.

gahooa
That's a good book, but UNIX Network Programming Vol 1, 3rd Edn (same author) might be even better.
Jonathan Leffler
+2  A: 

First, never do something like this:

int errno;

errno is already defined for you.

More than that I suggest you to use perror() instead of

printf("Error in connect(): %s\n", strerror( errno));

Third, you can't call printf and than strerror( errno) because printf whould change value of errno to success.

Third, I'd sugget to take a look on examples in internet and start from them.

I'd suggest to read man select_tut there are many good written code examples of how to do and what.

Artyom
i used perrorit is coming as "socket operation on non socket
A: 

struct sockaddr_in s1;

could you please try memset of s1, at the beginning of your program. I have experienced some thing similar to this.

Warrior
A: 

Could you print debugging info about the address and port string ?

Remove the errno thing, include and use perror.

Compile with -Wall

shodanex
A: 

Low level socket programming is tedious and error prone. You would be well advised to start using frameworks like Boost or ACE that shield you from these low level details and allow you to program platform independent.

lothar
A: 

Judging from your comment that perror() returns "socket operation on non socket"... How are your address and port variables declared? Is it possible that port[index2++]='\0' somehow overwrite onto sock_fd or such?

billyswong