slide1 n.
Download
Skip this Video
Download Presentation
Introduction to Socket Programming

Loading in 2 Seconds...

play fullscreen
1 / 42

Introduction to Socket Programming - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

Introduction to Socket Programming. CONTENTS. SOCKETS SOCKET ADDRESS SOCKET SYSTEM CALLS CONNECTIONLESS ITERATIVE SERVER UDP CLIENT-SERVER PROGRAMS CONNECTION-ORIENTED CONCURRENT SERVER TCP CLIENT-SERVER PROGRAMS. CONTENTS (continued). BYTE ORDERING

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Socket Programming' - giuseppe-bishop


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide2

CONTENTS

  • SOCKETS
  • SOCKET ADDRESS
  • SOCKET SYSTEM CALLS
  • CONNECTIONLESS ITERATIVE SERVER
  • UDP CLIENT-SERVER PROGRAMS
  • CONNECTION-ORIENTED CONCURRENT SERVER
  • TCP CLIENT-SERVER PROGRAMS
slide3

CONTENTS (continued)

  • BYTE ORDERING
  • ADDRESS TRANSFORMATION
  • BYTE MANIPULATION FUNCTIONS
  • INFORMATION ABOUT REMOTE HOST
what is socket
Unix programs do any sort of I/O by reading or writing to a file descriptor.

This logical file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else.

When a program wants to communicate with another program over the Internet, also do it through a file descriptor-- socket descriptor.

What is socket
slide9

socket address structure

// General socket address structure

struct sockaddr

{ unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address

}; OR

// Internet address: AF_INET

struct sockaddr_in

{ short sin_family; // 2 bytes

u_short sin_port; // 2 bytes

struct in_addr sin_addr; // 4 bytes

char sin_zero; // 8 bytes, unused, set to 0

}

slide10
// Internet address

struct in_addr

{ unsigned long s_addr; // that's a 32-bit long, or 4 bytes

};

OR

Internet address structure

struct in_addr

{ union

{ struct { u_char s_b1, s_b2, s_b3,s_b4; };

struct { u_short s_w1, s_w2; };

u_long S_addr;

} S_un;

}

#define s_addr S_un.S_addr

internet address structure
struct sockaddr_in my_addr;

my_addr.sin_addr.s_addr = INADDR_ANY; // use my IP address

By setting my_addr.sin_addr.s_addr to INADDR_ANY, you are telling it to automatically fill in the IP address of the machine the process is running on.

Internet address structure
slide13

Declaration for bind function

sockfd is the socket file descriptor returned by socket().

localaddr is a pointer to a struct sockaddr that contains information about your address, namely, port and IP address.

localaddrlen can be set to sizeof(struct sockaddr).

By setting localaddr.sin_port to zero, you are telling bind() to choose the port for you.

slide14

IANA Port Numbers Ranges

The Internet Assigned Numbers Authority(IANA)

http://www.iana.org/assignments/port-numbers

slide15

Declaration for listen function

sockfd is the usual socket file descriptor from the socket() system call.

backlog is the number of connections allowed on the incoming queue.

slide16

Declaration for connect function

sockfd is the socket file descriptor, as returned by the socket() call. But can be used to communicate with server after successfully called connect().

serveraddr is a struct sockaddr containing the destination port and IP address.

serveraddrlen can be set to sizeof(struct sockaddr).

slide17

Declaration for accept function

sockfd is the listen()ing socket descriptor. Easy enough.

clientaddr will usually be a pointer to a local struct sockaddr_in. This is where the information about the incoming connection will go (and with it you can determine which host is calling you from which port).

clientaddrlen is a local integer variable that should be set to sizeof(struct sockaddr_in) before its address is passed to accept().

slide19

Declaration for write function

Used in connection-oriented (TCP) program.

sockfd is the socket descriptor you want to send data to (whether it's the one returned by socket() or the one you got with accept().) buf is a pointer to the data you want to send, and buflen is the length of that data in bytes.

If the value returned by write() doesn't match the value in buflen, it's up to you to send the rest of the string.

Ssize_t send(int sockfd, const void *buf, int buflen, int flags);

Just set flags to 0. Set flag=MSG_OOB to send out-of-band data.

slide20

Declaration for read function

Used in connection-oriented (TCP) program.

A similar system call

ssize_t recv(int sockfd, void *buf, int len, unsigned int flags);

sockfd is the socket descriptor to read from, buf is the buffer to read the information into, buflen is the maximum length of the buffer, flags can again be set to 0.

Set flag=MSG_OOB to read out-of-band data.

slide21

Declaration for sendto function

Used in connection-less (UDP) program.

toaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the destination IP address and port).

toaddrlen can simply be set to sizeof(struct sockaddr).

slide22

Declaration for recvfrom function

Used in connection-less (UDP) program.

fromaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the IP address and port of the sender).

fromaddrlen can simply be set to sizeof(struct sockaddr).

slide23

CONNECTIONLESSITERATIVE

SERVER &

UDP CLIENT-SERVER

PROGRAMS

slide24

Socket interface

for connectionless

iterative server

slide25

CONNECTION-ORIENTED

CONCURRENT SERVER

slide26

Socket interface

for connection-oriented

concurrent server

slide29

Part I

Client

and

Server

slide36

Declarations for byte-order transformation

Examples:

my_addr.sin_port = htons(0); // choose an unused port at random my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address

slide38

Address transformation

Internal data in socket_address structure

Predefined in program or user input

slide39

Declarations for address

transformation functions

Example:

struct sockaddr_in dest_socket_add;

dest_socket_add.sin_addr.s_addr = inet_addr("10.12.110.57");

slide41

Declaration for gethostbyname

When you know the remote site’s domain name, but you don’t know its IP address.