1.

s1_tcp.c

/* Modulul Server tcp/ip*/

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <stdio.h>

#include <errno.h>

#include <netdb.h>

main(int argc,char ** argv)

{

               unsigned short port;  /*portul client*/

               char buffer[32];

               struct sockaddr_in client,server; /*adresele client server*/

               struct hostent * srvinfo;

               int s,rc,namelen,ns;

               if(argc!=1)

               {

                              printf("Apel: %s port \n",argv[0]);

                              exit(1);

               }

 

               /*Preia numele calculatorului local*/

               if(gethostname(buffer,32))

                              perror("Eroare la preluarea numelui serverului");

                printf("nume server : %s \n",buffer);

 

                /*Preia informatiile despre configurarea hostului*/

                if (!(srvinfo = gethostbyname(buffer)))

                              perror("Eroare la prel. inform. despre server");

/*           port=(unsigned short) atoi(argv[1]);*/

     port=10000;

 

                               /* Creaza un socket*/

                 if((s=socket(AF_INET,SOCK_STREAM,0))<0)

                 {

                              perror("Eroare la creare socket");

                              exit(3);

                 }

 

                 /* Asociaza socketului o adresa de server*/

                 memset(&server,0,sizeof(server));

                 server.sin_family=AF_INET;

                 server.sin_port= htons(port);

                 server.sin_addr.s_addr=inet_addr("193.226.34.61");

          /*adresa internet locala*/

                 if(bind(s,&server,sizeof(server))<0)

                 {

                              perror("Eroare la obtinerea adresei");

                              exit(4);

                 }

 

                 /* Creaza coada pt cererile de conexiune.Aceasta va

                 putea prelua maximum 5 cereri de conexiune de la clienti,

                 restul fiind refuzate*/

          if(listen(s,5)!=0)

                               {

                                             perror("Eroare la obt cozii de cereri");

                                             exit(5);

                               }

 

                /*Creaza un descriptor de socket pt comunic. server-client*/

 

                namelen=sizeof(client);

                if((ns=accept(s,&client,&namelen))==-1)

                               {

                                             perror("Eroare la acceptarea conexiunii");

                                             exit(6);

                               }

 

                /*Receptionarea mesajului de la client*/

                if(recv(ns,buffer,32,0)==-1)

                               {

                                             perror("Eroare la receptie");

                                             exit(7);

                               }

 

                /* Afiseaza mesajul primit de la client*/

                printf("Mesaj receptionat de la client:%s\n",buffer);

                /*Transmite confirmarea clientului*/

                if(send(ns,buffer,strlen(buffer),0)<0)

                               {

                                             perror("Eroare la transmisie");

                                             exit(8);

                               }

         endhostent();/* inchide sesiunea client */

                close(ns);

                close (s);

                exit(0);

    }

//////////

c1_tcp.c

 

/* Modulul Client tcp/ip*/

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <stdio.h>

#include <errno.h>

#include <netdb.h>

main(int argc,char ** argv)

{

               unsigned short port;  /*portul client*/

               char buffer[32];

               struct sockaddr_in server; /*adresele client server*/

               struct hostent * hostnm;

               int s,rc;

               if(argc!=2)

               {

                              printf("Apel: %s hostname (portul implicit este 10.000) \n",argv[0]);

                              exit(1);

               }

 

               if((hostnm=(struct hostent *) gethostbyname(argv[1])) == NULL)

               {

                              perror("Eroare la preluarea numelui serverului");

                              exit(3);

                }

                port=10000;

                strcpy(buffer,"Mesaj transferat prin socket");

 

                server.sin_family=AF_INET;

                server.sin_port=htons(port);

                server.sin_addr.s_addr=INADDR_ANY;/*adresa internet locala*/

                if((s=socket(AF_INET,SOCK_STREAM,0))<0)

                {

                              perror("Eroare la creare socket");

                              exit(4);

                }

 

                if(connect(s,&server,sizeof(server))<0)

                {

                              perror("Eroare la obtinerea conexiunii");

                              exit(5);

                }

 

                /*Transmite confirmarea serverului*/

                if(send(s,buffer,strlen(buffer),0)<0)

                {

                              perror("Eroare la transmisie");

                              exit(6);

                }

                /*Confirmare de la server*/

                if(recv(s,buffer,32,0)<0)

                {

                              perror("Eroare la receptie");

                              exit(7);

                }

                close (s);

                exit(0);

    }

 

/////////////

2.

s2_tcp.c

 

/* Modulul Server tcp/ip*/

#include <netinet/in.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <stdio.h>

#include <errno.h>

#include <netdb.h>

main(int argc,char ** argv)

{

               unsigned short port;  /*portul client*/

               char buffer[32];

               struct sockaddr_in client,server; /*adresele client server*/

               struct hostent * srvinfo;

               int s,rc,namelen,ns;

               if(argc!=2)

               {

                              printf("Apel: %s port \n",argv[0]);

                              exit(1);

               }

 

               /*Preia numele calculatorului local*/

 

               if(gethostname(buffer,32))

                              perror("Eroare la preluarea numelui serverului");

                printf("nume server : %s \n",buffer);

 

                /*Preia informatiile despre configurarea hostului*/

 

                if (!(srvinfo = gethostbyname(buffer)))

                              perror("Eroare la prel. inform. despre server");

                port=(unsigned short) atoi(argv[1]);

 

                /* Creaza un socket*/

 

                if((s=socket(AF_INET,SOCK_STREAM,0))<0)

                {

                              perror("Eroare la creare socket");

                              exit(3);

                }

 

                /* Asociaza socketului o adresa de server*/

 

                memset(&server,0,sizeof(server));

                server.sin_family=AF_INET;

                server.sin_port=htons(port);

                server.sin_addr.s_addr=inet_addr("193.226.34.61");/*adresa internet locala*/

                if(bind(s,&server,sizeof(server))<0)

                {

                              perror("Eroare la obtinerea adresei");

                              exit(4);

                }

                /* Creaza coada pt cererile de conexiune.Aceasta va

                putea prelua maximum 5 cereri de conexiune de la clienti,

                restul fiind refuzate*/

                if(listen(s,5)!=0)

                {

                              perror("Eroare la obt cozii de cereri");

                              exit(5);

                }

                /*Creaza un descriptor de socket pt comunic. server-client*/

                namelen=sizeof(client);

                if((ns=accept(s,&client,&namelen))==-1)

                {

                              perror("Eroare la acceptarea conexiunii");

                              exit(6);

                }

                /*Receptionarea mesajului de la client*/

                if(recv(ns,buffer,32,0)==-1)

                {

                              perror("Eroare la receptie");

                              exit(7);

                }

                /* Afiseaza mesajul primit de la client*/

                printf("Mesaj receptionat de la client:%s\n",buffer);

                /*Transmite confirmarea clientului*/

                if(send(ns,buffer,strlen(buffer),0)<0)

                {

                              perror("Eroare la transmisie");

                              exit(8);

                }

                endhostent();/*Incheie sesiunea TCP/IP*/

                close(ns);

                close (s);

                exit(0);

    }

/////

c2_tcp.c

 

/* Modulul Client tcp/ip*/

#include <stdio.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <errno.h>

#include <netdb.h>

main(int argc,char ** argv)

{

               unsigned short port;  /*portul client*/

               char buffer[32];

               struct sockaddr_in server; /*adresele client server*/

 

               struct hostent * hostnm;

               int s,rc;

               if(argc!=3)

               {

                              printf("Apel: %s hostname port \n",argv[0]);

                              exit(1);

               }

 

               if(!(hostnm=(struct hostent *) gethostbyname(argv[1])))

               {

                              perror("Eroare la preluarea numelui serverului");

                              exit(3);

                }

                port=(unsigned short) atoi(argv[2]);

                strcpy(buffer,"Mesaj transferat prin socket");

 

                server.sin_family=AF_INET;

                server.sin_port=htons(port);

                server.sin_addr.s_addr=inet_addr("193.226.34.61");/*adresa internet locala*/

                if((s=socket(AF_INET,SOCK_STREAM,0))<0)

                {

                              perror("Eroare la creare socket");

                              exit(4);

                }

 

                if(connect(s,&server,sizeof(server))<0)

                {

                              perror("Eroare la obtinerea conexiunii");

                              exit(5);

                }

 

                /*Transmite confirmarea serverului*/

                if(send(s,buffer,strlen(buffer),0)<0)

                {

                              perror("Eroare la transmisie");

                              exit(6);

                }

                /*Confirmare de la server*/

                if(recv(s,buffer,32,0)<0)

                {

                              perror("Eroare la receptie");

                              exit(7);

                }

                close (s);

                exit(0);

    }

 

 

////

3.

s3_udp.c

/* modulul server udp */

#include <sys/types.h>

#include <sys/socket.h>

#include <stdio.h>

#include <errno.h>

#include <netdb.h>

#include <netinet/in.h>

 

main()

{

 char buffer[32];

 struct sockaddr_in client,server;

 int s,rc,namelen;

 

 if((s = socket(AF_INET,SOCK_DGRAM,0)) < 0)

  {

   perror("Eroare la creare socket !");

   exit(1);

  }

 printf("S-a creat socket-ul....\n");

 

 

 memset(&server,0,sizeof(server));

 server.sin_family = AF_INET;

 server.sin_port = htons(15200);

 server.sin_addr.s_addr = inet_addr("193.226.34.61");

 

 if(bind(s,&server,sizeof(server)) < 0)

  {

   perror("Eroare la obtinerea adresei");

   exit(2);

  }

 printf("S-a obtinut o adresa ....\n");

 

/* namelen = sizeof(server);

 if(getsockname(s, &server,&namelen))

  {

   perror("Eroare la determinarea portului !");

   exit(3);

  }

  printf("S-a determinat portul....\n");

 */

 

  namelen = sizeof(client);

  if (recvfrom ( s, buffer, 32, 0, &client, sizeof(client))< 0 )

    {

     perror("Eroare la receptie !");

     exit(4);

    }

  printf("S-a receptionat mesajul....\n");

 

  printf("Mesaj receptionat de la client : %s \n",buffer);

  printf("Parametrii: \n nume domeniu : %s \nport : %d \nadresa Internet : %s\n",(server.sin_family == AF_INET ?

 "AF_INET" : "AF_UNIX"),ntohs(server.sin_port),

inet_ntoa(server.sin_addr));

 

 endhostent();

 /*inchide sesiunea UDP/IP*/

 

 close(s);

 exit(0);

}

 

////

c3_udp.c

 

/* modulul client */

# include <sys/types.h>

# include <sys/socket.h>

# include <stdio.h>

# include <errno.h>

# include <netinet/in.h>

# include <netdb.h>

 

main(int argc,char **argv)

{

 unsigned short port;

 char buffer[32];

 struct sockaddr_in server;

 int s,rc;

 if(argc != 2)

  {

   printf("apel : %s serverport 15200 (implicit) \n",argv[0]);

   exit(1);

  }

 port=(unsigned short)atoi(argv[1]);

 strcpy(buffer,"s-a trimis mesaj prin socket");

 server.sin_family = AF_INET;

 server.sin_port = htons(port);

 server.sin_addr.s_addr = inet_addr("193.226.34.61");

 if((s = socket(AF_INET,SOCK_DGRAM,0)) < 0)

  {

   perror("Eroare la creare socket !");

   exit(2);

  }

 if(sendto(s,buffer,strlen(buffer)+1,0,&server,sizeof(server)) < 0 )

  {

   perror("Eroare la transmisie");

   exit(3);

  }

 close(s);

 exit(0);

}

 

 

////

 

4.

/*header.h */

 

#include<stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netdb.h>

#include <sys/socketvar.h>

#include <netinet/in.h>

#include <sys/ioctl.h>

#include <curses.h>

#include <errno.h>

///////

 

s4-udp.c

 

/*UDPSERVER.C */

 

#include "header.h"

main()

{

   char mesaj_de_la_client[50];

   struct sockaddr_in server;

    struct sockaddr_in client;

   int s;

 

   if ((s=socket(AF_INET,SOCK_DGRAM,0))<0)

                {perror("Eroare la creare socket"); exit(1);}

   memset(&server,0,sizeof(server));

   server.sin_family=AF_INET;

   server.sin_port=htons(15000);

   server.sin_addr.s_addr=INADDR_ANY;

   if(bind(s,&server,sizeof(server))<0)

                {perror("Eroare la alocare de adresa "); exit(2);}

   if(recvfrom(s,mesaj_de_la_client,50,0, &client,sizeof(client))<0)

                {perror("Eroare de receptie "); exit(3);}

   printf("Mesajul receptionat de la client : %s\n",mesaj_de_la_client);

endhostent();

close(s);

exit(4);

}

///

 

c4-udp.c

 

/*UDPCLIENT.C */

 

#include "header.h"

 

main()

 

{

char mesajul_clientului[50];

struct sockaddr_in server;

int s;

strcpy(mesajul_clientului,"Mesaj transmis cu protocol UDP ");

  server.sin_family = AF_INET;

  server.sin_port = htons(15000);

  server.sin_addr.s_addr = inet_addr("193.226.34.61");

  if((s=socket(AF_INET,SOCK_DGRAM,0))<0)

         {perror(" Eroare la creare socket ");

          exit(1);}

  if(sendto(s,mesajul_clientului,strlen(mesajul_clientului)+1,0,&server,sizeof(server))<0)

         {perror("Eroare la transmiterea mesajului clientului");

          exit(2);}

  close(s);

  exit(0);

}

 

 

////