0% found this document useful (0 votes)
88 views9 pages

Raport: Lucrarea de Laborator Nr. 6 Disciplina: Programarea de Sistem Și de Rețea

This document is a lab report for a networking and systems programming course. It describes a client-server program developed by the student to demonstrate socket-based inter-process communication. The program allows multiple clients to connect to a server, exchange text-based messages, and disconnect. While the server can handle multiple sequential client connections, it cannot receive packets from multiple clients simultaneously due to using blocking socket calls. The student concluded they learned how to initiate socket programming and implement the client-server paradigm for inter-process communication using sockets.

Uploaded by

Dutca Alexandru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views9 pages

Raport: Lucrarea de Laborator Nr. 6 Disciplina: Programarea de Sistem Și de Rețea

This document is a lab report for a networking and systems programming course. It describes a client-server program developed by the student to demonstrate socket-based inter-process communication. The program allows multiple clients to connect to a server, exchange text-based messages, and disconnect. While the server can handle multiple sequential client connections, it cannot receive packets from multiple clients simultaneously due to using blocking socket calls. The student concluded they learned how to initiate socket programming and implement the client-server paradigm for inter-process communication using sockets.

Uploaded by

Dutca Alexandru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Ministerul Educației al Republicii Moldova

Universitatea Tehnică a Moldovei


Facultatea Calculatoare, Informatica și Microelectronică

Raport
Lucrarea de laborator Nr. 6
Disciplina: Programarea de Sistem și de Rețea

A elaborat: st.gr. CR-182


Dutca Alexandru

A verificat: A. Godonoga

2021
Comentarii in cod

client.c
#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>
// Defenim eroare si inchidem programul
#define ERROR(s) {printf("%d-",errno); perror(s); return (-1);}
char buf[BUFSIZ];
int main (argc, argv)
int argc;
char *argv[];
{
int sock;
struct sockaddr_in sa;
struct hostent * host;
int n;
if (argc!=3) {
printf("Usage: %s hostname port\n",argv[0]);
exit (-1);
}
// Se verifica daca exita un soket pentru comunicare
if((sock=socket(AF_INET, SOCK_STREAM,
IPPROTO_TCP))<0)
ERROR("socket");
// Are loc 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.c

#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);} int
main (argc, argv)
int argc;
char *argv[];
{
struct sockaddr_in sa;
struct sockaddr_in caller;
int ssd;
int csd; int
length; int
retval; int
action;
char buf[BUFSIZ];
char* prompt[]={">>",
"Available commands: \n\tput - to send a text to server \thelp - to view this help\
n\tquit
- to close the connection\n>>", "Enter the text terminating it with '.' in a new li
ne.\n>>",
"Enter a command\n>>"};
if (argc != 2) {
fprintf (stdout, "usage: %s hostname\n", argv[0]);
exit (-1);
}
sa.sin_family= AF_INET; // IPv4 sa.sin_addr.s_addr
= INADDR_ANY; // adresa internet
sa.sin_port= htons((u_short) atoi(argv[1])); // port
// loop infinit
while (1) {
int action = 0;
printf ("Starting new connection:");
sleep(5);
// verificarea daca exista socket
if ((ssd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
ERROR ("socket");
// verificarea adresa si port pentru soket
if (bind(ssd, (struct sockaddr *)&sa, sizeof sa) < 0)
ERROR ("bind");
length = sizeof(sa);
// ascultam portul
listen (ssd, 5);
if ((csd = accept(ssd, (struct sockaddr *)&caller, &length)) < 0)
ERROR ("accept");
write(csd, prompt[0], strlen(prompt[0]));
// loop activ cit nu e introdus exit
while(action != 2) {
if((retval = read(csd, buf, sizeof(buf))) < 0)
ERROR("Reading stream message\n");
buf[retval]='\0';
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;
// scase-urile cu comanda introdusa
switch (action) {
case 0:
write(csd, prompt[1], strlen(prompt[1]));
action =- 1;
break;
case 1:
write(csd, prompt[2], strlen(prompt[2]));
action = 4;
break;
case 2:
printf("\nEnding connection ...\n");
break;
case 3:
write(csd, prompt[3], strlen(prompt[3]));
action =- 1;
break;
case 4 :
printf("%s", buf);
break;
default:
write(csd, prompt[3], strlen(prompt[3]));
}
}
// oprirea conexiunii
close(csd); close(ssd);
}
exit(0);
}

Comunicarea in retea

Starting new connection: laborator6.


PSR.
Sesiuni multiple:
Primirea a mai multor pachete de la alti clienti nu este posibila.

>>put
Enter the nexr terminating it with . in a new line.
>>1st client

Enter a command

>>quit

Starting new connection:1st client

Ending connection . . .
Starting new connection:2nd client
Concluzie:
In lucrarea de laborator numarul 6 am studiat,invatat initierea in programarea de socket-
uri,am realizat practic Paradigma Client-Server si Comunicarea intre procese folosinf
sockets,pe parcursul lucrarii am realizat compilarea si executarea programului,am efectuat
comunicarea si transmiterea prin intermediul ma multor clienti .

You might also like