330 likes | 476 Views
Department of ELECTRONIC AND INFORMATION ENGINEERING. 5. Window Socket by Dr Daniel Lun. EIE360 Integrated Project. Lecture 5 Windows Socket. References: 1. J.M. Hart, Windows System Programming , 4th Ed., Addison-Wesley, 2010, Ch.12. Department of
E N D
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun EIE360 Integrated Project Lecture 5 Windows Socket References: 1. J.M. Hart, Windows System Programming, 4th Ed., Addison-Wesley, 2010, Ch.12
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Architecture of the Interactive Virtual Aquarium System Computer A USB port Your program Kinect Sensor Device Network Computer B 3D Graphics System Your program 3D Graphics
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Motion Tracking Server • For easy access of the data generated by the Kinect sensor, a simple network server is installed that accepts clients’ connection thru Winsock Network backbone Motion Tracking Server Windows Socket Server IP: ???.???.???.??? Port: 8888
Process A Process B Department of ELECTRONIC AND INFORMATION ENGINEERING a socket 5. Window Socket by Dr Daniel Lun Socket API and Socket • The socket API is an Interprocess Communication (IPC) programming interface originally provided as part of the Berkeley UNIX OS • Ported to all modern operating systems, including Sun Solaris and Windows systems • It is a de facto standard for programming IPC, and is the basis of more sophisticated IPC interface such as remote procedure call and remote method invocation • A socket API provides a programming construct termed a socket • A process wishing to communicate with another process must instantiate such a construct • The two processes then issue operations provided by the API to send and receive data.
socket socket Process A Process B API runtime API runtime support support transport layer software transport layer software connection-oriented datagram socket Connection-oriented stream socket • A socket programming construct can make use of either the UDP or TCP protocol • Sockets that use UDP for transport are known as datagram sockets, while sockets that use TCP are termed stream sockets • Socket APIs can support both connectionless and connection-oriented communication at the application layer • For the Motion Tracking Server, it uses stream socket and works in connection-oriented mode, i.e. a connection has to be made before data can be sent or received
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Windows Sockets API • An extension of the Berkeley Sockets API used in the Windows environment • Porting of code already written for Berkeley Sockets • Windows stations easily integrated into TCP/IP networks • Winsock API is supported by a DLL (ws2_32.dll) that can be accessed by linking ws2_32.lib with your project • Include winsock2.h as well for the definition of some names
General Procedure for Setting Up a Server Using Winsock • Just follow the procedure below and fill in the required information: WSAStartup( ... ); //1. Initialize getaddrinfo( ... ); //2. Collect network info socket ( ... ); //3. Create a socket bind( ... ); //4. Bind the socket to IP addr listen ( ... ); //5. Listen to incoming request accept ( ... ); //6. If connect request recv, accept send ( ... ); / recv ( ... ); //7. Send or receive data : closesocket ( ... ); //8. Close the socket after using it WSACleanup ( ... ); //9. Free resource allocated
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Winsock Initialization • To initialize, a nonstandard Winsock-specific function WSAStartup() must be the first function to call • For example, WORD sockVersion; //Store the socket version WSADATA wsaData; //Store socket info sockVersion = ... ; //Input the version number int iResult = // Return non-zero if error WSAStartup(sockVersion, &wsaData);
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun WSAStartup() Parameters • sockVersion • Indicates the highest version of the WinSock DLL you need • Returns a non-zero value if the DLL cannot support the version you want • Can use a macro MAKEWORD to generate the number • &wsaDatapoints to a WSADATA structure that returns information on the configuration of the DLL • iResult should be equal to zero if successful sockVersion = MAKEWORD(2,2); //version 2.2
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun getaddrinfo() • getaddrinfo()provides protocol-independent translation from an ANSI host name to an addr struct addrinfo *result = NULL, hints; • ZeroMemory(&hints, sizeof(hints)); • hints.ai_family = AF_INET; • hints.ai_socktype = SOCK_STREAM; • hints.ai_protocol = IPPROTO_TCP; • hints.ai_flags = AI_PASSIVE; • iResult = getaddrinfo(NULL, portNo, &hints,&result); • if ( iResult != 0 ) • { WSACleanup(); • return false; • } Result kept here Port number for making the connection, e.g. 8888 Means the system will be free to use any registered IP addr
Department of ELECTRONIC AND INFORMATION ENGINEERING 1. Motion Tracking – Polhemus Liberty Latus by Dr Daniel Lun addrinfo parameters • ai_family = AF_INET denotes the address family. designates the Internet protocol (IP) • ai_socktype = SOCK_STREAM specifies connection-oriented (for datagram communications, use SOCK_DGRAM) • ai_protocol = IPPROTO_TCP specifies transport layer protocol is TCP • ai_flag = AI_PASSIVE indicates the caller intends to use the returned socket address structure in a call to the bind function
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Create a Socket • Call socket()to create (or open) a socket Assume mListenSocket is a member variable of type SOCKET in your class to store the created socket mListenSocket = socket( result->ai_family, result->ai_socktype, result->ai_protocol); if (mListenSocket == INVALID_SOCKET) { freeaddrinfo(result); WSACleanup(); return false; } If create failed, free the memory for result and cleanup everything
5. Window Socket by Dr Daniel Lun Bind to the Socket • The bind() function associates a local address with a socket. For example, iResult = bind( mListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { freeaddrinfo(result); closesocket(mListenSocket); WSACleanup(); return false; } freeaddrinfo(result); result is created in getaddrinfo() mListenSocket is created in socket() result is not needed any more, so free it here Department of ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun Listen and Accept • The listen() function places a socket in a state in which it is listening for an incoming connection • Client connection requests will be queued iResult = listen(mListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { closesocket(mListenSocket); WSACleanup(); return false; } It is an integer that indicates the maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket will set the backlog to a maximum reasonable value. Department of ELECTRONIC AND INFORMATION ENGINEERING
Listen and Accept (cont) • The accept() function permits an incoming connection attempt on a socket • It extracts the first connection on the queue of pending connections on the created socket • However, if no pending connections are present on the queue, the accept() function can block the caller until a connection is present SOCKET mClientSocket = accept (mListenSocket, NULL, NULL); //return a new socket with connection
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Send or Receive Data • After a connection is established, the server can send or receive data to or from the client • Partner stations exchange data using send()and recv() • send() and recv() have identical arguments: int send ( int recv ( SOCKET s, SOCKET s, LPSTR lpBuffer, LPSTR lpBuffer, int nBufferLen, int nBufferLen, int nFlags); int nFlags);
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun send / recv Parameters • lpBuffer • send: the buffer that keeps the string to be sent • recv: the buffer to keep the received string • nBufferLen • send: the length of the string • recv: the size of the buffer used to keep the string • nFlags • Can be used to indicate urgency • Can also be used to allow reading the data but not removing it • In general, use 0 • Return the actual number of bytes transmitted or received. An error is indicated by the value less than or equal to 0
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun send / recv Examples mClientSocket is created by accept() char buffer[DEFAULT_BUFLEN]; int rVal = recv(mClientSocket, buffer, DEFAULT_BUFLEN, 0); //if rVal <= 0, error rVal = send(mClientSocket, "Hello client, have a nice day!\n", 31, 0); //if rVal <= 0, error If no incoming data is available at the socket, the recv call blocks and waits for data to arrive
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun When Finish … • When finish transmitting or receiving data, remember to destroy the sockets and release the resource • Use closesocket() and WSACleanup() SOCKET mListenSocket, mClientSocket; : //After finish using the socket ... closesocket(mListenSocket); closesocket(mClientSocket); WSACleanup(); //Release the resource acquired
Problem of Straightforward Approach • The Motion Tracking Server is responsible for two tasks: • To collect data from Kinect sensor • To communicate with the client via the socket • If no incoming data is available at the socket, the recv()call blocks to wait for data to arrive • Not desirable as the two tasks should be carried out independently Network backbone Motion Tracking Server Windows Socket
Solution – Multithreading Main Collect data from Kinect sensor Communicate with the client Routine A and B run at the same time (virtually) Routine A Routine B 21
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Processes and Threads • In Windows, usually a process will be generated when an application is executed • When an application is executed m times, m processes will be generated, each with a different process ID • A Windows process contains its own independent virtual address space with both code and data • Each process contains one or more independently execution unit, calledthreads • A process can • Create new threads within the processes • Create new, independent processes • Manage communication and synchronization between these objects
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun A Process And Its Threads Windows Process 1 Winword Process 5 Process 2 Lab1 Notepad Threads Process 3 Process 4 Winword Excel
Why Threads? • In normal execution, a program always needs to wait • Wait for user input, wait for screen display, wait for file access, etc. • It is unwise to require programs to execute one after the finish of another • Program B can make use of the waiting time of program A to start its execution as long as program B is not waiting for the result from program A • It was first proposed to achieve this by having multiple processes • However, it was soon found that the overhead (e.g. the time required) for switching between processes is very high • Besides, processes are not tightly coupled to one another, it is difficult to share resources, such as open files • They motivate the idea of further dividing a process into smaller units, i.e. threads
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun How Threads Are Executed? • A computer has only one CPU, which can execute one program at a time • Hence, in reality, threads are not executing at the same time, but alternatively one after the other • For a multithreading system, a thread has at least the following three states: Time slice end Ready Running Start execution Finish execution Got the CPU Resource ready Waiting resource Sleeping
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Implementation - CreateThread HANDLE CreateThread ( LPSECURITY_ATTRIBUTES lpsa, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddr, LPVOID lpvThreadParm, DWORD dwCreationFlag, LPDWORD lpThreadId); Example HANDLE mThread; //Handle to thread DWORD mServerThreadId; //Store the thread id mThread = CreateThread(NULL, 0, Server_ProcessThread, this, 0, &mServerThreadId);
CreateThread Parameters • Input parameters • lpsa –A pointer to a security attribute structure. It determines whether the returned handle can be inherited by child processes. If lpsa is NULL, the handle cannot be inherited • dwStackSize – Indicate thread’s stack size. Use 0 for default size • lpStartAddr – A pointer to the function to be executed. For our example, a static function Server_ProcessThread will be executed. DWORD WINAPI CSkeletalViewerApp:: Server_ProcessThread(LPVOID pParam) { : }
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun CreateThread Parameters (cont) • lpvThreadParm – A pointer passed as the thread argument. • dwCreationFlag – indicate the readiness of the thread. If 0, means that the thread is ready to run immediately • lpThreadId – A pointer to a DWORD that receives the new thread’s identifier. The system will fill in it with the thread ID • CreateThread will return the handle to the thread created. A NULL handle value indicates a failure
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Passing Parameters to Thread • A thread function only accepts one input parameter, i.e.lpvThreadParm • When setting lpvThreadParm to this, it means the pointer of the object that is creating the thread will be passed to the thread function LPVOID lpvThreadParm = this;
Receive the Passed Parameters • Note that when the thread function receives this pointer, it does not know what kind of pointer it is • A static cast is applied to define the pointer type DWORD WINAPI CSkeletalViewerApp:: Server_ProcessThread(LPVOID pParam) { CSkeletalViewerApp *pthis = static_cast<CSkeletalViewerApp*>(pParam); : }
How a Thread Terminates • Most common way: • A thread terminates itself by returning from the thread function using the exit code as the return value DWORD WINAPI CSkeletalViewerApp:: Server_ProcessThread(LPVOID pParam) { : : return 0; //exit code = 0 //Never return 259 since it is equal to the // constant STILL_ACTIVE, which is used to // indicate the thread is still active }
Department of ELECTRONIC AND INFORMATION ENGINEERING 5. Window Socket by Dr Daniel Lun Software Architecture for the Project pthis = pointer of CSkeletalViewerApp Thread 2 Thread 1 createThread(…,this,…) this CSkeletalViewerApp:: Server_Init() Server_ProcessThread() Pthis->mServer-> ListenOnPort(); mServer ServerSocket ListenOnPort() AcceptConnection() ProcessClient() CloseConnection() AcceptConnection(); ProcessClient(); CloseConnection(); ServerSocket is defined as a friend class of CSkeletalViewerApp so that it can have the access of any private and protected member variables of CSkeletalViewerApp Collect data from Kinect Sensor