MERANCANG Program Transfer File (PTF) PTFServer dan PTFClient I Made Astawa

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

1 Chapter 1 Foundation Computer Networks: A Systems Approach, 5e Larry L. Peterson and Bruce S. Davie Copyright © 2010, Elsevier Inc. All rights Reserved.
Socket Programming Computer Networks, Spring 2008 Your TAs.
UDP Sockets UDP: unreliable delivery, no connection DNS, NFS, SNMP.
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
$100 $400 $300$200$400 $200$100$100$400 $200$200$500 $500$300 $200$500 $100$300$100$300 $500$300$400$400$500.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
TCP-over-UDP Cheng-Han Lee (cl2804).
Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne) Data Communication: Introduction to Berkeley Sockets INF1060: Introduction to Operating.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne)
CS 4700 / CS 5700 Network Fundamentals
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
IPv6 Technologies and Advanced Services page 1 Porting applications to IPv6 OpenH323 and IPv6 support K. Stamos Computer Engineer, University of Patras.
Chapter 10 IPv4 and IPv6 Interoperability. contents Introduction IPv4 Client, IPv6 Server IPv6 Client, IPv4 Server IPv6 Address Testing Macros IPV6_ADDRFORM.
Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
CS252: Systems Programming Ninghui Li Based on slides by Prof. Gustavo Rodriguez-Rivera Topic 16: Socket Programming & Project 5.
1 Chapter 1 Foundation Computer Networks: A Systems Approach, 5e Larry L. Peterson and Bruce S. Davie Copyright © 2010, Elsevier Inc. All rights Reserved.
1 Pertemuan 9 Windows Socket ( WinSock ) Matakuliah: H0483 – Network Programming Tahun: 2008 Versi: 2.0.
1 Pertemuan 16 First & Second Conditionals Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 12 Binary Search Tree Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
1 Pertemuan 22 Radix Sort Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
1 Pertemuan 6 Elementary UDP Sockets Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.
1. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu :  Mendemonstrasikan penggunaan windows common control lanjutan dan propertinya (C3) 2.
1 Pertemuan 26 Object Relational Database Management System (Lanjutan) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
Application Layer Pertemuan 25 Matakuliah: H0484/Jaringan Komputer Tahun: 2007.
1 Pertemuan 19 Layer Network Matakuliah: H0174/Jaringan Komputer Tahun: 2006 Versi: 1/0.
1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
07 - Advertising Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 21 Audit Reporting Matakuliah:A0274/Pengelolaan Fungsi Audit Sistem Informasi Tahun: 2005 Versi: 1/1.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
11 - Innovation Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 20 Time & Condition Clauses with Future reference Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
Transport Layer Pertemuan 24 Matakuliah: H0484/Jaringan Komputer Tahun: 2007.
Introduction to Project 1 Web Client and Server Jan 2006.
1 Pertemuan #3 Clocks and Realtime Matakuliah: H0232/Sistem Waktu Nyata Tahun: 2005 Versi: 1/5.
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
13 - Organisation Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 8 The Object Definition Language (Lanjutan) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Sirak Kaewjamnong Computer Network Systems
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Socket Programming Lab 1 1CS Computer Networks.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.
CS 447 Networks and Data Communication Server-Process Organization IP address and SockAddr_In Data Structure Department of Computer Science Southern Illinois.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Pertemuan 20 The Business Views of the Technology Architecture
Table Pertemuan 10 Matakuliah : L0182 / Web & Animation Design
Pertemuan 13 Perencanaan Perdagangan Saham
Pertemuan 22 The Business Views of the Technology Architecture
Socket Programming (Cont.)
Pertemuan 7 I/O Multiplexing
Chapter4 Elementary TCP Socket
Network Programming with Sockets
Transport layer API: Socket Programming
Matakuliah : Web Design
TCP Sockets Programming
Chapter 06. UDP Server/Client.
Pertemuan 14 File Processing II
Socket Programming(1/2)
Presentation transcript:

MERANCANG Program Transfer File (PTF) PTFServer dan PTFClient I Made Astawa

2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan program aplikasi Client Server untuk mentransfer file

3 Outline Materi Merancang Flow Control Merancang Protokol Membuat Program Server Membuat Program Client Compilasi & Testing

4 Rancangan

5

6 #define SERVER_PORT_ID 6081 #define CLIENT_PORT_ID 6086 #define SERVER_HOST_ADDR " " #define MAXSIZE 512 #define ACK 2 #define NACK 3 #define REQUESTFILE 100 #define COMMANDNOTSUPPORTED 150 #define COMMANDSUPPORTED 160 #define BADFILENAME 200 #define FILENAMEOK 400 #define STARTTRANSFER 500 Implementasi Server

7 #include #define MY_PORT_ID 6081 #define MAXLINE 256 #define MAXSIZE 512 #define ACK 2 #define NACK 3 #define REQUESTFILE 100 #define COMMANDNOTSUPPORTED 150 #define COMMANDSUPPORTED 160 #define BADFILENAME 200 #define FILENAMEOK 400

8 Implementasi Server int writen(int sd,char *ptr,int size); int readn(int sd,char *ptr,int size); int len; main() { int sockid, newsd, pid, clilen; struct sockaddr_in my_addr, client_addr; printf("server: creating socket\n"); sockid = socket(AF_INET,SOCK_STREAM,0); if(sockid < 0){ printf("socket error: %d\n", errno); exit(0); }

9 Implementasi Server printf("binding my local socket\n"); bzero((char *) &my_addr,sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MY_PORT_ID); my_addr.sin_addr.s_addr=htons(INADDR_ANY); if(bind(sockid,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0){ printf("bind error:%d\n", errno); exit(0); } printf("server: starting listen \n"); if(listen(sockid,5) < 0){ printf("listen error :%d\n",errno); exit(0); }

10 while(1){ printf("server: starting accept\n"); if((newsd=accept(sockid,(struct sockaddr*) &client_addr,&clilen))<0){ printf("accept error:%d\n", errno); exit(0); } doftp(newsd); } Implementasi Server

11 Implementasi Server doftp(int newsd){ int i,fsize,fd,msg_ok,fail; int fail1,req,c,ack; int no_read,num_blks; int num_blks1,num_last_blk; int num_last_blk1,tmp; char fname[MAXLINE]; char out_buf[MAXSIZE]; FILE *fp; no_read = 0; num_blks = 0; num_last_blk = 0;

12 Implementasi Server req = 0; if((readn(newsd,(char *)&req,sizeof(req))) < 0){ printf("read error %d\n",errno);exit(0); } req = ntohs(req); printf("client request code is: %d\n",req); if(req!=REQUESTFILE){ printf("unsupported operation. goodbye\n"); msg_ok = COMMANDNOTSUPPORTED; msg_ok = htons(msg_ok); if((writen(newsd,(char *)&msg_ok,sizeof(msg_ok)))<0){ printf("write error :%d\n",errno); exit(0); } exit(0); }

13 Implementasi Server msg_ok = COMMANDSUPPORTED; msg_ok = htons(msg_ok); if((writen(newsd,(char *) &msg_ok,sizeof(msg_ok))) < 0){ printf("write error :%d\n",errno); exit(0); } fail = FILENAMEOK; if((len=read(newsd,fname,MAXLINE)) < 0) { printf("filename read error :%d\n",errno); fail = BADFILENAME ; } fname[len]='\0';

14 if((fp=fopen(fname,"r"))==NULL)fail= BADFILENAME; tmp = htons(fail); if((writen(newsd,(char *)&tmp,sizeof(tmp))) < 0){ printf("write error :%d\n",errno);exit(0); } if(fail==BADFILENAME){ printf("server cant open file\n"); close(newsd); exit(0); } printf("filename is %s\n",fname); req = 0; if((readn(newsd,(char *)&req,sizeof(req)))<0){ printf("read error :%d\n",errno);exit(0); } printf("start transfer,%d, received\n",ntohs(req)); Implementasi Server

15 Implementasi Server printf("starting transfer\n"); fsize = 0;ack = 0; while((c=getc(fp))!=EOF) fsize++; num_blks = fsize / MAXSIZE; num_blks1 = htons(num_blks); num_last_blk = fsize % MAXSIZE; num_last_blk1 = htons(num_last_blk); if((writen(newsd,(char *)&num_blks1, sizeof(num_blks1)))< 0){ printf("write error :%d\n",errno); exit(0); } printf("told client there are %d blocks\n", num_blks);

16 Implementasi Server if((readn(newsd,(char *)&ack,sizeof(ack))) < 0){ printf("ack read error :%d\n",errno); exit(0); } if(ntohs(ack) != ACK){ printf("ACK not received on file size\n"); exit(0); } if((writen(newsd,(char *)&num_last_blk1, sizeof(num_last_blk1))) < 0){ printf("write error :%d\n",errno);exit(0); } printf("told client %d bytes in last block\n", num_last_blk); if((readn(newsd,(char *)&ack,sizeof(ack))) < 0){ printf("ack read error :%d\n",errno);exit(0); }

17 Implementasi Server if(ntohs(ack) != ACK){ printf("ACK not received on file size\n"); exit(0); } rewind(fp);

18 Implementasi Server for(i= 0; i < num_blks; i ++){ no_read = fread(out_buf,sizeof(char),MAXSIZE,fp); if (no_read == 0){ printf("file read error\n"); exit(0); } if (no_read != MAXSIZE){ printf("file read error\n"); exit(0); } if((writen(newsd,out_buf,MAXSIZE)) < 0){ printf("error sending block:%d\n",errno);exit(0); } if((readn(newsd,(char *)&ack,sizeof(ack))) < 0){ printf("ack read error :%d\n",errno);exit(0); } if (ntohs(ack) != ACK){ printf("ACK not received for block %d\n",i); exit(0); } printf("%d\r",i); }

19 Implementasi Server if(num_last_blk > 0){ printf("%d\n",num_blks); no_read = fread(out_buf,sizeof(char),num_last_blk,fp); if (no_read == 0){ printf("server: file read error\n"); exit(0); } if (no_read != num_last_blk){ printf("file read error : no_read is less 2\n");exit(0); } if((writen(newsd,out_buf,num_last_blk)) < 0){ printf("server: file transfer error %d\n",errno);exit(0); } if((readn(newsd,(char *)&ack,sizeof(ack))) < 0){ printf("server: ack read error %d\n",errno);exit(0); } if(ntohs(ack) != ACK) { printf("server: ACK not received last block\n"); exit(0); } else printf("\n");

20 Implementasi Server /* FILE TRANSFER ENDS */ printf("FILE TRANSFER COMPLETE on socket %d\n",newsd); fclose(fp); close(newsd); }

21 #include #define SERVER_PORT_ID 6081 #define CLIENT_PORT_ID 6086 #define SERVER_HOST_ADDR " " #define MAXSIZE 512 Implementasi Client

22 #define ACK 2 #define NACK 3 #define REQUESTFILE 100 #define COMMANDNOTSUPPORTED 150 #define COMMANDSUPPORTED 160 #define BADFILENAME 200 #define FILENAMEOK 400 #define STARTTRANSFER 500 int readn(int sd,char *ptr,int size); int writen(int sd,char *ptr,int size); Implementasi Client

23 main(int argc,char *argv[]) { int sockid, newsockid, i, getfile; int ack, msg, msg_2, c, len; int no_writen,start_xfer, num_blks,num_last_blk; struct sockaddr_in my_addr, server_addr; FILE *fp; char in_buf[MAXSIZE]; if(argc != 2){ printf("error: usage : ftp filename\n"); exit(0); } Implementasi Client

24 no_writen = 0; num_blks = 0; num_last_blk = 0; len = strlen(argv[1]); printf("client: creating socket\n"); if((sockid=socket(AF_INET,SOCK_STREAM,0))< 0){ printf("client: socket error : %d\n", errno); exit(0); } Implementasi Client

25 printf("client: binding my local socket\n"); bzero((char *) &my_addr,sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons(CLIENT_PORT_ID); if(bind(sockid,(struct sockaddr *) &my_addr, sizeof(my_addr)) < 0){ printf("client: bind error :%d\n", errno); exit(0); } Implementasi Client

26 printf("client: starting connect\n"); bzero((char *)&server_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr=inet_addr(SERVER_HOST_ADDR); server_addr.sin_port = htons(SERVER_PORT_ID); if(connect(sockid,(struct sockaddr *) &server_addr, sizeof(server_addr)) < 0){ printf("client: connect error :%d\n", errno); exit(0); } Implementasi Client

27 getfile = htons(REQUESTFILE); printf("sending command request to ftp server\n"); if((writen(sockid,(char *)&getfile, sizeof(getfile))) < 0){ printf("write error:%d\n", errno); exit(0); } msg = 0; if((readn(sockid,(char *)&msg,sizeof(msg)))< 0){ printf("read error:%d\n", errno); exit(0); } msg = ntohs(msg); if(msg==COMMANDNOTSUPPORTED){ printf("server refused command\n"); exit(0); } else printf("server replied %d, command supported\n",msg); Implementasi Client

28 /* send file name to server */ printf("client: sending filename\n"); if((writen(sockid,argv[1],len))< 0){ printf("write error:%d\n", errno); xit(0); } msg_2 = 0; if((readn(sockid,(char *)&msg_2, sizeof(msg_2)))< 0){ printf("read error:%d\n", errno); exit(0); } msg_2 = ntohs(msg_2); if(msg_2 == BADFILENAME){ printf("server reported bad file name\n"); exit(0); } else printf("server replied filename OK\n"); Implementasi Client

29 printf("sending start transfer command\n"); start_xfer = STARTTRANSFER; start_xfer = htons(start_xfer); if((writen(sockid,(char *)&start_xfer,m sizeof(start_xfer)))< 0){ printf("write error:%d\n", errno); exit(0); } if((fp = fopen(argv[1],"w")) == NULL){ printf("local open file error\n");exit(0); } Implementasi Client

30 if((readn(sockid,(char *)&num_blks, sizeof(num_blks))) < 0){ printf("read error on nblocks :%d\n",errno); exit(0); } num_blks = ntohs(num_blks); printf("server responded: %d blocks in file\n",num_blks); ack = ACK; ack = htons(ack); if((writen(sockid,(char*)&ack,sizeof(ack)))<0){ printf("ack write error :%d\n",errno); exit(0); } Implementasi Client

31 if((readn(sockid,(char *)&num_last_blk, sizeof(num_last_blk))) < 0){ printf("read error:%d on nbytes\n",errno); exit(0); } num_last_blk = ntohs(num_last_blk); printf("server responded: %d bytes last blk\n", num_last_blk); if((writen(sockid,(char *)&ack,sizeof(ack)))<0){ printf("write error:%d\n",errno); exit(0); } Implementasi Client

32 printf("client: starting to get file contents\n"); for(i= 0; i < num_blks; i ++) { if((readn(sockid,in_buf,MAXSIZE)) < 0){ printf("block error read: %d\n",errno); exit(0); } no_writen=fwrite(in_buf,sizeof(char),MAXSIZE,fp); if(no_writen == 0){ printf("file write error\n");exit(0); } if (no_writen != MAXSIZE){ printf("write error: no_writen is less\n"); exit(0); } if((writen(sockid,(char *)&ack,sizeof(ack))) < 0){ printf("ack write error :%d\n",errno);exit(0); } printf(" %d...",i); } Implementasi Client

33 if(num_last_blk > 0){ printf("%d\n",num_blks); if((readn(sockid,in_buf,num_last_blk)) < 0){ printf("last block error read :%d\n",errno); exit(0); } no_writen=fwrite(in_buf,sizeof(char),num_last_blk,fp); if(no_writen == 0){ printf("last block file write err :%d\n",errno); exit(0); } if(no_writen != num_last_blk){ printf("file write error : no_writen is less 2\n"); exit(0); } if((writen(sockid,(char *)&ack,sizeof(ack))) < 0){ printf("ack write error:%d\n",errno); exit(0); } else printf("\n"); Implementasi Client

34 fclose(fp); printf("FILE TRANSFER COMPLETE\n"); close(sockid); } Implementasi Client

35 Fungsi readn() int readn(int sd,char *ptr,int size) { int no_left,no_read; no_left = size; while (no_left > 0){ no_read = read(sd,ptr,no_left); if(no_read <0) return(no_read); if (no_read == 0) break; no_left -= no_read; ptr += no_read; } return(size - no_left); }

36 Fungsi writen() int writen(int sd, char *ptr,int size) { int no_left, no_written; no_left = size; while(no_left > 0){ no_written=write(sd, ptr,no_left); if(no_written <=0)return(no_written); no_left -= no_written; ptr += no_written; } return(size - no_left); }

37 TUGAS Contoh kasus diatas, servernya tidak bisa melayani lebih dari satu client. Modifikasi program PTFServer diatas agar bisa melayani banyak client dengan menggunakan : 1.multi proses ( system call fork() ), satu proses melayani satu client 2.Multi threading ( satu thread melayani satu client ).