Solved

Simulation of Aloha Protocol

Posted on 2000-04-19
10
2,421 Views
Last Modified: 2010-05-18
I need a program written in C language which can show the Simulation of Aloha Protocol including throughput and other details .
( Algorithm or Code in another language may also work )
0
Comment
Question by:c_sh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Author Comment

by:c_sh
ID: 2737591
Adjusted points from 200 to 250
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2739053
Are you talking about simulating an implementaion of "Slotted Aloha Protocol"?
0
 

Author Comment

by:c_sh
ID: 2742894
jlevie :
 Yes, you are right, I am taking about "Slotted Aloha Protocol" only. Is there any any code available with you or any other reference from where I can get a help solving this problem.
0
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
LVL 37

Expert Comment

by:meverest
ID: 2743372
how do you want to simulate and what variables do you want? (eg number of stations, length of time-slots, etc)

why do you want slotted aloha only and not straight aloha, csma, cd-csma, etc?

i am not really sure about exactly what you are trying to do here...

cheers.
0
 

Author Comment

by:c_sh
ID: 2754388
meverest :
   I want to simulate the protocol which can produce :

(a) Delay Vs Troughput Curve

(b) Throughput Vs Load Curve

The values of delay,throughput and load should be provided by the user and the program can produce a plotted graph on the user's screen.


0
 
LVL 1

Accepted Solution

by:
elston_sa earned 250 total points
ID: 2758306
Here's the code for simulating aloha protocol. Note that the packets terminate at the server.

u require TCPIP installed and link with wsock32.lib

have fun

ELston

// Aloha server

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <Windows.h>


BOOL CLOSESERVER; // flag to specify to close server
BOOL CONNACTIVE;  // flag to specify that the connection is active

#define SERVERPORT 5060  
#define BUFSIZE 1024
#define COLLISION -1      //collision flag sent to client incase of collision
#define SUCCESS 0            //success flag sent to client incase of collision


SOCKET gListen_Socket;  //socket to listen to incomming connections
SOCKET gClient_Socket;      //socket to establish conncection with client

int StartServer();  //start server
int CloseServer();  // close server

DWORD WINAPI ServerThread(LPVOID lpParameter/* thread data*/);
DWORD WINAPI RecieveThread(LPVOID lpParameter/* thread data*/);

DWORD RecieveThreadID,ServerThreadID, dwThrdParam = 1;

HANDLE hServerThread,hRecieveThread;
int serverdelay;
int main()
{
 
   // Check the return value for success.
      CLOSESERVER=0;
      StartServer();
      printf("Enter the network delay : ");
      scanf("%d",&serverdelay);
      fflush(stdin);
      while(1)
      {
            printf("press q to quit\n");
            if(getchar()=='q')
                  break;
      }
      CLOSESERVER=1;
      CloseServer();
      printf("Thank you!\n");
      return 0;
}

int StartServer()
{
       WSADATA wsaData;
       struct sockaddr_in local;
       SOCKET listen_socket;

      if(WSAStartup(0x202,&wsaData)!=0)
            {printf("\nError in Startup session.\n");WSACleanup();return -1;};

      local.sin_family=AF_INET;
      local.sin_addr.s_addr=INADDR_ANY;
      local.sin_port=htons(SERVERPORT);

      listen_socket=socket(AF_INET,SOCK_STREAM,0);
      if(listen_socket==INVALID_SOCKET)
            {printf("\nError in New a Socket.");WSACleanup();return -2;}

      if(bind(listen_socket,(struct sockaddr *)&local,sizeof(local))!=0)
            {printf("\n Error in Binding socket.");      WSACleanup();return -3;      };

      if(listen(listen_socket,5)!=0)
            {printf("\n Error in Listen."); WSACleanup(); return -4;}
      gListen_Socket=listen_socket;
   
      hServerThread = CreateThread(
        NULL,                        // no security attributes
        0,                           // use default stack size  
        ServerThread,                  // thread function
        NULL,                // argument to thread function
        0,                           // use default creation flags
        &ServerThreadID);                // returns the thread identifier

      return 1;
}





int CloseServer()
{
      if (gListen_Socket!=INVALID_SOCKET)
            closesocket(gListen_Socket);
      if (gClient_Socket!=INVALID_SOCKET)
            closesocket(gClient_Socket);
      WSACleanup();
      if (hServerThread != NULL)
         CloseHandle( hServerThread );
      if (hRecieveThread != NULL)
         CloseHandle( hRecieveThread );
      return 1;
}

DWORD WINAPI ServerThread( LPVOID lpParameter )
{
      struct sockaddr_in from;
      SOCKET msg_socket;
      int fromlen;
      int collision=COLLISION;
      CONNACTIVE=0;
      fromlen=sizeof(from);
      while (!CLOSESERVER)
      {
            msg_socket=accept(gListen_Socket,(struct sockaddr*)&from,&fromlen); //accept a client connection [Blocking]
            
            if( msg_socket==INVALID_SOCKET)
            {
                  printf( "\nError  in accept ");
                  continue;
            }
            else
            {
                  printf( "Client : %d.%d.%d.%d connected \n",\
                        from.sin_addr.S_un.S_un_b.s_b1,\
                        from.sin_addr.S_un.S_un_b.s_b2,\
                        from.sin_addr.S_un.S_un_b.s_b3,\
                        from.sin_addr.S_un.S_un_b.s_b4);  //this is giving me an error i dont know why
            }
            if (CONNACTIVE)
            {
                  //connection already active collision
                  send(gClient_Socket,&collision,sizeof(int),0);
                  send(msg_socket,&collision,sizeof(int),0);
                  closesocket(gClient_Socket);
                  closesocket(msg_socket);
                  if (hRecieveThread)
                        CloseHandle(hRecieveThread);
                  CONNACTIVE=0;
            }
            else
            {
                  //channel free create thread to recieve message body
                  CONNACTIVE=1;
                  gClient_Socket=msg_socket;
                  hRecieveThread = CreateThread(
                        NULL,                        // no security attributes
                        0,                           // use default stack size  
                        RecieveThread,                  // thread function
                        NULL/*&dwThrdParam*/,                // argument to thread function
                        0,                           // use default creation flags
                        &RecieveThreadID);            // returns the thread identifier
            }
            
      }

       return 0;
}

DWORD WINAPI RecieveThread(LPVOID lpParameter/* thread data*/)
{
      char Buffer[BUFSIZE];
      int retval;
      int collision=SUCCESS;
      SOCKET temp;
      temp=gClient_Socket;
      Sleep(serverdelay); //network delay introduced
      retval=recv(temp,Buffer,sizeof(Buffer),0);
      send(temp,&collision,sizeof(int),0);
      CONNACTIVE=0; //receive complete
      if(retval==SOCKET_ERROR)
      {
      //      printf("Error Recv\n");
            return -1;
      }
      if(retval==0)
      {
      //      printf("Client Close connection\n");
            return -1;
      }
      Buffer[retval]=0;
      printf("Recieved: %s\n",Buffer);
      return 1;
}





// Aloha Client

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <Windows.h>

//#define RAND_MAX 1000
#define SERVERPORT 5060
#define BUFSIZE 1024
#define SERVERADDR "172.16.1.44"
#define COLLISION -1
#define SUCCESS 0
#define MAXTRY  10

char Buffer[BUFSIZE];

char address[16]=SERVERADDR;
unsigned short port=SERVERPORT;
int SendMess();
int main()
{
      int resp=0;
      int ret=0;
      WSADATA wsaData;
      if(WSAStartup(0x202,&wsaData)!=0)
      {
            printf("\nError in Startup session.\n");
            WSACleanup();
            return -1;
      }

      do
      {
            printf("1) SET SERVER ADDRESS & PORT\n");
            printf("2) SEND MESSAGE\n");
            printf("3) EXIT\n");
            scanf("%d",&resp);
            fflush(stdin);
            switch (resp)
            {
            case 1:
                  printf("Address: ");
                  scanf("%s",address);
                  printf("Port : ");
                  scanf("%d",&port);      
                  break;
            case 2:
                  printf("Message : ");
                  gets(Buffer);
                  printf("Sending.........\n");
                  ret=SendMess();
                  if (ret==-1)
                        printf("Failed");
                  else
                        printf("Done");
                  getch();
                  break;
            case 3:
                  break;
            }
            system("cls");
      }
      while (resp!=3);
      WSACleanup();
      return 1;
}

int SendMess()
{
      int error;
      int maxtry,flag,maxtryrecieve;
      int waittime;
      int retval,Len;
      unsigned int addr;
      int socket_type ;
      struct sockaddr_in server;
      struct hostent *hp;
      SOCKET  conn_socket;

      socket_type = SOCK_STREAM;
      srand((unsigned)time( NULL ) );

      if (isalpha(address[0])) {   /* server address is a name */
            hp = gethostbyname(address);
      }
      else  { /* Convert nnn.nnn address to a usable one */
            addr = inet_addr(address);
            hp = gethostbyaddr((char *)&addr,4,AF_INET);
      }
      if (hp == NULL ) {
            fprintf(stderr,"Client: Cannot resolve address [%s]: Error %d\n",
                  address,WSAGetLastError());
            return -1;
      }

      //
      // Copy the resolved information into the sockaddr_in structure
      //
      memset(&server,0,sizeof(server));
      memcpy(&(server.sin_addr),hp->h_addr,hp->h_length);
      server.sin_family = hp->h_addrtype;
      server.sin_port = htons(port);

      maxtry=0;
      maxtryrecieve=0;
      retval=0;
      flag=COLLISION;
      while (flag==COLLISION && maxtry<MAXTRY)
      {
            conn_socket = socket(AF_INET,socket_type,0); /* Open a socket */
            if (conn_socket <0 )
            {
                  fprintf(stderr,"Client: Error Opening socket: Error %d\n",
                        WSAGetLastError());
                  return -1;
            }
            printf("Client connecting to: %s\n",hp->h_name);
            if (connect(conn_socket,(struct sockaddr*)&server,sizeof(server))== SOCKET_ERROR)
            {
                  fprintf(stderr,"connect() failed: %d\n",WSAGetLastError());
                  return -1;
            }
            Len=strlen(Buffer);
            send(conn_socket,Buffer,Len,0);
            retval=recv(conn_socket,&flag,sizeof(int),MSG_PEEK);
            if (maxtryrecieve==20)
            {
                  return -1;
            }
            if (flag==COLLISION )
            {
                  waittime=1+(int) (maxtry*300.0*rand()/(RAND_MAX+1.0));
                  printf("Collision, waiting %d milisec.......",waittime);
                  Sleep(waittime);
                  printf("resending......\n");
            }
            maxtry++;
      }
      error=WSAGetLastError();
      closesocket(conn_socket);
      return 1;
}
0
 

Author Comment

by:c_sh
ID: 2761825
elston_sa :
  I am heartly thankful to you for the program code.

I tried to compile the code for Aloha Client and Aloha Server, but following error occured :

--------------------Configuration: AlServer - Win32 Debug--------------------
Linking...
AlServer.obj : error LNK2001: unresolved external symbol __imp__listen@8
AlServer.obj : error LNK2001: unresolved external symbol __imp__bind@12
AlServer.obj : error LNK2001: unresolved external symbol __imp__socket@12
AlServer.obj : error LNK2001: unresolved external symbol __imp__htons@4
AlServer.obj : error LNK2001: unresolved external symbol __imp__WSACleanup@0
AlServer.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
AlServer.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
AlServer.obj : error LNK2001: unresolved external symbol __imp__send@16
AlServer.obj : error LNK2001: unresolved external symbol __imp__accept@12
AlServer.obj : error LNK2001: unresolved external symbol __imp__recv@16
Debug/AlServer.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.

AlServer.exe - 11 error(s), 0 warning(s)

Similar error occured while compiling Aloha Client

I really appreciate you for your programm code, can you help me in removing these linker errors !



0
 
LVL 1

Expert Comment

by:elston_sa
ID: 2761931
u have to link to the library wsock32.lib when compiling
that is why u are getting the errors
0
 
LVL 2

Expert Comment

by:duneram
ID: 2763686
Interesting topic.  We're developing a satellite communications system that uses this same algorithm.  I was just cruising by and saw the question.

Our algorithm does it for real, but it's proprietary so simulation is definitely the way to go.
0
 

Author Comment

by:c_sh
ID: 2765189
ELston :
   Thanks for your co-operation, I accept your answer.


duneram :
   Thanks for you too, for giving attention to the question.


c_sh (Chandan).    
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
WARNING:   If you follow the instructions here, you will wipe out your VTP and VLAN configurations.  Make sure you have backed up your switch!!! I recently had some issues with a few low-end Cisco routers (RV325) and I opened a case with Cisco TA…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question