Socket Programming - PowerPoint PPT Presentation

Loading...

PPT – Socket Programming PowerPoint presentation | free to download - id: 68f5f9-MDBjM



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Socket Programming

Description:

Socket Programming References: redKlyde s tutorial set Winsock2 for games (gamedev.net) Fall 2010 * Introduction (javadoc) TCP provides a reliable, point-to-point ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 34
Provided by: Melod95
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Socket Programming


1
Socket Programming
  • References
  • redKlydes tutorial set
  • Winsock2 for games (gamedev.net)

2
Introduction (javadoc)
  • TCP provides a reliable, point-to-point
    communication channel that client-server
    applications on the Internet use to communicate
    with each other.
  • To communicate over TCP, a client program and a
    server program establish a connection to one
    another. Each program binds a socket to its end
    of the connection. To communicate, the client and
    the server each reads from and writes to the
    socket bound to the connection.

3
Client/Server
  • Normally, a server runs on a specific computer
    and has a socket that is bound to a specific port
    number.
  • The server just waits, listening to the socket
    for a client to make a connection request.
  • The client knows the hostname of the machine on
    which the server is running and the port number
    to which the server is connected.
  • To make a connection request, the client tries to
    rendezvous with the server on the server's
    machine and port.

Spring 2005
4
Client/Server (cont)
  • If everything goes well, the server accepts the
    connection.
  • On the client side, if the connection is
    accepted, a socket is successfully created and
    the client can use the socket to communicate with
    the server. The client and server can now
    communicate by writing to or reading from their
    sockets.

5
Socket Definition
  • A socket is one endpoint of a two-way
    communication link between two programs running
    on the network. A socket is bound to a port
    number so that the TCP layer can identify the
    application that data is destined to be sent.

6
Winsock 2.2
  • There are different ways to program with WinSock.
  • the very basic UNIX/Berkeley type functions,
  • the Microsofts Windows specialized version of
    the basic functions, or
  • the Object Orientated MFC version.

7
Learning Objectives
  • Creating a socket (and VC setup)
  • Making the connection
  • Sending and receiving data
  • Multiple host connection
  • Optional multi-thread implementation
  • Integrating with game loop

8
redKlyde (Tutorial 1)
  • VC setting add WS2_32.lib to project
  • WSAData hold info about our network capability
  • WSAStartup () start up winsock
  • mysock socket (AF_INET, SOCK_STREAM,0)
  • creates a TCP socket for Internet fails if too
    many sockets are created
  • closesocket (mysock)
  • Every winsock function will return SOCKET_ERROR
    if error occurred
  • WSAGetLastError() to get the errorcode see msdn
    for details
  • WSACleanup() last call of winsock

9
Tutorial 1
Winsock v2.2
AF_INET Socket for internet
SOCK_STREAM TCP socket SOCK_DGRAM UDP socket
10
redKlyde (Toot 2 Server)
  • Host/net byte ordering of numbers (next page)
  • Bind associate a port to socket to send/receive
    data
  • Socket address structure sockaddr_in port no.,
    Internet protocol,
  • Port no stay above 2024
  • Listen (listensock, backlog)
  • Clientsocket accept (listensock, 0,0)
  • A blocking call use debugger to verify
  • Backlog the number of clients you can keep
    waiting before calling accept()

11
Endianness
  • Big endian
  • Network protocol
  • hton, ntoh
  • Host-network conversion
  • Little endian
  • Windows

12
hton ntoh
Sizeof (short) 4 bytes Sizeof (long) 8 bytes
htons (7654) 58909 htonl (7654) 3860660224
13
In fact
If the sending and receiving ends are of the same
endianness, there is no need to convert
7654
1D
1D
E6
E6
However, if not so, only through ntoh/hton can
guarantee the correctness of the info
14
Bind the socket to port 7654
Spring 2005
15
redKlyde (Toot2 client)
  • Which server to connect to?
  • host entry structure gethostbyname
    (servername)
  • Socket address sockaddr_in port, host_addr
  • Connect (sock, sockaddr_in, size)

16
Tut 2(c)
Or IP 140.129.25.194
17
redKlyde (Toot3)
  • Send/recv fixed length message
  • Macros for earlier commands
  • Nbytes recv (clientsock, buffer,buffersize,0)
  • Nbytes send (clientsock,buffer,size,0)

18
Tut3 (server)
Spring 2005
19
Tut3 (client)
20
redKlyde (Toot4)
  • How about variable length message?
  • TCP concatenates all packets into one big chunk
  • Use escape character to decipher
  • Send the size of message first, then recv
    accordingly
  • !? Why the size wont be mixed with the coming
    messages?!
  • This is the simple protocol we set for this
    example.

21
Tut4 (server)
22
Tut4 (client)
23
Tut5 Overview
  • Thread a sequence of instructions that may
    execute in parallel with other threads
  • Multi-threaded AP
  • Accept thread accept clients connecting to the
    server socket add client socket to masterset
  • Main thread polling from selected sets to
    process variable-length input
  • Shared memory FD_SET (set of client sockets)

24
redKlyde (Toot5 server)
  • Blocking calls accept, recv
  • Nonblocking calls send, connect,
  • FD_SET (file descriptor set) a list of sockets
  • Data member fd_count, fd_array
  • Macros for FD_SET
  • FD_SET (sock, set)
  • FD_ZERO (set)
  • FD_CLR (sock,set)
  • FD_ISSET(sock,set)
  • Select create a list of sockets that already
    have data to read
  • Exception handling
  • Clean disconnect the client closes the socket
    and exits no error
  • dropped connection an error will occur
  • Remove from FD_SET, close the corresponding socket

25
Setting Up
Code-generation debug multi-threaded
Mutex mutual exclusion algorithms used in
concurrent programming to avoid the simultaneous
use of a common resource
26
Accepting thread
27
Main Thread1
Spring 2005
28
Main Thread2
Error check
29
Error (dropped connection)
30
Error (clean disconnect)
Client called closesocket
31
Command Summary
  • WSAStartup WSACleanup
  • WSAGetLastError()
  • socket() closesocket()
  • bind() listen() accept() gethostbyname(),
    connect()
  • ntohl(), ntohs(), htonl(), htons()
  • send(), recv()
  • select()
  • FD_SET, FD_ZERO, FD_CLR, FD_ISSET

32
Networking for Game Programmers
  • Reference Glenn Fieldlers Blog

33
Topics
  • TCP vs. UDP
  • Sending Receiving Packets (UDP)
  • UDP (Virtual) Connection
  • UDP flow control
About PowerShow.com