Presentation is loading. Please wait.

Presentation is loading. Please wait.

SISTEMET E SHPERNDARE (DISTRIBUTED SYSTEMS)‏ TCP Sockets

Similar presentations


Presentation on theme: "SISTEMET E SHPERNDARE (DISTRIBUTED SYSTEMS)‏ TCP Sockets"— Presentation transcript:

1 SISTEMET E SHPERNDARE (DISTRIBUTED SYSTEMS)‏ TCP Sockets
Neki Frasheri Fakulteti Teknologjise Informacionit Universiteti Politeknik i Tiranes / 2

2 Koncepti i Socket Protokolli TCP/IP Socket = ( IP# , port# )‏ HOST_A
HOST_B INTERNET ap_1 ap_a ap_1 ap_b ap_1 ap_c ap_1 ap_d port# port# IP# IP# Socket = ( IP# , port# )‏

3 Parimi i Punes me Socket
Parimi klasik i komunikimit Open Write / Send Read / Receive Close Sistemi Klient ~ aktiv Server ~ pasiv Lidhja {Source<IP# , Port#> , Destination <IP# , Port#>}

4 Tipet e Socket Tipet e Socket Stream – protokolli TCP standard
Datagram – protokolli UDP i thjeshtezuar (?)‏ Raw – raw data transfer over IP Perdorimi i Sockets Unicast – destination IP i vetem – A.B.C.D Multicast – grup destinacion – 224.x.x.x Broadcast – direkt lokal – x.x.x.255 Loopback – loopback – 127.x.x.x

5 Skema Klient - Server Klienti Serveri shtresa OSI >4 aplikimi
socket klient shtresa OSI 4 transport TCP socket server shtresa OSI <4 network data link

6 Klienti TCP Algoritmi per klient TCP Gjendet Adresa IP e serverit
Numri i portes te aplikimit ne server Krijohet kopje e TCP socket e serverit Lidhet [connect] socket me serverin Serveri duhet te jete ne regjim degjimi Send / Receive me serverin nepermjet socket Mbyllet lidhja

7 Skema e Klientit ' KLIENTI shtresa OSI >4
aplikimi klient Kopja e server socket sherben si identifikator i kanalit te komunikimit kopje socket server shtresa OSI 4 transport TCP socket lokale shtresa OSI <4 network data link connect server

8 Skema e Klientit “ KLIENT Sock-Kli(IP,port|)‏ Server
Sock-Serv(IP,port)‏ Kopje Sock-Serv Kopje Sock-Kli Kopje Sock-Serv Kopje Sock-Serv

9 Serveri TCP Algoritmi per server TCP Gjendet Adresa IP e serverit
Numri i portes te aplikimit ne server Krijohet ne aplikim socket TCP e serverit Lidhet [bind] socket e serverit me protokollin TCP Kalohet ne pritje degjimi (listen)‏ Pranohet lidhja [connect] nga klienti Merret kopje e socket te klientit Send / Receive data me socket e klientit Mbyllet lidhja me klientin

10 Skema e Serverit Server shtresa OSI >4 aplikimi server socket
transport TCP kopje socket klient bind connect shtresa OSI <4 network data link klienti

11 Hapja e Sesionit aplikim aplikim Transport Transport connect accept po
Network Network Send/recv

12 Procedurat e Socket Klient
Krijohet socket socket() system call Lidhet socket me serverin connect() system call Send / receive data send() system call recv() system call Mbyllet socket close() system call

13 Procedurat e Socket Server
Krijohet socket socket() system call Lidhet socket me serverin bind() system call Kalohet ne regjim degjimi listen() system call Pranohet lidhja me klientin accept() system call Send / receive data Mbyllet socket

14 Klient – Server Minimal
Klienti Ekzekutohet <klient> <host> <port> Hap socket me serverin Kap mesazhin nga tastiera dhe e dergon Pret pergjigje nga serveri Fund Serveri Ekzekutohet <server> <port> Krijon socket te serverit Kalon ne gjendje “listen” Pranon mesah nga klienti dhe e afishon Dergon pergjigje klientit

15 Programi Minimal Klient 1
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error (char *msg) { perror(msg); exit(1); } int main (int argc, char *argv[ ]) { struct sockaddr_in serv_addr; struct hostent *server; int sockfd, portno, rc; char buffer[256]; if (argc<3) { printf("usage %s host port\n",argv[0]); exit(0); }

16 Programi Minimal Klient 2
/* krijohet kopje e socket te serverit */ sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (sockfd<0) error("ERROR opening socket"); /* inicializohet kopje e socket te serverit */ server = gethostbyname(argv[1]); portno = atoi(argv[2]); if (server==NULL) error("ERROR host not found\n"); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portno); bcopy( (char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );

17 Programi Minimal Klient 3
/* lidhet socket me serverin ~ socket lokale automatike */ if (connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0) error("ERROR while connecting"); else printf(“Server connected\n”); /* get & send message ne socket te serverit */ fgets(buffer,255,stdin); rc = send(sockfd,buffer,strlen(buffer),0); if (rc < 0) error("ERROR writing to socket"); /* receive & print message ne socket te serverit */ rc = recv(sockfd,buffer,255,0); if (rc < 0) error("ERROR reading from socket"); printf("%s\n",buffer); /* mbyllet socket */ close(sockfd); return 0;

18 Programi Minimal Server 1
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg){ perror(msg); exit(1);} int main(int argc, char *argv[]){ int sockfd, newsockfd, portno, clilen, rc; struct sockaddr_in serv_addr, cli_addr; char buffer[256]; if (argc<2) {printf("usage %s port\n",argv[0]); exit(0); }

19 Programi Minimal Server 2
/* krijohet socket e serverit */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); /* inicializohet socket e serverit */ portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); /* lidhet socket me protokollin TCP */ if (bind( sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))‏ < 0) error("ERROR on binding");

20 Programi Minimal Server 3
/* regjim degjimi ne socket te serverit */ listen(sockfd,5); /* pritet kerkesa e klientit & lidhet socket e serverit me kopjen e socket te klientit */ clilen = sizeof(cli_addr); newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR cannot accept");

21 Programi Minimal Server 4
/* receive & print message */ n = recv(newsockfd,buffer,255,0); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); /* send message */ n = send(newsockfd,"I got your message",18,0); if (n < 0) error("ERROR writing to socket"); /* mbyllen socket */ close(newsockfd); close(sockfd); return 0; }

22 Specifika te DEV_C++ 1 Skedaret e deklarimeve jane:
#include <stdio.h> #include <stdlib.h> #include <winsock.h> #include <sys/types.h> Lidhet libraria libws2_32.a ne strukturat e projektit ose perdoret deklarimi #pragma comment(lib, "ws2_32.lib")‏ Perdoret memcpy() versus bcopy()‏ ...

23 Specifika te DEV_C++ 2 Inicializohet WSA (WinSockApi) ne fillim te main{} WORD wVersionRequested; WSADATA wsaData; SOCKADDR_IN SockAddr; wVersionRequested = MAKEWORD (1,1); if(WSAStartup(wVersionRequested , &wsaData) != 0)‏ { printf("WSAStartup() Error! \n"); return 1;} Caktivizohet WSA ne fund te main{} WSACleanup();

24 Konsiderata Programet reale duhet te mundesojne perseritjen e veprimeve edhe ne rast gabimesh te perdoruesit. Programi duhet te abortoje vetem ne raste ekstreme Ne server mund te praktikohet leshimi i proceseve paralele per trajtimin e kerkesave, duke i lene procesit kryesor server vetem pritjen e kerkesave ...

25 Ushtrime


Download ppt "SISTEMET E SHPERNDARE (DISTRIBUTED SYSTEMS)‏ TCP Sockets"

Similar presentations


Ads by Google