Homework Assignment 3 Multithread Application Homework Assignment 3

  • Slides: 13
Download presentation
Homework Assignment #3 Multithread Application

Homework Assignment #3 Multithread Application

Homework Assignment #3: Multithread Application • In this work, you should make a application

Homework Assignment #3: Multithread Application • In this work, you should make a application similar to FTP server/client, and it can be accept several user requests at the same time. • Use the pthread library for multithreading

About pthread • Functions: – pthread_create( pthread_t* child, pthread_attr_t* attr, void*(*fn)(void*), void* arg)) •

About pthread • Functions: – pthread_create( pthread_t* child, pthread_attr_t* attr, void*(*fn)(void*), void* arg)) • Establish thread – pthread_exit(…) • Exit thread – pthread_join(…) • Waiting thread to complete

About pthread(cont. ) • Ex: Void *thread_func(void *arg) { /* do whatever a child

About pthread(cont. ) • Ex: Void *thread_func(void *arg) { /* do whatever a child does */ } pthread_t thread; pthread_create(&thread, NULL, thread_func, (void *)message)

Flowchart_Server Start Establish socket Establish bind Establish listen Establish accept Setup your own port:

Flowchart_Server Start Establish socket Establish bind Establish listen Establish accept Setup your own port: #define MYPORT 9999 addr. sin_port=htons(MYPORT); accept function will return a new socket description Ex. int clientsd; clientsd=accept (…. . , …. ); You can pass the clientsd to pthread_create() And then In the thread, you can operator read/write to this socket

Flowchart_Server (cont. ) Thread create loop Read/Write file Thread exit Socket close Header: #include

Flowchart_Server (cont. ) Thread create loop Read/Write file Thread exit Socket close Header: #include <pthread. h> Define: int pthread_create(pthread_t* child, pthread_attr_t* attr, void*(*fn)(void*), void* arg) fopen() / fcolse() : open/close file fread() / fwrite() : read/write file from/to buffer read() / write() : read/write buffer from/to socket feof() : check file end

Flowchart_Client Start Establish socket Setup your own port: #define MYPORT 9999 addr. sin_port=htons(MYPORT); The

Flowchart_Client Start Establish socket Setup your own port: #define MYPORT 9999 addr. sin_port=htons(MYPORT); The same to the server side Establish connect Read/Write file Socket close

Executable Command • Ex. – #myserver Socket create… Socket bind to 0. 0: 9999

Executable Command • Ex. – #myserver Socket create… Socket bind to 0. 0: 9999 Listening on server socket… Ready to accept incoming connection… 140. 120. 15. 125: 32708 accepted 140. 120. 15. 125 getting file “test”… -----------------------Ready to accept incoming connection… 140. 120. 15. 124: 32741 accepted 140. 120. 15. 124 getting file “test”… ------------------------

Executable Command(cont. ) • Ex. – #myclient 140. 120. 15. 125 Socket create… Socket

Executable Command(cont. ) • Ex. – #myclient 140. 120. 15. 125 Socket create… Socket connect to 140. 120. 15. 125: 9999 getting file “test”…

Turn In 1. Source code 2. Executing result (snapshot)

Turn In 1. Source code 2. Executing result (snapshot)

Turn In (cont. ) • Deadline – 23: 59, Jun , 13 2006 •

Turn In (cont. ) • Deadline – 23: 59, Jun , 13 2006 • Ftp – IP: 140. 120. 15. 123 2222 – Username/Password: comm 94/comm 94 • Filename – HW 3_ID. doc eg. HW 3_79356001. doc • If you want to update – HW 3_ID_new 1. doc, HW 3_ID_new 2. doc …etc

Turn In (cont. ) • No late work is acceptable – You get zero

Turn In (cont. ) • No late work is acceptable – You get zero if you miss the due day • No cheat work is acceptable – You get zero if you copy other people’s version

Reference • Linux C/C++ 網路程式設計, 金禾 • Linux C 函式庫參考手冊, 旗標 • Linux Socket

Reference • Linux C/C++ 網路程式設計, 金禾 • Linux C 函式庫參考手冊, 旗標 • Linux Socket Programming, 碁峰