julen
04-27-2009, 03:13 PM
Hi!
I am trying to connect two processes using BSD Sockets under Ubuntu using latest gcc compiler.
I do what all the stuff found in Internet tell me to do but I am stuck in an strange segmentation fault. I think is something related with memory management but I can still no see it. It's just happens beforen calling send() method.
Here is the code:
int
Ics::RunOneEvent ()
{
struct sockaddr_in stSockAddr;
int Res;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == SocketFD)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
}
//Inicializa memoria.
memset(&stSockAddr, 0, sizeof(stSockAddr));
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1983);
Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
if (0 > Res)
{
perror("error: first parameter is not a valid address family");
close(SocketFD);
exit(EXIT_FAILURE);
}
else if (0 == Res)
{
perror("char string (second parameter does not contain valid ipaddress");
close(SocketFD);
exit(EXIT_FAILURE);
}
if (-1 == connect(SocketFD, (sockaddr*)&stSockAddr, sizeof(stSockAddr)))
{
perror("connect failed");
close(SocketFD);
exit(EXIT_FAILURE);
}
char *code;
strcpy (code,"002;");
puts (code); //I can see the code value but just crash happens here.
int a = send (SocketFD, code, strlen(code), 0);
printf("%d",a); //This value is not printed.
shutdown (SocketFD, SHUT_RDWR);
close (SocketFD);
}
I am pretty newbie in C++ but this is driving me crazy since I wanted to learn about sockets and I did just what every tutorial says. :(
Thank you very much in advance for your advice.
Julen.
I am trying to connect two processes using BSD Sockets under Ubuntu using latest gcc compiler.
I do what all the stuff found in Internet tell me to do but I am stuck in an strange segmentation fault. I think is something related with memory management but I can still no see it. It's just happens beforen calling send() method.
Here is the code:
int
Ics::RunOneEvent ()
{
struct sockaddr_in stSockAddr;
int Res;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == SocketFD)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
}
//Inicializa memoria.
memset(&stSockAddr, 0, sizeof(stSockAddr));
stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1983);
Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
if (0 > Res)
{
perror("error: first parameter is not a valid address family");
close(SocketFD);
exit(EXIT_FAILURE);
}
else if (0 == Res)
{
perror("char string (second parameter does not contain valid ipaddress");
close(SocketFD);
exit(EXIT_FAILURE);
}
if (-1 == connect(SocketFD, (sockaddr*)&stSockAddr, sizeof(stSockAddr)))
{
perror("connect failed");
close(SocketFD);
exit(EXIT_FAILURE);
}
char *code;
strcpy (code,"002;");
puts (code); //I can see the code value but just crash happens here.
int a = send (SocketFD, code, strlen(code), 0);
printf("%d",a); //This value is not printed.
shutdown (SocketFD, SHUT_RDWR);
close (SocketFD);
}
I am pretty newbie in C++ but this is driving me crazy since I wanted to learn about sockets and I did just what every tutorial says. :(
Thank you very much in advance for your advice.
Julen.