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

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
CS 4700 / CS 5700 Network Fundamentals
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
ISP – 9 th Recitation Socket programming – Client side.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
UNIX Socket Programming CS 6378
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Remote Shell CS230 Project #4 Assigned : Due date :
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
Socket Programming Lab 1 1CS Computer Networks.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Socket programming Péter Verhás August 2002
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
CS 1652 Jack Lange University of Pittsburgh
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
Socket Programming in C
Review: TCP Client-Server Interaction
Socket Programming in C
Chapter 05. Multithread.
TCP/IP Socket Programming in C
Chapter 06. UDP Server/Client.
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Practical Sockets and Threads Derek Weitzel

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

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

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

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);

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);

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

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

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*

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

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);

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

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

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

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

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

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

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

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

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

Resources – Microsoft Developer Network – Microsoft Developer Network /LinuxTutorialPosixThreads.html - pthread tutorial /LinuxTutorialPosixThreads.html - pthread tutorial /LinuxTutorialPosixThreads.html /LinuxTutorialPosixThreads.html