Download presentation
Presentation is loading. Please wait.
Published byGiles Dawson Modified over 8 years ago
1
Socket Program Training 10/29/2008
2
2 TCP Client Socket ( ) Connect ( ) send ( ) Close ( ) send ( ) Read ( ) Accept ( ) recv ( ) Listen ( ) Bind ( ) Socket ( ) recv ( ) Close ( ) Waiting for the requests from client Build a connection Data (request) Data (reply) Deal with the request TCP Server Notify the end of the file
3
3 FTP Server source code #include …… #define MAXDATA 1024 int main(void){ int sockfd; int recfd; int length; int filename, ndata, fdesc, fk; char buf[BUFSIZ]; char data[MAXDATA]; struct sockaddr_in myaddr; struct sockaddr_in client_addr;
4
4 FTP Server source code //Get a socket into TCP/IP if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){ perror("socket error"); //write error messages to standard error return 0;} // AF_INET IPv4, AF_INET6 IPv6 //SOCK_STREAM TCP, SOCK_DGRAM UDP //Set up address memset((char*)&myaddr,0,sizeof(myaddr)); myaddr.sin_family=AF_INET; myaddr.sin_addr.s_addr=htonl(INADDR_ANY); myaddr.sin_port=htons(1234);
5
5 FTP Server source code if(bind(sockfd,(struct sockaddr*)&myaddr,sizeof(myaddr))<0){ perror("bind failed"); return 0;} if(listen(sockfd,1)<0){ perror("listen error"); return 0;} //Loop continuously, waiting for connection requests and performing the service length=sizeof(client_addr); printf("Server start !\n");
6
6 FTP Server source code while(1){ if((recfd=accept(sockfd,(struct sockaddr*)&client_addr,&length))<0){ perror("accept error"); return 0;} if((fk=fork())<0){ perror("fork error"); exit(1);} if(fk==0){ if((filename=read(recfd,buf,BUFSIZ))<0){ perror("server read error"); return 0;}
7
7 FTP Server source code printf("Create socket #%d from %s : %d\n",recfd,inet_ntoa(client_addr.sin_addr),htons(client_addr.sin_p ort)); printf("receive: %s\n",&buf); //Reserve two parts for you (fdesc=open(buf,O_RDONLY) perror (ndata=read(fdesc,data,MAXDATA)
8
8 FTP Server source code //return to client if((write(recfd,buf,ndata))<0){ perror("server write error"); return 0;} memset(buf,0,sizeof(buf)); close(recfd);} close(sockfd); return 0;}
9
9 FTP Client source code int main (int argc, char *argv[]) //ex: file option1 option2 option3 //argc = 4, argv[0]="file“,argv[1]="option1“, argv[2]="option2“,argv[3]="option3" { …… int ndata, fdesc, k, fk, index; int nbytes, count; …… //Check for proper usage if(argc < 4){ printf("use : ftpc IP file_count file1 file2...\n"); return 0;}
10
10 FTP Client source code count=atoi(argv[2]); printf("%d files request!\n",count); index=3; for(k=0;k<count;k++){ if((fk=fork())<0){ perror("fork error!");} if(fk==0){ if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){ printf("socket error\n"); return 0;}
11
11 FTP Client source code …… //Reserve two parts for you write(sockfd,argv[ ],strlen(argv[ ]) perror fdesc=open(argv[ ],O_WRONLY|O_CREAT|O_APPEND); Index; perror
12
12 FTP Client source code for(;;){ if((nbytes=read(sockfd,&buf,BUFSIZ))<0){ perror("first read"); exit(1);} if(nbytes==0){ printf("file %s transmit complete ! \n",argv[ ]); close(fdesc); close(sockfd); exit(1); break;} else{ if(write(fdesc,&buf,nbytes)<0){ perror("write error!"); exit(1); ……
13
13 Compile gcc -o filename filename.c –# gcc -o server server.c –# gcc -o client client.c Execute the filename –#./filename –#./filename server_IP number_of_file file1 file2 file3
14
Tcpdump and Tcpstat
15
15 Tcpdump and Tcpstat Listen the interfaces (network cards) of the router and write into a dump file. –Tcpdump options –-i, -w Also listen the interfaces of the server and client.
16
16 Tcpdump and Tcpstat Gather statistics from the dump files. –Tcpstat options –-r, -f ‘ ‘, -o “ “ Filter out the three different files. –Tcpdump primitives –port, src net,
17
17 Tcpdump and Tcpstat Output formatting –%a: the average packet size –%b: the number of bits per second –%I: the number of IPv4 packets –%n the number of packets –%p: the number of packets per second
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.