public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] RE : [ECOS] I lose sockets
@ 2006-08-16 14:17 Roger Arn 
  2006-08-16 14:28 ` Andrew Lunn
  0 siblings, 1 reply; 2+ messages in thread
From: Roger Arn  @ 2006-08-16 14:17 UTC (permalink / raw)
  To: ecos-discuss

[-- Attachment #1: Type: text/plain, Size: 1393 bytes --]


> is soclose being called on the socket? If so, what over bits are set 
> in so->so_state? soclose() needs to get > to the code after the 
> discard: label to set the SS_NOFDREF to true in order that sofree will

> actually free the socket. Is this happening and if not why?


Yes, soclose is being called. And at the label discard: it sets the
SS_NOFDREF. But this is done for an other socket. It works like that:

With 
hmiParameter->IPsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
It allocates a socket (s1). Then the application hangs in the accept()
method until an external application connect to my application. At this
moment it allocates a second socket (s2). Now I close the external
application and my application calls the soclose() which deallocats s1
in the manner you told me before. But s2 has never been allocated and is
lost. In my opinion, s2 has to be deallocated when I'am closing the
external application (before calling the close()), because it tryes to
do it. But this socket (s2) has a wrong state and fails in the sofree()
methode. For s2 it never calls the methode soclose(). Soclose() is
called from my application code and works fine. But here, s2 was created
automatically in the accept() methode when an external application
connects. So it should deallocate automatically too when the connection
stops, right ?



Roger Arn

[-- Attachment #2: Application_close.txt --]
[-- Type: text/plain, Size: 4803 bytes --]

while (true) {
			tv.tv_sec = 1;
			tv.tv_usec = 250000;
			FD_ZERO(&in_fds);
			FD_SET(hmiParameter->client, &in_fds);
			num = select(hmiParameter->client + 1, &in_fds, 0, 0, &tv);
			if (num > 0) {
				len = read(hmiParameter->client, buf, sizeof(buf)-1);
				if (len > 0){
					frameLength = inputFrame.getLength();
					if (frameLength > 300) {
						inputFrame.setLength(0);
						frameLength = inputFrame.getLength();
					}
					for (int i = 0; i < len; i++) {
						frame[frameLength] = buf[i];
						frameLength++;
					}
					inputFrame.setLength(frameLength);
					if (frameLength >= HEADER_LGTH) {
						Header head;
						inputFrame.getHeader(head);
						if (inputFrame.getDataLength() == head.length) {
							bool isCrcOk = crc.checkCRC(&inputFrame);
							if(isCrcOk) {
							//				diag_printf("CRC ok\n");
								hmiParameter->channel = CHANNEL_ETHERNET_TCP;
								handleReceivedFrame(&inputFrame);
//								cmdInterpreter->processFrame(&inputFrame);
							}else{
								// TODO logg the comunication error.
								diag_printf("CRC error !  cmdNbr : %d  cmdID : %d \n", inputFrame.getLineNumber(), inputFrame.getId());
//								sendAcknowledge(false, head.numeroDeLigne);
								//This is only temporarly until....
								// ....the communcication error listener is implemented.
							}
							inputFrame.setLength(0);
						}else{
							inputFrame.setLength(0);
						}
					}else{
						inputFrame.setLength(0);
					}
		//				diag_printf("buf = '%s'\n", buf);
				}else{
					disconnect();
					((Task*)(this))->delayFor(2000);
					connect();
				}
			} else if (num == 0) {
				inputFrame.setLength(0);
			} else {
				diag_printf("ReceiverMain: Select error! \n");
				disconnect();
				((Task*)(this))->delayFor(2000);
				connect();
			}
		}

void disconnect(){
		if (hmiParameter->IPsocket != 0){
		  if (hmiParameter->client > 0){
			close(hmiParameter->IPsocket);
		  }
		diag_printf("HMI disconnected \n");
	}


connect() {
:
:
:
struct sockaddr_in local;
int client_len;
int one = 1;
hmiParameter->IPsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hmiParameter->IPsocket < 0) {
    diag_printf("ReceiverMain: Invalid Socket \n");
}
if (setsockopt(hmiParameter->IPsocket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
    diag_printf("ReceiverMain: Setsockopt SO_REUSEADDR \n");
}
if (!setsockopt(hmiParameter->IPsocket, SOL_SOCKET, SO_RCVTIMEO, 0, sizeof(0))) {
    diag_printf("Error Socket-option SO_RCVTIMEO \n");
}
if (!setsockopt(hmiParameter->IPsocket, SOL_SOCKET, SO_SNDTIMEO, 0, sizeof(0))) {
    diag_printf("Error Socket-option SO_SNDTIMEO \n");
}
if (!setsockopt(hmiParameter->IPsocket, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
    diag_printf("ReceiverMain: Setsockopt SO_REUSEPORT \n");
}
// Set blocking:
unsigned long y = 0;
ioctlsocket(hmiParameter->IPsocket, FIONBIO, &y);
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_len = sizeof(local);
local.sin_port = MAIN_IN_PORT;
hmiParameter->client = 0;
local.sin_addr.s_addr = INADDR_ANY;
int retValue = bind(hmiParameter->IPsocket, (struct sockaddr *) &local, sizeof(local));
if (retValue < 0) {
    diag_printf("ReceiverMain: Bind error! \n");
}else{
    retValue = listen(hmiParameter->IPsocket, SOMAXCONN);
    if (retValue < 0){
        diag_printf("ReceiverMain: Listen error! \n");
    }else{
        client_len = sizeof(hmiParameter->hmiAddress);
        struct sockaddr_in client_addr;
        memset(&client_addr, 0, sizeof(client_addr));
        socklen_t x = (socklen_t)&client_addr;
        if ((hmiParameter->client = accept(hmiParameter->IPsocket, (struct sockaddr *)&client_addr, &x)) < 0) {
            diag_printf("ReceiverMain: Accept Error! \n");
        }else{
            hmiParameter->hmiAddress = client_addr;
            hmiParameter->waitForAck = false; // modif ra, otherwise i can't use HMI for a machine allready running
            hmiParameter->lineNumber = 0; // modif ra, otherwise i can't use HMI for a machine allready running
            hmiParameter->timeoutTimerID = 0; // modif ra, otherwise i can't use HMI for a machine allready running
            client_len = sizeof(hmiParameter->hmiAddress);
            getpeername(hmiParameter->client, (struct sockaddr *)&hmiParameter->hmiAddress, (socklen_t *)&hmiParameter->hmiAddress);
            diag_printf("ReceiverMain: connection from %s:%d \n", inet_ntoa(hmiParameter->hmiAddress.sin_addr), ntohs(hmiParameter->
                                                                                                                                                                                hmiAddress.sin_port));
        }
    }
}
:
:
}

[-- Attachment #3: Type: text/plain, Size: 148 bytes --]

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [ECOS] RE : [ECOS] I lose sockets
  2006-08-16 14:17 [ECOS] RE : [ECOS] I lose sockets Roger Arn 
@ 2006-08-16 14:28 ` Andrew Lunn
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Lunn @ 2006-08-16 14:28 UTC (permalink / raw)
  To: Roger Arn; +Cc: ecos-discuss

Ah, now i see your problem.

You have the basic TCP server structure wrong. You should create one
listen'ing socket and then keep it for ever, repeatadly doing accept()
on it as each client connects. ie you don't need a listening socket
per connection.

    Andrew

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-08-16 14:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-16 14:17 [ECOS] RE : [ECOS] I lose sockets Roger Arn 
2006-08-16 14:28 ` Andrew Lunn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).