Professional Documents
Culture Documents
Raport: Ministerul Educației Al Republicii Moldova Universitatea Tehnică A Moldovei
Raport: Ministerul Educației Al Republicii Moldova Universitatea Tehnică A Moldovei
RAPORT
Lucrarea de laborator Nr.7 la
Programare de sistem și de reţea
A efectuat:
A verificat:
Chișinău 2020
Scopul lucrrii:
Implimentarea unui server web simplificat care funcționează doar cu pagini web statice.
Exercitiu 1:
Cod Client:
#include <stdlib.h>
#include<stdio.h> //printf
#include<string.h> //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr
//Create socket
sock = socket(AF_INET , SOCK_STREAM ,
0); if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 4000 );
puts("Connected\n");
// close(sock);
return 0;
}
Cod Server:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM ,
0); if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
Exercitiu 2:
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
if(strcmp(charPtr[0],"GET")!=0) return(4);
if(strcmp(charPtr[2]+1,"HTTP/1.1")!=0) return(5);
strncpy(string,charPtr[1]+2,stringSize);//Se decaleasa sirul cu 2 octeti: primul octet este '\0', al
doilea decalaj permite sa retragem "/"
//Daca stringSize nu este suficient de mare, sirul nu contine '\0'. Pentru a verifica et
suficient sa testam string[stringSize-1] care
// trebuie sa fie = '\0' deoarece strncpy unple sirul cu '\0' cand exista loc.
if(string[stringSize-1]!='\0'){
fprintf(stderr,"Erreur parseRequest(): lungimea sirului string nu este suficienta
(stringSize=%d)\n",stringSize);
exit(3);
}
//DEBUG
if( *(charPtr[1]+2) == '\0') fprintf(stderr,"DEBUG-SERVEUR: nomele fisieruui est vid -
\nDEBUG-SERVEUR: - se asociaza fisierul implicit index.html\n");
else fprintf(stderr,"DEBUG-SERVEUR: numele fisierului cerut este %s\n",string); if(
return(0);
}
if (stat ==0)
{
}
else{
while (fgets(buffer2,1000,source) != EOF)
{
puts(buffer2);
write(new_socket , buffer2, strlen(buffer2));
}
}
fclose(source);
}
else
{
write(new_socket , msg, strlen(msg));
}
}
close(new_socket);
return 0; }
Exercitiu 3:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define PORT_NO 15050
#define NET_BUF_SIZE
32 #define cipherKey 'S'
#define sendrecvflag 0
#define nofile "File Not Found!"
// function to encrypt
char Cipher(char ch)
{
return ch ^ cipherKey;
}
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
// bind()
if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
printf("\nSuccessfully binded!\n"); else
printf("\nBinding Failed!\n");
while (1) {
printf("\nWaiting for file name...\n");
while (1) {
// send
sendto(sockfd, net_buf,
NET_BUF_SIZE, sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
clearBuf(net_buf);
}
printf("~~~~~~~~~~~~~~~~~~\n"); if (fp !=
NULL)
fclose(fp);
}
return 0;
}
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define IP_ADDRESS "127.0.0.1" // localhost
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
// socket()
sockfd = socket(AF_INET,
SOCK_DGRAM, IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
while (1) {
printf("\nPlease enter file name to receive:\n");
scanf("%s", net_buf); sendto(sockfd, net_buf,
NET_BUF_SIZE,
sendrecvflag, (struct sockaddr*)&addr_con,
addrlen);
printf("\n---------Data Received-----------");
while (1) { //
receive
clearBuf(net_buf);
nBytes = recvfrom(sockfd, net_buf,
NET_BUF_SIZE, sendrecvflag, (struct
sockaddr*)&addr_con, &addrlen);
printf("\n%i", nBytes);
printf("\n%s", net_buf); //
process
if (recvFile(net_buf, NET_BUF_SIZE))
{ break;
}
}
printf("\n--------------------------------\n");
}
return 0; }
Exercitiu 4
Cod Client:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 512
#define PORT 9930
if(argc != 2)
{
printf("Usage : %s <Server-IP>\n",argv[0]);
exit(0);
}
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; serv_addr.sin_port
= htons(PORT);
if (inet_aton(argv[1], &serv_addr.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
while(1)
{
printf("\nEnter data: ");
scanf("%[^\n]",buf);
getchar();
if(strcmp(buf,"exit") == 0)
exit(0);
close(sockfd);
return 0; }
Cod Server:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 512
#define PORT 9930
int main(void)
{
struct sockaddr_in my_addr, cli_addr;
int sockfd, i;
socklen_t slen=sizeof(cli_addr);
char buf[BUFLEN];
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET; my_addr.sin_port
= htons(PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
while(1)
{
close(sockfd);
return 0; }
Concluzie: