EIE360 Integrated Project

32
1 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 Lecture 5 Windows Socket EIE360 Integrated Project

description

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 - PowerPoint PPT Presentation

Transcript of EIE360 Integrated Project

Page 1: EIE360 Integrated Project

1

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

Lecture 5

Windows Socket

EIE360 Integrated Project

Page 2: EIE360 Integrated Project

Architecture of the Interactive Virtual Aquarium System

2

USB port

3D GraphicsSystem

3D Graphics

Your program

Your program

Network

Computer A

Computer B

Kinect Sensor Device

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 3: EIE360 Integrated Project

3

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

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Motion Tracking

Server

Network backbone

Windows SocketServer IP: ???.???.???.???Port: 8888

Page 4: EIE360 Integrated Project

4

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

Process A Process B

• 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.

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 5: EIE360 Integrated Project

5

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

Process AsocketAPI runtime

supportProcess B

socketAPI runtime

support

transport layer software transport layer software

connection-oriented datagram socket

Page 6: EIE360 Integrated Project

6

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

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 7: EIE360 Integrated Project

7

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

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

Page 8: EIE360 Integrated Project

8

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

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

Page 9: EIE360 Integrated Project

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

&wsaData points to a WSADATA structure that returns information on the configuration of the DLL

iResult should be equal to zero if successful

9

WSAStartup() Parameters

sockVersion = MAKEWORD(2,2); //version 2.2

sockVersion = MAKEWORD(2,2); //version 2.2

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 10: EIE360 Integrated Project

getaddrinfo()

getaddrinfo() provides protocol-independent translation from an ANSI host name to an addr

10Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

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

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

Port number for making the connection, e.g. 8888

Means the system will be free to use any registered IP addr

Result kept here

Page 11: EIE360 Integrated Project

11

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

1. Motion Tracking – Polhemus Liberty Latus by Dr Daniel Lun

Page 12: EIE360 Integrated Project

12

Create a Socket

Call socket() to create (or open) a socket

mListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

if (mListenSocket == INVALID_SOCKET) { freeaddrinfo(result);

WSACleanup();return false;

}

mListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

if (mListenSocket == INVALID_SOCKET) { freeaddrinfo(result);

WSACleanup();return false;

}

Assume mListenSocket is a member variable of type SOCKET in your class to store the created socket

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

If create failed, free the memory for result and cleanup everything

Page 13: EIE360 Integrated Project

13

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

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

5. Window Socket by Dr Daniel Lun

result is not needed any more, so free it here

Department of ELECTRONIC AND INFORMATION ENGINEERING

Page 14: EIE360 Integrated Project

14

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;

iResult = listen(mListenSocket, SOMAXCONN);if (iResult == SOCKET_ERROR) { closesocket(mListenSocket);

WSACleanup();return false;

5. Window Socket by Dr Daniel Lun

Department of ELECTRONIC AND INFORMATION ENGINEERING

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.

Page 15: EIE360 Integrated Project

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

15

SOCKET mClientSocket = accept(mListenSocket, NULL, NULL);

//return a new socket with connection

SOCKET mClientSocket = accept(mListenSocket, NULL, NULL);

//return a new socket with connection

Page 16: EIE360 Integrated Project

16

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

Page 17: EIE360 Integrated Project

17

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

Page 18: EIE360 Integrated Project

send / recv Examples

18Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

char buffer[DEFAULT_BUFLEN];int rVal = recv(mClientSocket, buffer,

DEFAULT_BUFLEN, 0);//if rVal <= 0, error

char buffer[DEFAULT_BUFLEN];int rVal = recv(mClientSocket, buffer,

DEFAULT_BUFLEN, 0);//if rVal <= 0, error

mClientSocket is created by accept()

rVal = send(mClientSocket, "Hello client, have a nice day!\n", 31, 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

Page 19: EIE360 Integrated Project

19

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

SOCKET mListenSocket, mClientSocket;: //After finish using the socket ...

closesocket(mListenSocket);closesocket(mClientSocket);

WSACleanup(); //Release the resource acquired

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 20: EIE360 Integrated Project

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

20

Motion Tracking

Server

Network backbone

Windows Socket

Page 21: EIE360 Integrated Project

Solution – Multithreading

2121

Communicate with the client

Routine BRoutine A

Routine A and B run at the same time(virtually)

Collect data from Kinect sensor

Main

Page 22: EIE360 Integrated Project

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, called threads A process can

Create new threads within the processes Create new, independent processes Manage communication and synchronization between

these objects

22Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 23: EIE360 Integrated Project

A Process And Its Threads

23Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Windows

Winword

Process 1

Notepad

Process 2

Winword

Process 3

Excel

Process 4

Process 5

Threads

Lab1

Page 24: EIE360 Integrated Project

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

24

Page 25: EIE360 Integrated Project

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:

25

Ready RunningStart execution

Finish execution

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Sleeping

Waiting resourceResource ready

Got the CPU

Time slice end

Page 26: EIE360 Integrated Project

Implementation - CreateThread

26Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

HANDLE CreateThread (

LPSECURITY_ATTRIBUTES lpsa,

DWORD dwStackSize,

LPTHREAD_START_ROUTINE lpStartAddr,

LPVOID lpvThreadParm,

DWORD dwCreationFlag,

LPDWORD lpThreadId );

HANDLE mThread; //Handle to threadDWORD mServerThreadId; //Store the thread id

mThread = CreateThread(NULL, 0, Server_ProcessThread, this, 0, &mServerThreadId);

Example

Page 27: EIE360 Integrated Project

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.

27

DWORD WINAPI CSkeletalViewerApp::Server_ProcessThread(LPVOID pParam)

{ :}

Page 28: EIE360 Integrated Project

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

28Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 29: EIE360 Integrated Project

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

29

LPVOID lpvThreadParm = this;

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

Page 30: EIE360 Integrated Project

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

30

DWORD WINAPI CSkeletalViewerApp::Server_ProcessThread(LPVOID pParam)

{ CSkeletalViewerApp *pthis = static_cast<CSkeletalViewerApp*>(pParam);

 :

}

Page 31: EIE360 Integrated Project

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

31

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

}

Page 32: EIE360 Integrated Project

createThread(…,this,…)

Software Architecture for the Project

32

Thread 2Thread 1

Collect data from Kinect Sensor

CSkeletalViewerApp:: Server_Init()

Server_ProcessThread()

ServerSocket

ListenOnPort()AcceptConnection()ProcessClient()

CloseConnection()

ServerSocket

ListenOnPort()AcceptConnection()ProcessClient()

CloseConnection()

Pthis->mServer-> ListenOnPort(); AcceptConnection(); ProcessClient(); CloseConnection();

this

Department of ELECTRONIC AND INFORMATION ENGINEERING

5. Window Socket by Dr Daniel Lun

pthis = pointer of CSkeletalViewerApp

mServer

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