Winsock

advertisement
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
1
Architecture of the Interactive
Virtual Aquarium System
Computer A
Your
program
USB port
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
2
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
3
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
Process A
Process B
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.
a socket
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
4
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 connectionoriented 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

socket
API runtime
support
Process A
Process B
socket
API runtime
support
transport layer software
transport layer software
connection-oriented datagramsocket
5
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
6
General Procedure for Setting
Up a Server Using Winsock

Just follow the procedure below and fill in the
required information:
WSAStartup( ... );
getaddrinfo ( ... );
socket ( ... );
bind ( ... );
listen ( ... );
accept ( ... );
send ( ... ); / recv
//1. Initialize
//2. Collect network info
//3. Create a socket
//4. Bind the socket to IP addr
//5. Listen to incoming request
//6. If connect request recv, accept
( ... );
//7. Send or receive data
:
closesocket ( ... ); //8. Close the socket after using7 it
WSACleanup ( ... ); //9. Free resource allocated
Winsock Initialization


To initialize, a nonstandard Winsock-specific
function WSAStartup() must be the first
function to call
For example,
WORD sockVersion;
WSADATA wsaData;
//Store the socket version
//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
8
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
sockVersion = MAKEWORD(2,2);
//version 2.2

&wsaData points to a WSADATA structure that returns

information on the configuration of the DLL
iResult should be equal to zero if successful
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
9
getaddrinfo()
getaddrinfo() provides protocolindependent translation from an ANSI host
struct
addrinfo
*result = NULL, hints;
name
to an addr

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
Result kept here
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();
Port number for making the
return false;
connection, e.g. 8888
} Department of
10
ELECTRONIC AND INFORMATION ENGINEERING
Means
will be free to use any registered IP addr
5. Windowthe
Socketsystem
by Dr Daniel Lun
addrinfo parameters




ai_family = AF_INET denotes the address family.
designates the Internet protocol (IP)
ai_socktype = SOCK_STREAM specifies connectionoriented (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
11
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
12
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); result is created in
getaddrinfo()
WSACleanup();
mListenSocket is
return false;
created in socket()
}
freeaddrinfo(result);
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
result is not needed
any more, so free it here
13
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
Department of
for socket will set the backlog to a 14
ELECTRONIC AND INFORMATION ENGINEERING
maximum reasonable value.
5. Window Socket by Dr Daniel Lun
Listen and Accept (cont)

The accept() function permits an incoming
connection attempt on a socket
SOCKET mClientSocket = accept
(mListenSocket, NULL, NULL);
//return a new socket with connection


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
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
16
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
17
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
Department of
recv
callAND
blocks
and
waits for data to arrive
ELECTRONIC
INFORMATION
ENGINEERING
5. Window Socket by Dr Daniel Lun
18
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
19
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
Motion
Tracking
Server
Network
backbone
20
Windows Socket
Solution – Multithreading
Main
Collect data from
Kinect sensor
Routine A
Communicate with
the client
Routine A and
B run at the
same time
(virtually)
Routine B
21
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
22
A Process And Its Threads
Windows
Process 1
Process 5
Winword
Process 2
Lab1
Threads
Notepad
Process 3
Process 4
Winword
Excel
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
23
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
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:
Start
execution
Time slice end
Ready
Running
Got the CPU
Resource ready
Finish
execution
Waiting resource
Sleeping
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
25
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,
Department of
&mServerThreadId);
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
26
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)
{
:
}
27
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
28
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;
Department of
ELECTRONIC AND INFORMATION ENGINEERING
5. Window Socket by Dr Daniel Lun
29
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);
:
}
30
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
31
}
Software Architecture for
pthis = pointer of
the Project
CSkeletalViewerApp
Thread 1
this
CSkeletalViewerApp
:: Server_Init()
mServer
ServerSocket
ListenOnPort()
AcceptConnection()
ProcessClient()
CloseConnection()
Thread 2
createThread
(…,this,…) Server_ProcessThread()
Pthis->mServer->
ListenOnPort();
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
Department of
32
ELECTRONIC AND INFORMATION ENGINEERING
of CSkeletalViewerApp
5. Window Socket by Dr Daniel Lun
Collect data from
Kinect Sensor
Download