130 likes | 313 Views
Homework Assignment #3. Multithread 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.
E N D
Homework Assignment #3 Multithread 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)) • 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 does */ } pthread_t thread; pthread_create(&thread,NULL,thread_func,(void *)message)
Flowchart_Server Start Setup your own port: #define MYPORT 9999 addr.sin_port=htons(MYPORT); Establish socket 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 Establish bind Establish listen Establish accept
Flowchart_Server (cont.) Header: #include <pthread.h> Define: int pthread_create(pthread_t* child, pthread_attr_t* attr, void*(*fn)(void*), void* arg) Thread create Read/Write file loop 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 Thread exit Socket close
Flowchart_Client Start Setup your own port: #define MYPORT 9999 addr.sin_port=htons(MYPORT); The same to the server side Establish socket Establish connect Read/Write file Socket close
Executable Command • Ex. • #myserver Socket create… Socket bind to 0.0.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 connect to 140.120.15.125:9999 getting file “test”…
Turn In • Source code • Executing result (snapshot)
Turn In (cont.) • Deadline • 23:59, Jun ,13 2006 • Ftp • IP:140.120.15.123 2222 • Username/Password: comm94/comm94 • Filename • HW3_ID.doc eg.HW3_79356001.doc • If you want to update • HW3_ID_new1.doc, HW3_ID_new2.doc …etc
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 Programming,碁峰