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.