SISTEMET E SHPERNDARE (DISTRIBUTED SYSTEMS)‏ TCP Sockets

Slides:



Advertisements
Similar presentations
ZSocket address structure in : struct sockaddr{ u_shortsa_family;/* address family: AF_xxx value */ charsa_data[14];/* up to 14 bytes of protocol- */ /*
Advertisements

Introduction to Information Security Networking. Transmission Control Protocol (aka TCP) Most widely used protocol A TCP Connection is based on 6 crucial.
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
CHAIMS CPAM in TCP/IP Cho Chan and Kun Jiang Nov. 30, 1998 CS 446.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Introduction to Information Security Networking. Transmission Control Protocol (aka TCP) Most widely used protocol A ‘reliable’ (but not secure!) protocol.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
ECE 4110 – Internetwork Programming Client-Server Model.
Computer Networks Lecture 1 Adrian Sergiu DARABANT.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Sirak Kaewjamnong Computer Network Systems
Winsock Programming Blocking and Asynchronous Sockets for Windows.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Socket programming case study NCKU Multimedia Networking Lab Pei Chuan Liu.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Introduction A Simple Daytime Client A Simple Daytime Server
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
OS view of networking – Sockets API (an exercise in planning for the future) David E. Culler CS162 – Operating Systems and Systems Programming Lecture.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Cs288 Intensive Programming in Linux
Cs288 Intensive Programming in Linux
Sockets and Beginning Network Programming
Socket programming in C
Socket programming Péter Verhás August 2002
Part 2 Socket Programming UDP.
CS 1652 Jack Lange University of Pittsburgh
Elementary UDP Sockets
Network Programming CSC- 341
Introduction to Information Security
Network and Sockets Today: Get started doing network programming
Socket Programming in C
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Socket Programming in C
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
UNIX Domain sockets The Linux Programming Interface (ch 57)
Network Programming CSC- 341
CSE454 Internet Technology. Asst. Prof. Dr
Internet and Intranet Protocols and Applications
Berkeley API Socket Programming
Berkeley API Socket Programming
Chapter 2: Application layer
TCP/IP Socket Programming in C
Elementary UDP Sockets connectionless, unreliable, datagram
Socket Programming(1/2)
Example (UDP Client) // This program sends UDP packets to the given address #include #include #include #include.
Internet Networking recitation #8
Berkeley API Socket Programming
Outline Communications in Distributed Systems Socket Programming
Socket programming in C
Chapter 2: Application layer
Presentation transcript:

SISTEMET E SHPERNDARE (DISTRIBUTED SYSTEMS)‏ TCP Sockets Neki Frasheri Fakulteti Teknologjise Informacionit Universiteti Politeknik i Tiranes 2008-2009 / 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# )‏

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#>}

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

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

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

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

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

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

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

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

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

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

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

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); }

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 );

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;

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); }

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");

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");

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; }

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()‏ ...

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();

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 ...

Ushtrime 