16 March, 2015

Lect-20 (Unix Signals and NetBIOS)

Unix Signal:
Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.

Default Actions:
Every signal has a default action associated with it. The default action for a signal is the action that a script or program performs when it receives a signal. Some of the possible default actions are:
·         Terminate the process.
·         Ignore the signal.
·         Dump core. This creates a file called core containing the memory image of the process when it     received the signal.
·         Stop the process.
·         Continue a stopped process.
 The following are some of the more common signals you might encounter and want to use in your programs:
Signal Name
Signal Number
Description
SIGHUP
1
Hang up detected on controlling terminal or death of controlling process
SIGINT
2
Issued if the user sends an interrupt signal (Ctrl + C).
SIGQUIT
3
Issued if the user sends a quit signal (Ctrl + D).
SIGFPE
8
Issued if an illegal mathematical operation is attempted
SIGKILL
9
If a process gets this signal it must quit immediately and will not perform any clean-up operations
SIGALRM
14
Alarm Clock signal (used for timers)
SIGTERM
15
Software termination signal (sent by kill by default).
Sending Signals:
There are several methods of delivering signals to a program or script. One of the most common is for a user to type CONTROL-C or the INTERRUPT key while a script is executing.
When you press the Ctrl+C key, a SIGINT is sent to the script and as per defined default action script terminates.
The other common method for delivering signals is to use the kill command whose syntax is as follows:
$ kill -signal pid
Here signal is either the number or name of the signal to deliver and pid is the process ID that the signal should be sent to. For Example:
$ kill -1 1001
Sends the HUP or hang-up signal to the program that is running with process ID 1001. To send a kill signal to the same process use the folloing command:
$ kill -9 1001
This would kill the process running with process ID 1001.

Trapping Signals:

When you press the Ctrl+C or Break key at your terminal during execution of a shell program, normally that program is immediately terminated, and your command prompt returned. This may not always be desirable. For instance, you may end up leaving a bunch of temporary files that won't get cleaned up.
Trapping these signals is quite easy, and the trap command has the following syntax:
$ trap commands signals
Here command can be any valid Unix command, or even a user-defined function, and signal can be a list of any number of signals you want to trap.
There are three common uses for trap in shell scripts:
     Clean up temporary file
     Ignore signals


NetBIOS
NetBIOS (Network Basic Input / Output System) is a program that allows applications on different computers to communicate within a local area network (LAN). It was created by IBM and used de facto industry standard. NetBIOS is used in Ethernet and Token Ring networks and, included as part of NetBIOS Extended User Interface (NetBEUI). It does not in itself support a routing mechanism so applications communicating on a wide area network (WAN) must use another "transport mechanism" (such as Transmission Control Protocol) rather than or in addition to NetBIOS.
NetBIOS frees the application from having to understand the details of the network, including error recovery (in session mode). A NetBIOS request is provided in the form of a Network Control Block (NCB) which, among other things, specifies a message location and the name of a destination.
NetBIOS provides the session and transport services described in the Open Systems Interconnection (OSI) model. However, it does not provide a standard frame or data format for transmission. A standard frame format is provided by NetBUI.
NetBIOS provides two communication modes: session or datagram. Session mode lets two computers establish a connection for a "conversation," allows larger messages to be handled, and provides error detection and recovery. Datagram mode is "connectionless" (each message is sent independently), messages must be smaller, and the application is responsible for error detection and recovery. Datagram mode also supports the broadcast of a message to every computer on the LAN.



Lect-19 (UDP socket programming)

//Program to convert lower-case character into upper-case character using UDP Socket.
/************* UDP SERVER CODE *******************/

//#include “stdio.h”
//#include “sys/socket.h”
//#include “netinet/in.h”
//#include “string.h”
//#include “stdlib.h”
int main()
{
  int udpSocket, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr, clientAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size, client_addr_size;
  int i;

  /*Create UDP socket*/
  udpSocket = socket(PF_INET, SOCK_DGRAM, 0);

  /*Configure settings in address struct*/
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(7891);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*Bind socket with address struct*/
  bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*Initialize size variable to be used later on*/
  addr_size = sizeof serverStorage;

  while(1){
/* Try to receive any incoming UDP datagram. Address and port of    requesting client will be stored on serverStorage variable */
    nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);

    /*Convert message received to uppercase*/
    for(i=0;i
      buffer[i] = toupper(buffer[i]);

    /*Send uppercase message back to client, using serverStorage as the address*/
    sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
  }

  return 0;
}



/************* UDP CLIENT CODE *******************/
 
//#include “stdio.h”
//#include “sys/socket.h”
//#include “netinet/in.h”
//#include “string.h”
//#include “stdlib.h”
int main(){
  int clientSocket, portNum, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;
 
  /*Create UDP socket*/
  clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
 
  /*Configure settings in address struct*/
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(7891);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  
 
  /*Initialize size variable to be used later on*/
  addr_size = sizeof serverAddr;
 
  while(1){
    printf("Type a sentence to send to server:\n");
    fgets(buffer,1024,stdin);
    printf("You typed: %s",buffer);
    nBytes = strlen(buffer) + 1;
    
    /*Send message to server*/
    sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
 
    /*Receive message from server*/
                nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
    printf("Received from server: %s\n",buffer);
 
  }
  return 0;
}


Compile & Run
Firstly compile and run server socket on Terminal1.
Secondly compile and run client socket on Terminal2.
Now type string on client socket on Terminal2.

Server will send back a string uppercase.