Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)

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.
Transport Layer3-1 Transport Overview and UDP. Transport Layer3-2 Goals r Understand transport services m Multiplexing and Demultiplexing m Reliable data.
Socket Programming.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
Page: 1 Director 1.0 TECHNION Department of Computer Science The Computer Communication Lab (236340) Summer 2002 Submitted by: David Schwartz Idan Zak.
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.
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.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
ISP – 9 th Recitation Socket programming – Client side.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Assignment 3 A Client/Server Application: Chatroom.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Sirak Kaewjamnong Computer Network Systems
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.
CSTP FS01CS423 (cotter)1 Protocols 2 References: RFC’s 791, 793, 768, 826.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Sockets API Overview Sockets with UDP Sockets with TCP Fast Sockets (Fast UDP) IP Multicasting.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
Remote Shell CS230 Project #4 Assigned : Due date :
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Client/Server Socket Programming Project
Part 4: Network Applications Client-server interaction, example applications.
Distributed systems (NET 422) Prepared by Dr. Naglaa Fathi Soliman Princess Nora Bint Abdulrahman University College of computer.
Network Programming By J. H. Wang Nov. 21, Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Berkeley Socket Abstraction
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Chapter 9: Transport Layer
Assignment 3 A Client/Server Application: Chatroom
Instructor Materials Chapter 9: Transport Layer
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
UNIX Sockets COS 461 Precept 1.
MCA – 405 Elective –I (A) Java Programming & Technology
Jim Fawcett CSE 687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE 775 – Distributed Objects Spring 2007
Socket Programming in C
27.
Chapter 2 Application Layer
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)

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

Fall 2010Spring 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.

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

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

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

Fall 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

Fall 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

Fall Tutorial 1 Winsock v2.2 AF_INET: Socket for internet SOCK_STREAM: TCP socket [SOCK_DGRAM: UDP socket]

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

Fall [Endianness] Big endian Network protocol hton, ntoh Host-network conversion Little endian Windows

Fall 2010 hton & ntoh 12 Sizeof (short) = 4 bytes Sizeof (long) = 8 bytes htons (7654) = htonl (7654) =

Fall 2010 In fact: If the sending and receiving ends are of the same endianness, there is no need to convert 1D E6 1D E However, if not so, only through ntoh/hton can guarantee the correctness of the info…

Fall 2010Spring Bind the socket to port 7654

Fall redKlyde (Toot#2 – client) Which server to connect to? “ host entry ” structure: gethostbyname (servername) Socket address [sockaddr_in]: port, host_addr Connect (sock, sockaddr_in, size)

Fall Tut 2(c) Or IP “ ”

Fall redKlyde (Toot#3) Send/recv fixed length message Macros for earlier commands Nbytes = recv (clientsock, buffer,buffersize,0) Nbytes = send (clientsock,buffer,size,0);

Fall 2010Spring Tut3 (server)

Fall Tut3 (client)

Fall redKlyde (Toot#4) 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 won ’ t be mixed with the coming messages?! This is the simple protocol we set for this example.

Fall Tut4 (server)

Fall Tut4 (client)

Fall 2010 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) 23

Fall redKlyde (Toot#5 – 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

Fall 2010 Setting Up 25 Mutex: mutual exclusion algorithms used in concurrent programming to avoid the simultaneous use of a common resource Code-generation: debug multi-threaded

Fall Accepting thread

Fall 2010 Main Thread 1 Spring

Fall Main Thread 2 Error check…

Fall 2010 Error (dropped connection) 29

Fall 2010 Error (clean disconnect) 30 Client called closesocket

Fall 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

Networking for Game Programmers Reference: Glenn Fieldler’s BlogGlenn Fieldler’s Blog Fall

Topics TCP vs. UDP Sending & Receiving Packets (UDP) UDP (Virtual) Connection UDP flow control Fall