© 2012 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies
« NS2  Projects »

Home Lab Exercise Network Lab Exercise Programs Demonstration to generate SIGPIPE Error with Socket▼


Demonstration to generate SIGPIPE Error with Socket:

Algorithm Steps:

Server:

Step 1: start

Step 2: create a socket with address family AF_INET, type SOCK_STREAM and default protocol.

Step 3: Initialize the socket and set its attributes assign any port number as desired.

Step 4: Bind the server to the socket using bind() function.

Step 5: Establish the listen queue using the listen function.

Step 6: wait for client program on request, establish a connection using accept function.

Step 7: Read the message from the client using recv() function. display the

Step 8: Terminate the connection.

Client:

Step 1: Start.

Step 2: Create a socket with address family AF_INET type SOCK_STERAM and default protocol.

Step 3: Initialize the socket and set it attributes set the required port number.

Step 4: Connect to the server using connect() function to initialize the request.

Step 5: Send the message using send function.

Step 6: stop.

C Program To Generate SIGPIPE Error with Socket

//Server Program: #include #include #include #include #include #include #include /*---------------------------------------------------------------------*/ /*--- sig_pipe - SIGPIPE ---*/ /*---------------------------------------------------------------------*/ void sig_pipe(int signum) { printf("got it!\n"); signal(SIGPIPE, sig_pipe); } /*---------------------------------------------------------------------*/ /*--- main - allow client to connect and then terminate. ---*/ /*---------------------------------------------------------------------*/ main() { struct sockaddr_in addr; int sd, byte_count, addr_size; signal(SIGPIPE, sig_pipe); if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) { perror("Socket"); abort(); } addr.sin_family = AF_INET; addr.sin_port = htons(9999); addr.sin_addr.s_addr = INADDR_ANY; if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) { perror("Bind"); abort(); } if ( listen(sd, 20) != 0 ) { perror("Listen"); abort(); } while (1) { int client; char buffer[1024]; addr_size = sizeof(addr); client = accept(sd, (struct sockaddr*)&addr, &addr_size); printf("Connected: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); recv(client, buffer, 4, 0); close(client); } close(sd); } //Client Program: #include #include #include #include #include #include #include /*---------------------------------------------------------------------*/ /*--- sig_pipe - SIGPIPE ---*/ /*---------------------------------------------------------------------*/ void sig_pipe(int signum) { printf("got it!\n"); signal(SIGPIPE, sig_pipe); } /*---------------------------------------------------------------------*/ /*--- main - force a SIGPIPE error by connecting to an echo server ---*/ /*--- and sending data while the server just terminates the ---*/ /*--- connection. ---*/ /*---------------------------------------------------------------------*/ main() { struct sockaddr_in addr; int sd, byte_count, addr_size; signal(SIGPIPE, sig_pipe); if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) { perror("Socket"); abort(); } addr.sin_family = AF_INET; addr.sin_port = htons(9999); if ( inet_aton("127.0.0.1", &addr.sin_addr) == 0 ) { perror("127.0.0.1"); abort(); } if ( connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) { perror("Bind"); abort(); } if ( send(sd, "this is a test", 17, 0) < 0 ) perror("send"); sleep(1); if ( send(sd, "this is a test", 17, 0) < 0 ) perror("send"); close(sd); }

SAMPLE INPUT OUTPUT:

$gcc clientexample.c -o client.exe
$client.exe
send:Connection reset by peer

$gcc serevrexample.c -o server.exe
$server.exe
Connected: 127.0.0.1:2324

$gcc clientexample.c -o client.exe
$client.exe
send:Connection reset by peer

$gcc serevrexample.c -o server.exe
$server.exe
Connected: 127.0.0.1:2636

 
SLogix Student Projects

⇓ Student Projects ⇓
⇑ Student Projects ⇑
bottom