LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   socket programming (https://www.linuxquestions.org/questions/linux-general-1/socket-programming-706098/)

muchala123 02-20-2009 01:08 AM

socket programming
 
hello,
i have a doubt in socket programming..

i wrote a client socket program..and a server program..

in client program,,after creating a socket using socket system call,i did few things required for my program not related to socket..

after that i prepared the structures required for the socket,i used connect system call.

when i am running these two programs,i am getting error as unable to connect.i found that connect is returning -1..

but the same program when i put connect system call immediately after socket system call without doing anything other it is working fine?

please tell what might be the reason for it?

vkmgeek 02-20-2009 02:22 AM

depends on "wht other" you are doing ?
post code if possible

muchala123 02-20-2009 02:32 AM

Quote:

Originally Posted by vkmgeek (Post 3450709)
depends on "wht other" you are doing ?
post code if possible


client program:

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';
printf("%s\n%s\n",address,port);
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;
}
printf("%d\n",sock_fd);

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


if(connect(sock_fd,(struct sockaddr *)&s1,sizeof(s1)) == -1)
{
printf("error:unable to connect\n");
return -1;
}

like this if i do it is coming....but if the for loops are kept at below socket system call and above preparing that socket structures,then it is printing it is unable to connect..pls see it and tell me


All times are GMT -5. The time now is 08:47 AM.