Simulation of Aloha Protocol

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 )
c_shAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

c_shAuthor Commented:
Adjusted points from 200 to 250
0
jlevieCommented:
Are you talking about simulating an implementaion of "Slotted Aloha Protocol"?
0
c_shAuthor Commented:
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

meverestCommented:
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
c_shAuthor Commented:
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
elston_saCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
c_shAuthor Commented:
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
elston_saCommented:
u have to link to the library wsock32.lib when compiling
that is why u are getting the errors
0
duneramCommented:
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
c_shAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking

From novice to tech pro — start learning today.