Hello,
I communicate between my card and my server due to interrupt function. When my client (card RABBIT BL4S100) use the button, I create a socket between my server and my client to receive the state of my client. My problem is : after my first “while”, I receive during the second reconnection : connection reset by peer.
To have a reconnection i used it :
if (button_event)
{
// See if button message is not currently displayed
if (digIn(3) !=0)
{
char buffer[2048];
int bytes_read;
longword destIP;
tcp_Socket socket;
int digIn();
//création de la socket
sock_init();
while (ifpending(IF_DEFAULT) == IF_COMING_UP) {
tcp_tick(NULL);
}
if( 0L == (destIP = resolve(DEST)) ) {
printf( "ERROR: Cannot resolve \"%s\" into an IP address
", DEST );
exit(2);
}
tcp_open(&socket,0,destIP,PORT,NULL);
printf("Waiting for connection...
");
while(!sock_established(&socket) && sock_bytesready(&socket)==-1) {
tcp_tick(NULL);
}
printf("Connection established, sending request...
");
/*
* When tcp_tick()-ing on a specific socket, we get non-zero return while
* it is active, and zero when it is closed (as used here).
*/
sock_write(&socket,"client actif
",30);
do {
bytes_read=sock_fastread(&socket,buffer,sizeof(buffer)-1);
if(bytes_read>0) {
buffer[bytes_read] = '\0';
/*
* By using the "%s" format, if there are "%" in the buffer, printf()
* won't try to interpret them!
*/
printf("%s",buffer);
}
}
while(!tcp_tick(&socket)); //here it's my problem. I used "!" to close my socket because the condition isn't fulfilled else.
sock_abort(&socket);
printf("
Connection closed...
");
}
I think i don’t close correctly my socket here because normally my socket will must closed due to my server but this doesn’t work :
My server is like it :
int main (int argc, char **argv) {
int socket_desc;
int new_socket;
int n, pid;
char buffer[256];
struct sockaddr_in server;
struct sockaddr_in client;
// creation du socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
perror("ERROR opening socket");
exit(1);
}
// initialisation des membres de l'objet server
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
// bind
if (bind(socket_desc, (struct sockaddr * )&server, sizeof(server)) < 0) {
perror("ERROR on binding");
exit(1);
}
printf("
bind done
");
// sleep mode en attendant un client
listen(socket_desc, 3);
printf("
waiting for incoming connections...
");
n = sizeof(struct sockaddr_in);
// boucle infinie qui accepte et fork les clients distant
while(1) {
new_socket = accept(socket_desc, (struct sockaddr *) &client, (socklen_t *) &n);
if (new_socket < 0) {
perror("ERROR on accept");
exit(1);
}
// affiche les infos du client
printf("Connection from %s:%d accepted
",
inet_ntoa(client.sin_addr), // conversion adr en un format Inet Std dot Ntation (str)
ntohs(client.sin_port)); // conversion Network Byte Order vers Host Byte Order (endianess)
// creation process enfant
pid = fork();
if (pid < 0) {
perror("ERROR on fork");
exit(1);
}
if (pid == 0) {
// processus du client
close(socket_desc);
childProcess(new_socket);
exit(0);
}
else {
close(new_socket);
}
}
return 0;
}
I don’t understand why my socket isn’t close after my answer of my client on the state. (I have tried many things like sock_close, sock_shutdown (but doesn’t work on dynamic C). But i can’t correctly close my client on my server to avoid this problem.