Documente Academic
Documente Profesional
Documente Cultură
RAPORT
Lucrarea de laborator Nr.6 la
Programare de sistem și de reţea
A efectuat:
A verificat:
Chișinău-2020
Client:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <errno.h>
char buf[BUFSIZ];
if (argc!=3) {
printf("Usage: %s hostname port\n",argv[0]); exit (-1);
}
// verificarea host-ul
if((host=gethostbyname(argv[1]))==(struct hostent*)NULL)
ERROR("gethostbyname");
// socket-ul preia adresa host-ului
memcpy((char*)&sa.sin_addr, (char*)host->h_addr, host->h_length); sa.sin_family=AF_INET;
sa.sin_port=htons((u_short)atoi(argv[2]));
// conetarea server-client
if(connect(sock, (struct sockaddr *)&sa, sizeof sa) < 0)
ERROR("connect");
while(strcmp(buf,"quit\n")!=0) {
while(!((buf[strlen(buf)-2]=='>') && (buf[strlen(buf)-1]=='>'))) {
if ((n=read(sock, buf, sizeof(buf))) < 0)
ERROR("read");
buf[n]=0;
printf("%s", buf);
}
if(strcmp(buf, "Enter the text terminating it with '.' in a new line.\n>>")==0)
while(buf[0]!='.') { fgets(buf, sizeof(buf), stdin); if (write(sock, buf,
strlen(buf))<0)
ERROR("write");
}
else {
fgets(buf, sizeof(buf), stdin); if
(write(sock, buf, strlen(buf))<0)
ERROR("write");
}
}
}
Server:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <errno.h>
#define ERROR(s) {fprintf(stderr,"%d-",errno); perror(s); return(-1);}
if (argc != 2) {
fprintf (stdout, "usage: %s hostname\n", argv[0]); exit (-1);
}
// loop infinit
while (1) int
{ action = 0;
printf ("Starting new connection:");
sleep(5);
length = sizeof(sa);
// ascultam portul
listen (ssd, 5);
if (strcmp(buf,"help\n") == 0)
action = 0;
if (strcmp(buf,"put\n") == 0)
action = 1;
if (strcmp(buf,"quit\n") == 0 || retval == 0)
action = 2; if (strcmp(buf,".\n") == 0)
action = 3;
Server: