Download presentation
Presentation is loading. Please wait.
Published byOswald Mills Modified over 9 years ago
1
Practical Sockets and Threads Derek Weitzel
2
Windows Threads Concurrent processing Concurrent processing Windows Create Thread Windows Create Thread HANDLE WINAPI CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );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 threadReturns 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 HANDLEUsually 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 ); 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 ); 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 lpParameter is another void pointer to data that you want passed to the Thread Function
4
POSIX Threads (pthreads) Same idea as Windows! Same idea as Windows! int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); 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 threadMust create a pthread_t variable to hold the thread pointer, used further to control thread Thread attr can be 0, and I usually use 0Thread attr can be 0, and I usually use 0 Pointer to a function defined as: void* start_routine(void*)Pointer to a function defined as: void* start_routine(void*) arg is the void* to the argumentsarg is the void* to the arguments
5
Exiting Threads 2 ways: 2 ways: Can exit normally, simply calling exit(int) or reach the end of the Thread FunctionCan 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);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 ); Windows: BOOL WINAPI TerminateThread( HANDLE hThread, DWORD dwExitCode ); Where hThread is the HANDLE returned by the CreateThread function.Where hThread is the HANDLE returned by the CreateThread function. Pthreads: Pthreads: int pthread_cancel(pthread_t thread);int pthread_cancel(pthread_t thread);
7
Mutex Used to prevent race conditions with variables Used to prevent race conditions with variables Windows: Windows: HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCTSTR lpName ); where lpMutexAttributes is always 0 bInitialOwner: TRUE if caller wants ownership, otherwise false lpName: char* to the name of the mutex, used to distinguish the mutex return: HANDLE to the MutexHANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCTSTR lpName ); where lpMutexAttributes is always 0 bInitialOwner: TRUE if caller wants ownership, otherwise false lpName: char* to the name of the mutex, used to distinguish the mutex return: HANDLE to the Mutex
8
Mutex Pthread: Pthread: int pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);int pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); Where mutex is the variable to be used for the mutexWhere mutex is the variable to be used for the mutex Usually use 0 for attrUsually use 0 for attr
9
Locking Mutex Windows: Windows: The CreateMutex function automatically locks the mutex to the calling thread. The char*The CreateMutex function automatically locks the mutex to the calling thread. The char*
10
Sockets Low-Level Network communication Low-Level Network communication Most every program that communicates on the internet, uses Sockets, though usually abstracted through api’s 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 Follows the Berkley Sockets model, with 1 exception Must call: int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData );Must call: int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); These paramaters are difficult to find, so I just use the code: WSADATA WsaDat;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: Create the socket: Int socket( int socket_family, int socket_type, int protocol);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:Where socket_family is usually PF_INET(linux) or AF_INET(windows) socket_type can be: SOCK_STREAM – TCP SOCK_STREAM – TCP SOCK_DGRAM - UDP SOCK_DGRAM - UDP Protocol is usually 0, but can be IPPROTO_TCP or IPPROTO_UDP in windowsProtocol is usually 0, but can be IPPROTO_TCP or IPPROTO_UDP in windows
13
TCP vs DGRAM TCP: TCP: Reliable information transferReliable information transfer Guaranteed to be in correct order and uncorruptedGuaranteed to be in correct order and uncorrupted Guaranteed to be received by connected computerGuaranteed to be received by connected computer Unclear where messages begin and end, stream like operationUnclear where messages begin and end, stream like operation UDP: UDP: Not guaranteed that the information will reach connected computerNot guaranteed that the information will reach connected computer Not guaranteed to be in order, though guaranteed to be un-corruptedNot guaranteed to be in order, though guaranteed to be un-corrupted Clear beginning and endClear beginning and end
14
Socket Functions Bind: Bind: int bind( SOCKET s, const struct sockaddr* name, int namelen );int bind( SOCKET s, const struct sockaddr* name, int namelen ); Binds to the specified ip (usually your own) and port Binds to the specified ip (usually your own) and port Usually used for server side, to Listen for connections Usually used for server side, to Listen for connections
15
Socket Functions Listen Listen int listen( SOCKET s, int backlog );int listen( SOCKET s, int backlog ); Socket s specifies socket created by socket functionSocket s specifies socket created by socket function Backlog is max length of queue of incoming connectionsBacklog is max length of queue of incoming connections Used to listen for incoming connections, accept connections with accept functionUsed to listen for incoming connections, accept connections with accept function
16
Socket Functions Accept Accept Used mostly on server side communicationUsed mostly on server side communication SOCKET accept( SOCKET s, struct sockaddr* addr, int* addrlen );SOCKET accept( SOCKET s, struct sockaddr* addr, int* addrlen ); Returns the connected socketReturns the connected socket SOCKET s is the listening socketSOCKET s is the listening socket Addr is the structure to hold the address information of the socketAddr is the structure to hold the address information of the socket Addrlen is the sizeof addrAddrlen is the sizeof addr
17
Socket Functions Send Send Used to send informationUsed to send information Send for TCP:Send for TCP: int send( SOCKET s, const char* buf, int len, int flags ); int send( SOCKET s, const char* buf, int len, int flags ); Buf is character array of length len Buf is character array of length len Send for UDP:Send for UDP: int sendto( SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen ); 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 to is structure specifing the receiver of the packet
18
Socket Functions Receive for TCP Receive for TCP int recv( SOCKET s, char* buf, int len, int flags );int recv( SOCKET s, char* buf, int len, int flags ); Returns amount of bytes read in (a char is a byte in length, normally)Returns amount of bytes read in (a char is a byte in length, normally) buf is a character array previously made of length lenbuf is a character array previously made of length len Flags can be left as 0Flags can be left as 0
19
Socket Functions Receive for UDP Receive for UDP int recvfrom( SOCKET s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen );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 fromlenFrom is structure to hold the sender’s information of length fromlen
20
Socket Functions Select Select Used to poll the condition of sockets, useful to see status of multiple of socketUsed 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 );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)Nfds is largest value of the sockets (they’re ints) Fd_set provides macros, shown in examplesFd_set provides macros, shown in examples
21
Resources www.msdn.com – Microsoft Developer Network www.msdn.com – Microsoft Developer Network www.msdn.com http://www.yolinux.com/TUTORIALS /LinuxTutorialPosixThreads.html - pthread tutorial http://www.yolinux.com/TUTORIALS /LinuxTutorialPosixThreads.html - pthread tutorial http://www.yolinux.com/TUTORIALS /LinuxTutorialPosixThreads.html http://www.yolinux.com/TUTORIALS /LinuxTutorialPosixThreads.html
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.