Weird: get a socket connection without a running socket server !

Weird: get a socket connection without a running socket server !

Post by Christelle Dup » Wed, 01 Jan 2003 22:09:08

Hi folks,

I'm having a weird problem with sockets on Linux (RedHat 7.2, kernel
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);
        return (0);


The send_event function is provided by a linked library which formats
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)
                syslog(priority, "Error opening socket");
                return (1);
        server = gethostbyname("localhost");
        if (server == NULL)
                syslog(priority, "Error retrieving hostname");
        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,
        serv_addr.sin_port = htons(PORTNO);
        if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <
                syslog(priority, "Error connecting to REM");
        syslog(priority, "Got the connection\n");
        connected = 1;


I also try to use DDD to find out the problem but the only weird thing
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 ?



1. Opening a socket connection while in a socket connection.


I am new to using sockets and am working on a project that requires
me to audit services accessed under a client/server program (TCP/IP)
socket connection.

What I want to do is send a datapacket to a collector machine each
time a client accesses a service on the server.  It seems that
I am unable to do this from the server.  Basically, in short form
I need to be able to do the following:

1. Client connects (TCP/IP socket connection) to server
2. Client requests a service from the server
3. At that point I want to send a datapacket to a audit server.

Any Ideas?  I am familiar with most of the concepts involved in
using sockets.  Maybe I have been working on this to long and have
gotten tunnel vision.


Brent Curtiss

Academic Computing Services
The Ohio State University

2. kernel 2.4.20 compilation error without smp support

3. Sockets sockets and more sockets

4. How small can Linux/X be?

5. Socket, Socket, whose got the socket?

6. Telnet sessions immediately terminated by host

7. Socket, socket, who's got the socket?

8. Mystery page faulting kernel + silly *Gb limitation

9. Sockets sockets and more sockets

10. Making Linux server sockets connect with Borland C++ builder Client Sockets?

11. Sockets: server close leaves socket bound?

12. socket error 98 on RedHat 7.0, server restart tcp socket.

13. UDP server: single socket or multiple socket?