Hi folks,
I'm having a weird problem with sockets on Linux (RedHat 7.2, kernel
2.4.17).
I somehow manage to get a connection without having a socket server
listening on the port !! (Moreover the local and foreign address are
the same !)
When using netstat, here is the result I have
tcp 0 0 localhost.localdo:36000 localhost.localdo:36000 ESTABLISHED
In fact here is what I want to do.
I have a socket server which listen on a specific port (36000 for
now), accept incoming connections and handling the messages.
I have a client program which send events by using an API.
The client program is as simple as this
int main()
{
long i;
long j = 0;
while (1)
{
for (i=0 ; i<=30000 ; i++)
{
printf("%d *** %d\n",i,j);
send_event(1, 3, "main", "", 0, NULL, NULL);
}
sleep (10);
j++;
}
return (0);
The send_event function is provided by a linked library which formatsQuote:}
the arguments into a correct message, checks whether the connection
with the server is alive and sends the message to the server by
writing it on the socket. Before writing on the socket, a test is
performed to verify whether the socket is up. If not, the library try
to re-open the connection with the server.
In my case, the socket server is not running meaning I should NOT be
able to connect to it and my client is running, trying to send
messages. So for each message the library tries to established a
connection, the connection fails and the message is not sent. To this
point everything seems to be working fine. The problem is when I reach
message 288XX (or at least a certain number), the library somehow
manages to get a connection (connection describe by the result of the
netstat command I've put at the beginning of the post) and the client
is now stuck.
Here is the code snipet used in the library to establish a connection
int connectToServer()
{
struct sockaddr_in serv_addr;
struct hostent *server;
char *ident = "C Interface";
int facility = LOG_LOCAL1;
int option = LOG_NDELAY | LOG_PID;
int priority = LOG_ERR;
/* If connectToServer is called that means we were disconnected at
some point*/
connected = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
openlog(ident,option,facility);
syslog(priority, "Error opening socket");
closelog();
return (1);
}
server = gethostbyname("localhost");
if (server == NULL)
{
openlog(ident,option,facility);
syslog(priority, "Error retrieving hostname");
closelog();
close(sockfd);
return(2);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(PORTNO);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <
0)
{
openlog(ident,option,facility);
syslog(priority, "Error connecting to REM");
closelog();
close(sockfd);
return(3);
}
openlog(ident,option,facility);
syslog(priority, "Got the connection\n");
closelog();
connected = 1;
return(0);
I also try to use DDD to find out the problem but the only weird thingQuote:}
I can see is that when it establishes the weird connection I have
struct hostent *server.h_name = 0x2edc <Address 0x2edc out of bounds>
(same problem for h_addr_list <Address 0x57e58955 out of bounds>)
Does anybody know what I am doing wrong or what the problem is ?
Christelle