LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2009, 02:11 AM   #1
eryn
Member
 
Registered: Jul 2009
Posts: 43

Rep: Reputation: 15
How to test ethernet connection continuously?


Hi,

I am facing a problem here.
My program is working. However, I would like to add in some features.
Let see my code first

Code:
if (returnresult<=0)
    {
	printf("Connecting....\n");
	printf("Address=%s , Port=%s\n", szAddress, szPort);
        conn_s=Connect (szPort, szAddress);

	if (conn_s>0)
        {
            returnresult=1;
            printf("Connected to conn_no=%d\n",conn_s);
	}
    }
else   //line no. 13
    {
		SendStatus ();	
    		
		result = strtok(buffer, delims);
		if (result == NULL)
			buffer2[0]='\0';
		else
			strcpy(buffer2, result);
			
		if (strlen(buffer2)>0)
        	{
			if (strcmp(result, "A")==0)
            		{
				printf("Command: %s\n", result); 
				map = strtok(NULL, delims);
				printf("Map Filename: %s\n", map);
				filename = map;
				printf("Map Filename: %s\n", filename);

                                x = strlen(path);

				while (filename[y]!='\0')
				{
					path[x++] = filename[y++]; 
				}
		
				/*get map and calculate*/
				if(CheckFile(path, &cROW,&cCOL)==1)
				{
					int ** a = alloc_array2d(int, cROW , cCOL );
    					init_array2d(a, int, cROW , cCOL);
					GetArray(path,a);

				        for (r=0; r<cROW; r++) //line no. 47
					{
	    				    for (c=0; c< cCOL; c++)
					    {
						switch (a[r][c])
						{
                                                      //operations.
                                                      case 0: break; 
                                                      case 1: break;
                                                      default: break; 
                                                }
                                            } 		
					} 
					
					free(a);
					
				}
				else
				{
					// send file error.
					printf("file verification error!\n");
				}
				return 0;
                        }
                   }
          }
When the ethernet network connected, it will go into the else loop at line no. 13. The system will send status and display the status at control panel screen. If at this point, the ethernet disconnected, it will go back to the if loop at line 1. The system will keep trying to connect to the network and keep loop the if loop until it is connected.

Question now is I need the system to test the connection too after going into the operation loop that is start from the line no. 47.
If the system went into the for (int c=0;c<cCOL;c++), it will need to do all the operations as indicated by cCOL times before it exit that loop and go into the previous loop.

I need the system to detect connection everytime an operation is going to happen or ended and if there is no connection, it will trying to connect. Before it is connected, it will not continue the operations.

I tried to put the function "Connect (szPort, szAddress)" into each loop. Yes, the operation stop everytime the network disconnected. However, it shows on control panel screen "New Connection...." everytime it execute the statement. In fact, the network never disconnected.

Is there any suggestion that can solve my problem?

I tried to look at the Connect function code, it is as below, I cannot figure out any statement that can stop the appearance of that "New Connection...". It may be hidden at the library source code, it is too hard for a beginner to modify the library code.

Code:
int Connect (char *szPort, char *szAddress)
{
    /*  Set the remote port  */
    int       port;                 /*  port number               */
    int       conn_s;                /*  connection socket         */
    struct    sockaddr_in servaddr;  /*  socket address structure  */
    char     *endptr;               /*  for strtol()              */

	
    port = strtol(szPort, &endptr, 0);
    if ( *endptr ) 
	{
	printf("ECHOCLNT: Invalid port supplied.\n");
	exit(EXIT_FAILURE);
    }
	
    if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) 
   	{
		fprintf(stderr, "ECHOCLNT: Error creating listening socket.\n");
		exit(EXIT_FAILURE); 
   	}

    /*  Set all bytes in socket address structure to
        zero, and fill in the relevant data members   */

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family      = AF_INET;
    servaddr.sin_port        = htons(port);

    if ( inet_aton(szAddress, &servaddr.sin_addr) <= 0 ) 
	{
		printf("ECHOCLNT: Invalid remote IP address.\n");
		exit(EXIT_FAILURE);
	}

	/*printf ("%d, %s, %d\n", conn_s, szAddress, port);*/
    
	if (connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr) ) < 0) 
	{
		printf("ECHOCLNT: Error calling connect()\n");
		printf ("%d\n", conn_s);
		/* exit(EXIT_FAILURE); */
		return -1;
	}
	return conn_s;
}
I hope someone can help me on this.

Thank you.

Last edited by eryn; 11-04-2009 at 03:37 AM. Reason: To clarify the problem
 
Old 11-04-2009, 03:38 AM   #2
eryn
Member
 
Registered: Jul 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Hi,

Is there anyone that can help solve this problem.
It's been troubling me for few weeks.

Thank you very much.
 
  


Reply

Tags
automatic, connection, ethernet, loop, network, reconnect



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial loop back test doesnt work raypen Linux - Hardware 5 01-07-2012 06:06 AM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 07:59 AM
Test ethernet connection TroelsSmit Linux - Networking 2 02-09-2006 02:10 AM
Perl Net::Pcap install hang at loop test Bluesuperman Programming 1 08-17-2005 10:39 AM
Automated Network Connection Test assyrian47 Linux - Networking 1 06-16-2004 09:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration