Results 1 to 3 of 3
Hello everyone.
I am programming an application with an ARM device with an embedded version of Linux. My application talks to a java application via socket. If there is any ...
- 07-16-2010 #1
[SOLVED] Socket and Segmentation Fault.
Hello everyone.
I am programming an application with an ARM device with an embedded version of Linux. My application talks to a java application via socket. If there is any connection problems, it attempts the connection again. My problem is that after exactly 146 times, there is a Segmentation Fault. Apparently this happens in opening the socket, which is not successful after this amount of attempts.
Following, some code that I'm using:
The function for openning the socket and perform a connection:
and the problematic part:Code:int initConnection(char *host, int port) { int fd, countError = 0, connected = -1; struct sockaddr_in destination; struct hostent *he; fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd < 0) { printf("Socket error.\n"); return -1; } he = gethostbyname(host); if (!he) { printf("DNS error.\n"); return -1; } bzero(&destination, sizeof(destination)); destino.sin_family = AF_INET; destino.sin_port = htons(port); destino.sin_addr = *((struct in_addr*) he->h_addr); while (connected < 0 && countError < 5) { connected = connect(fd, (struct sockaddr*) &destination, sizeof(destination)); countError++; } if (countError == 5) { printf("Too many errors.\n"); close(fd); fd = -1; } return fd; }
If someone can help me, thanks!Code:..... while (true) { error = false; readConfigParametersClient("/disk/home/httpd/cgi-bin/Pages/connSettings.dat", host, &port, user, pass); fd = initConnection(host, port); if (fd < 0) { printf("Connection error.\n"); continue; } counter++; .....
Sorry for the bad English, I'm brazilian.
- 07-18-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Each time you go into initConnection() you create a new socked, yet when you exit on error you only close it on the 5th attempt, leaving 4 older sockets open/dangling. Your problem is that you can only have so many open sockets before the system refuses the socket() call.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-18-2010 #3
Ok. I'll try to close the socket if connect() fail and post here success or error in it.


