1 / 21

Practical Sockets and Threads Derek Weitzel

Practical Sockets and Threads Derek Weitzel. Windows Threads. Concurrent processing Windows Create Thread

talib
Télécharger la présentation

Practical Sockets and Threads Derek Weitzel

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical Sockets and ThreadsDerek Weitzel

  2. Windows Threads • Concurrent processing • Windows Create Thread • HANDLE WINAPI CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId ); • Returns HANDLE to new thread, HANDLE is used in other functions to modify and control the thread • Usually just pass 0 for lpThreadAttributes, dwStackSize, dwCreationFlags, lpThreadId -> usually used the HANDLE

  3. Windows Threads • HANDLE WINAPI CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId ); • lpStartAddress is a void pointer to a function defined as: DWORD WINAPI ThreadProc( LPVOID lpParameter ); • lpParameter is another void pointer to data that you want passed to the Thread Function

  4. POSIX Threads (pthreads) • Same idea as Windows! • int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); • Must create a pthread_t variable to hold the thread pointer, used further to control thread • Thread attr can be 0, and I usually use 0 • Pointer to a function defined as:void* start_routine(void*) • arg is the void* to the arguments

  5. Exiting Threads • 2 ways: • Can exit normally, simply calling exit(int) or reach the end of the Thread Function • Can call special thread functions:Windows: VOID WINAPI ExitThread( DWORD dwExitCode );Pthreads: void pthread_exit(void *value_ptr);

  6. Force Stop Threads • Windows:BOOL WINAPI TerminateThread( HANDLE hThread, DWORD dwExitCode ); • Where hThread is the HANDLE returned by the CreateThread function. • Pthreads: • int pthread_cancel(pthread_t thread);

  7. Mutex • Used to prevent race conditions with variables • Windows: • HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCTSTR lpName ); where lpMutexAttributes is always 0bInitialOwner: TRUE if caller wants ownership, otherwise falselpName: char* to the name of the mutex, used to distinguish the mutexreturn: HANDLE to the Mutex

  8. Mutex • Pthread: • int pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); • Where mutex is the variable to be used for the mutex • Usually use 0 for attr

  9. Locking Mutex • Windows: • The CreateMutex function automatically locks the mutex to the calling thread. The char*

  10. Sockets • Low-Level Network communication • Most every program that communicates on the internet, uses Sockets, though usually abstracted through api’s

  11. Windows Sockets • Follows the Berkley Sockets model, with 1 exception • Must call:int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); • These paramaters are difficult to find, so I just use the code:WSADATA WsaDat; WSAStartup(MAKEWORD(2,2), &WsaDat);

  12. Berkley Sockets • Create the socket: • Int socket(int socket_family, int socket_type, int protocol); • Where socket_family is usually PF_INET(linux) or AF_INET(windows) socket_type can be: • SOCK_STREAM – TCP • SOCK_DGRAM - UDP • Protocol is usually 0, but can be IPPROTO_TCP or IPPROTO_UDP in windows

  13. TCP vs DGRAM • TCP: • Reliable information transfer • Guaranteed to be in correct order and uncorrupted • Guaranteed to be received by connected computer • Unclear where messages begin and end, stream like operation • UDP: • Not guaranteed that the information will reach connected computer • Not guaranteed to be in order, though guaranteed to be un-corrupted • Clear beginning and end

  14. Socket Functions • Bind: • int bind( SOCKET s, const struct sockaddr* name, int namelen ); • Binds to the specified ip (usually your own) and port • Usually used for server side, to Listen for connections

  15. Socket Functions • Listen • int listen( SOCKET s, int backlog ); • Socket s specifies socket created by socket function • Backlog is max length of queue of incoming connections • Used to listen for incoming connections, accept connections with accept function

  16. Socket Functions • Accept • Used mostly on server side communication • SOCKET accept( SOCKET s, struct sockaddr* addr, int* addrlen ); • Returns the connected socket • SOCKET s is the listening socket • Addr is the structure to hold the address information of the socket • Addrlen is the sizeof addr

  17. Socket Functions • Send • Used to send information • Send for TCP: • int send( SOCKET s, const char* buf,int len, int flags ); • Buf is character array of length len • Send for UDP: • int sendto( SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen ); • to is structure specifing the receiver of the packet

  18. Socket Functions • Receive for TCP • int recv( SOCKET s, char* buf,int len, int flags ); • Returns amount of bytes read in (a char is a byte in length, normally) • buf is a character array previously made of length len • Flags can be left as 0

  19. Socket Functions • Receive for UDP • int recvfrom( SOCKET s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen ); • From is structure to hold the sender’s information of length fromlen

  20. Socket Functions • Select • Used to poll the condition of sockets, useful to see status of multiple of socket • int select( int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout ); • Nfds is largest value of the sockets (they’re ints) • Fd_set provides macros, shown in examples

  21. Resources • www.msdn.com – Microsoft Developer Network • http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html - pthread tutorial

More Related