socket programming tutorial department of computer science southern illinois university edwardsville...

38
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: [email protected] CS447 - Computer and Data Communication Socket/001

Upload: dylan-reynolds

Post on 13-Dec-2015

231 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket Programming Tutorial

Department of Computer ScienceSouthern Illinois University Edwardsville

Fall, 2015

Dr. Hiroshi FujinokiE-mail: [email protected]

CS447 - Computer and Data Communication

Socket/001

Page 2: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

• A socket is a virtual connection between two applications

• Using a socket, two processes can communicate with each other

• The socket is the major communication tool for Internet applications

• A socket is bi-directional (full-duplex) transmission

• A socket can be created dynamically

What is “socket”?

CS447 - Computer and Data Communication

Socket/001

Page 3: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Network

Socket as a virtual connection between two processes

(physical connection)

Host B

Process 2

Host A

Process 1

Network Adapter Card

Socket + NW protocol

Socket connection

(virtual connection)

Information Hiding

CS447 - Computer and Data Communication

Socket/002

Page 4: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Server

Host A

The server should always be waiting for requests from the clients. A client makes a request, then the server responds.

Client 2

Client 3

Host C

socket

socket

Client 1

Host B

Socket as a client/server model

RequestReply

Socket

How can we distinguishtwo connections?

“PORT #”

CS447 - Computer and Data Communication

Socket/003

Page 5: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/004

Concept of “ports”

What is “port”?

It’s a logical connecting point at the transport-layer protocol.

If we do not have “ports”, what would happen?

IP Address146.163.147.81

Server Host

Internet

Request

ResponseRequest (1)

Response with tag (2)

Request (3)

Response (4)

Internet

Request

Response

Internet

Client Host XProcess AProcess B

146.163.147.81

Client Host Y

Connect toprocess B

146.163.147.81

Page 6: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/005

Concept of “ports” (2)

IP Address146.163.147.81

Server Host

Internet

Request

ResponseRequest (1)

Response with tag (2)

Request (3)

Response (4)

Internet

Request

Response

Internet

Client Host XProcess AProcess B

146.163.147.81

Client Host Y

Connect toprocess B

146.163.147.81

If we use only IP address, we can’t select a destinationprocess at the destination host

Only one network application programCan exist at a time

Page 7: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Host Computer

IP Address

CS447 - Computer and Data Communication

Socket/006

MAC Layer

LLC Layer

Network Layer (IP Layer)

Transport Layer (TCP Layer)

Process DProcess CProcess BProcess A

ports

104591336055 12133

Concept of “ports” (3)

Physical Layer

Datalink Layer

Page 8: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

SERVER

bind()

listen()

accept()

read()

write()

close()

CLIENT

socket()

connect()

write()

close()

socket()

1: Connection Request

2. Send a request

3. Receive the resultread()

“*” indicates a blocking function call.

*

*

*

*Request Acknowledge

CS447 - Computer and Data Communication

Socket/007

Client/Server Process OrganizationApplication Programming Interface

API

Page 9: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Functions and parameter format (for server side)

(1) create socket: socket_id = socket (AF_INET, SOCK_STREM, DEFAULT_PROTOCOL);(2) bind socket: bind (socket_id, server_addr, server_len);(3) listen to socket: listen (socket_id, number_of_connection);(4) accept a connection: accept (socket_id, &client_addr, &client_len);(5) read (receive) data: read (socket_id, buffer, buffer_len); (6) write (send) data: write (socket_id, buffer, buffer_len);(7) close socket: close(socket_id);

Socket/008

CS447 - Computer and Data Communication

Page 10: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Functions and parameter format (for client side)

(1) create socket: same as server socket_id = socket (AF_INET, SOCK_STREM, DEFAULT_PROTOCOL);

(2) connect socket: connect (socket_id, serverINETaddress, server_len);

(3) write (send) data: write (socket_id, buffer, buffer_len);

(4) read (receive) data: read (socket_id, buffer, buffer_len);

(5) close socket: same as server close(socket_id);

Socket/009

CS447 - Computer and Data Communication

Page 11: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

“*” indicates a blocking function call.SERVER

bind()

listen()

accept()

read()

CLIENT

socket()

connect()

write()

close()

socket()

*

*

*

1: Connection Request

2. Send a command

3. Receive the resultread()write()

We are notdoing this...

*

Socket/010

4. END

CS447 - Computer and Data Communication

Page 12: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Server

Step 1: socket( ) call

It declares a socket to be used.

After socket(_) call:

Socket/011

• Prepare data structure to manage socket

• OS is responsible for this

Server Host Computer

CS447 - Computer and Data Communication

Page 13: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 2: bind( ) call

It connects a process to a specific port

After bind(_) call:

Port Numbers:

0~1023: System Reserved Port 21: FTP Port 23: telnet Port 80: HTTP

1024 and above: available to users

Socket/012

Port

Port =

A logical connecting point at a hostfor two communicating processesusing socket6500

Server

CS447 - Computer and Data Communication

Page 14: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 3: listen( ) call

After listen(_) call:

6500

Buffer

listen (socket_id, number_of_connection);

listen( ) system call: prepare memorybuffer for incoming connections

Server

Socket/013

Client request

We need to specify how many connection requests should be held in the buffer when SERVER is busy (can’t accept a request).

CS447 - Computer and Data Communication

Page 15: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 4 - Part 1: accept( ) call

After accept(_) call:

Server

6500

accept ( ) function isa blocking function

Socket/014

The server process accepts a request from a client

Client

CS447 - Computer and Data Communication

Page 16: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 4 - Part 2: accept( ) call

The accept(_) call returns another port number and establish another connection

Client

7100

6500

Socket/015

A new port is assigned by OS

OS duplicates the socket connection

Server needs to close the first

Server

CS447 - Computer and Data Communication

Page 17: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 5: read( ) and write( ) call

Client

Server 7100

The server and client communicate using the second socket

6500

Socket/016

Data transmission

write (or send)read (or recv)

CS447 - Computer and Data Communication

Page 18: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 6: close ( ) call

Client

Server 7100

Close the second socket and leave the first socket for next client

6500

Socket/017

CS447 - Computer and Data Communication

Page 19: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Step 7: Go back to accept( ) call

Client

Server

6500

Socket/018

The server process goes back to the accept call

CS447 - Computer and Data Communication

Page 20: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/019

Winsock Programming

Technical Details

CS447 - Computer and Data Communication

Page 21: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/020

Initialize Winsock

void main (void){ /* The following two lines needed for Window's socket */ WORD wVersionRequested = MAKEWORD(1,1); /* Stuff for WSA functions */ WSADATA wsaData; /* Stuff for WSA functions */

/* This stuff initializes winsock */WSAStartup(wVersionRequested, &wsaData);

/* Create a socket */My_SocketID = socket ( ….. );

Step 1: Define your socket

Step 2: Initialize your socket

Step 3: Start using it

Winsock version 1.1

CS447 - Computer and Data Communication

Page 22: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/021

socket ( ) function

unsigned int socket_id = socket (AF_INET, SOCK_STREAM, 0);

“AF_INET” = Use IP protocol

“SOCK_STREAM” = Use TCP

Returns socket ID on success

Always 0

CS447 - Computer and Data Communication

Page 23: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/022

bind ( ) function

int status = bind (socket_id, (struct sockaddr_in *) my_addr, sizeof(my_addr));

The sockaddr_in structureto specify port # and IP addressof this machine (server machine)

The byte size of theSockaddr_in structure

Return code (< 0 if error)

Socket ID returned by socket function

CS447 - Computer and Data Communication

Page 24: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/023

The “sock_addr” structure

struct sockaddr_in my_addr; /* My (client) Internet address */

/* Set My(client's) IP Address ---------------------------------------- */my_addr.sin_family = AF_INET; /* Address Family To Be Used */ my_addr.sin_port = htons (MY_PORT_NUM); /* Port number to use */ my_addr.sin_addr.s_addr = htonl (INADDR_ANY); /* My IP address */

Step 1: You instantiate the structure

Step 2: Fill up the components

CS447 - Computer and Data Communication

Page 25: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/024

listen ( ) function

int status = listen (socket_id, 3);

The size of the connection request buffer

Return code (< 0 if error)

Socket ID returnedby socket function

CS447 - Computer and Data Communication

Page 26: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/025

accept ( ) function

unsingned int child_sock = accept (socket_id, (struct sockaddr_in *) client_addr,

The size of the sockaddr_in structure for connecting client

duplicated socket ID (< 0 if error)

Socket ID returnedby socket function

sizeof (client_addr);

The sockaddr_in structure for a connecting client

CS447 - Computer and Data Communication

Page 27: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/026

recv ( ) function

int status = recv (child_sock, in_buffer, MAX_BUFFER_SIZE, 0);

Return code (< 0 if error)

Always 0

The maximum buffer size

The input (receive) bufferas a character string

Example: char in_buffer [MAX_BUFFER]

Socket ID returnedby socket function

On success, the number of bytes received

CS447 - Computer and Data Communication

Page 28: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/027

send ( ) function

int status = send (child_sock, out_buffer, MAX_BUFFER_SIZE, 0);

Return code (< 0 if error)

Socket ID returnedby socket function

Always 0

The maximum buffer size

The output (send) bufferas a character string

On success, the number of bytes actually sent

CS447 - Computer and Data Communication

Page 29: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/028

closesocket ( ) function

int status = closesocket (child_sock);

Return code (< 0 if error)

Socket ID returnedby socket function

CS447 - Computer and Data Communication

Page 30: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

Socket/029

Clear winsock

After you call “closesocket” function but before your program is terminated

/* This stuff cleans-up winsock */WSACleanup( );

CS447 - Computer and Data Communication

Page 31: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

“*” indicates a blocking function call.SERVER

bind()

listen()

accept()

read()

CLIENT

socket()

connect()

write()

close()

socket()

*

*

*

1: Connection Request

2. Send a command

3. Receive the resultread()write()

*

Socket/030Format of time stamp: “HH:MM:SS”

Client ID#

1 digitinteger

CS447 - Computer and Data Communication

Page 32: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/031

How to specify your destination in socket?

Server Host

Client Host X

IP Address

ServerProcess

Port #Each destination for a socket connectionis determined by < IP address + Port# >

< IP Address + Port# >

Page 33: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/032

struct sockaddr_in { u_char sin_len; /* Length of this structure */ u_char sin_family; /* Network protocol used*/ u_short sin_port; /* Port number */ struct in_addr sin_addr; /* Pointer to an IP address */ char sin_zero[8]; /* Extra information */};

How to specify your destination in our socket program source code?

struct in_addr { u_long s_addr; /* Actual IP address */};

sockaddr_in structure is used to define your destination

• The sockaddr_in structure is defined in C/C++ struct

• The sockaddr_in structure is defined in windows.h header file

Pointer

Page 34: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/033

How to specify your destination in our socket program source code (part 2)?

An instance of sockaddr_in structure in memory

Structure length (in bytes)

Network-layer protocol

Port number

Pointer to in_addr structure

Extra information

An instance of in_addrstructure in memory

IP address as a binary number (32 bits)

Page 35: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/034

How to specify your destination in our socket program source code (part 3)?

An instance of sockaddr_in structure in memory

128

IP protocol

1050

Extra information

An instance of in_addrstructure in memory

146 163 147 59

Page 36: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/035

struct sockaddr_in server_address;

How to specify your destination in our socket program source code (part 4)?

How can I set the IP address and the port number of my destination?

STEP #1: Instantiate a sockaddr_in structure:

STEP #2: Set your destination IP address:

server_address.sin_addr.s_addr = inet_addr(“146.163.147.59”);

server_address.sin_addr.s_addr = inet_aton(“cougar.siue.edu”);

Case 1: by “32-bit IP address”

Case 2: by a host name

Case 3: by a system-defined parameter

server_address.sin_addr.s_addr = htonl(INADDR_ANY);

IP Address

Host Name

“INADDR_ANY” keyword

Page 37: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/036

How to specify your destination in our socket program source code (part 4)?

How can I set the IP address and the port number of my destination?

STEP #3: Set your destination port number:

server_address.sin_port = htons(80);

You are connectingto port #80!

Page 38: Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu

CS447 - Computer and Data Communication

Socket/037

#define SERVER_IP "146.163.144.99“ /* Server IP address */#define SERVER_PORT 8050 /* Server-side port # */

struct sockaddr_in server_addr; /* Server Internet address */

/* Set Server's IP Address --------------------------------------------- */server_addr.sin_family = AF_INET; /* Address Family to be Used */server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); /* IP address */server_addr.sin_port = htons(int(SERVER_PORT));/* Port num to use */

Example of setting IP address and port number