210 likes | 335 Views
Practical Sockets and Threads Derek Weitzel. Windows Threads. Concurrent processing Windows Create Thread
E N D
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
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
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
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);
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);
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
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
Locking Mutex • Windows: • The CreateMutex function automatically locks the mutex to the calling thread. The char*
Sockets • Low-Level Network communication • Most every program that communicates on the internet, uses Sockets, though usually abstracted through api’s
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);
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
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
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
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
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
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
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
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
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
Resources • www.msdn.com – Microsoft Developer Network • http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html - pthread tutorial