LINUX SOCKET PROGRAMMING - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

LINUX SOCKET PROGRAMMING

Description:

Socket Attributes - Socket Protocol (cont' ... Network Management Protocol. Network File System. Remote Procedure Call. Trivial File Transfer Protocol. TCP/IP ... – PowerPoint PPT presentation

Number of Views:405
Avg rating:3.0/5.0
Slides: 40
Provided by: lens1Csi
Category:

less

Transcript and Presenter's Notes

Title: LINUX SOCKET PROGRAMMING


1
LINUX SOCKET PROGRAMMING
  • LAB 3

2
Outline
  • Introduce to Client/Server
  • Introduce to IPV4
  • Introduction to Socket
  • Socket Attributes
  • TCP/IP Components
  • TCP Socket Function
  • TCP Client/Server Structure
  • UDP Socket Function
  • UDP Client/Server Structure
  • Socket Example

3
Outline (cont)
  • Blocking and Nonblocking

4
Introduction to Client-Server
  • Client-server is computing architecture which
    separates a client from a server, and is almost
    always implemented over a computer network. Each
    client or server connected to a network can also
    be referred to as a node. The most basic type of
    client-server architecture employs only two types
    of nodes clients and servers. This type of
    architecture is sometimes referred to as
    two-tier. It allows devices to share files and
    resources.

5
Introduction to Client-Server (cont)
  • Each instance of the client software can send
    data requests to one or more connected servers.
    In turn, the servers can accept these requests,
    process them, and return the requested
    information to the client. Although this concept
    can be applied for a variety of reasons to many
    different kinds of applications, the architecture
    remains fundamentally the same.
  • These days, clients are most often web browsers,
    although that has not always been the case.
    Servers typically include web servers, database
    servers and mail servers.

6
Introduction IPV4
  • Internet Protocol version 4 is the fourth
    iteration of the Internet Protocol (IP) and it is
    the first version of the protocol to be widely
    deployed. IPv4 is the dominant network layer
    protocol on the Internet and apart from IPv6 it
    is the only protocol used on the Internet.

7
Introduction to Socket
  • A socket is one of the most fundamental
    technologies of computer networking. Sockets
    allow applications to communicate using standard
    mechanisms built into network hardware and
    operating systems.

8
Socket Attributes
  • Socket Domains
  • Socket Types
  • Stream Sockets
  • Datagram Sockets
  • Socket Protocols

9
Socket Attributes Socket Domain (cont)
  • Domains specify the network medium that the
    socket communication will use it.
  • The most common socket domain is AF_INET, which
    refers to Internet networking
  • AF_UNIX UNIX internal
  • AF_INET ARPA Internet protocol
  • AF_ISO ISO standard protocols
  • AF_NS Xerox Network System protocols
  • AF_IPX Novell IPX protocol
  • AF_APPLETALK Appletalk DDS
  • AF_X25 X.25 protocol

10
Socket Attributes Socket Type (cont)
  • Stream Sockets
  • Stream sockets (in some ways similar to standard
    input/output streams) provide a connection that
    is a sequence and a reliable two-way byte stream.
  • Datagram Sockets
  • Datagram sockets are implemented in the AF_INET
    domain by UDP/IP connections and provide a
    non-sequence, unreliable service.

11
Socket Attributes - Socket Protocol (cont)
  • Where the underlying transport mechanism allows
    for more than one protocol to provide the
    requested socket type, you can select a specific
    protocol for a socket.
  • Well concentrate on UNIX network and file system
    sockets, which dont require you to choose a
    protocol other than the default.

12
TCP/IP Components - Overview
  • Telnet
  • File Transfer Protocol
  • Simple Mail Transfer Protocol
  • Kerberos
  • Domain Name System
  • Simple Network Management Protocol
  • Network File System
  • Remote Procedure Call
  • Trivial File Transfer Protocol

13
TCP/IP Components Overview (cont)
  • Transmission Control Protocol
  • User Datagram Protocol
  • Internet Protocol
  • Internet Control Message Protocol

14
TCP Socket Function
  • Create and bind function socket and bind
  • Listening socket function listen
  • Accept connect require accept
  • Create connection and close connect and close
  • I/O function read and write
  • Advance I/O recv, send, readv, writev

15
TCP Client-Server Structure
  • Client
  • Set up socket
  • Connect
  • I/O
  • close
  • Server
  • Set up socket
  • Bind socket
  • Listen
  • Accept
  • I/O

16
TCP Client-Server Structure (cont)
17
TCP Client-Server Structure - Q A (cont)
  • How many clients to one server we can have each
    time?
  • Can you explain the priority for a normal
    connection between client and server?
  • How is the situation of client when server is
    gone ?

18
UDP Socket Function
  • UDP
  • Connectionless
  • Unreliable
  • Real-time
  • Create and bind function socket and bind
  • Connect interface connect
  • I/O function recvfrom and sendto

19
UDP Client-Server Structure
  • UDP client
  • Socket
  • Sendto (write)
  • Recvfrom (read)
  • UDP sever
  • Socket
  • Bind
  • Recvfrom
  • sendto

20
UDP Client-Server Structure (cont)
21
Socket Example- TCP Server
void main() sockfd socket(AF_INET,
SOCK_STREAM, 0) server_address.sun_family
AF_INET server_address.sin_addr.s_addrinet_addr
(127.0.0.1) server_address.sin_port
9734 server_len sizeof(addres) bind(server_s
ockfd,(struct sockaddr) server_address,
server_len) listen(server_sockfd,5) while(1)
char ch printf(server waiting\n) client_
len sizeof(client_address) client_sockfd
accept(server_sockfd, (struct sockaddr
)client_address, client_len) read(client_sock
fd, ch, 1) ch write(client_sockfd) clo
se(client_sockfd)
22
Socket Example- TCP Client (cont)
void main() char chA sockfd
socket(AF_INET, SOCK_STREAM, 0) address.sun_fami
ly AF_INET address.sin_addr.s_addrinet_addr(1
27.0.0.1) address.sin_port 9734 len
sizeof(addres) result connect(sockfd, (struct
sockaddr )address) if(result
-1) perror(oops client1) exit(1) w
rite( sockfd, ch , 1) read ( sockfd, ch ,
1) close( sockfd)
23
Socket Example- Socket Function (cont)
include ltsys/types.hgt include
ltsys/socket.hgt int socket(int domain, int type,
int protocol)
  • Domain
  • AF_UNIX, AF_INET, AF_ISO
  • Type
  • SOCK_STREAM
  • TCP
  • SOCK_DGRAM
  • UDP
  • Protocol
  • 0 default protocol setting

24
Socket Example- IPV4 Address Structure (cont)
struct socketaddr_in short int sin_family
/ AF_INET / unsigned short int
sin_port / Port number / struct in_addr
sin_addr / Internet address /
struct in_addr unsigned long int s_addr
  • address.sin_family AF_INET
  • address.sin_addr.s_addrinet_addr(127.0.0.1)
  • inet_addr(), this function translate IP address
    into specified format for socket
  • address.sin_port 9734 //1234? or abcd?

25
Socket Example- Bind Function (cont)
include ltsys/types.hgt include
ltsys/socket.hgt int bind(int socket, const struct
sockaddr address, size_t address_len)
  • The bind() system call assigns an address to an
    unbound socket
  • Socket
  • File descriptor
  • Address
  • Assign an address to its
  • Address_len
  • The length of sockaddr_in

26
Socket Example- Listen and Connect (cont)
  • Listening socket
  • Usually only one in server
  • It will exit before server close
  • Connected socket
  • Every client connect to server will built one I/O
    with client
  • When client close, the socket will close

27
Socket Example- Listen Function (cont)
include ltsys/socket.hgt int listen(int socket,
int backlog)
  • socket
  • File descriptor
  • backlog
  • The length of queue of unhandled request

28
Socket Example- Connect Function (cont)
include ltsys/socket.hgt int connect (int socket,
const struct sockaddr address, size_t
address_len)
  • socket
  • File descriptor
  • address
  • Address of wanted to connection
  • address_len
  • The length of sockaddr_in

29
Socket Example- I/O Function (cont)
includeltsys/socket.hgt int read (int sockfd,
char buf, intlen)
includeltsys/socket.hgt int write (int sockfd,
char buf, intlen)
  • sockfd
  • File descriptor
  • buf
  • The location of preparing for reading data into
    or writing data out
  • intlen
  • The length of above buf

30
Socket Example- Sendto and Recvfrom (cont)
  • Sendto
  • Recvfrom

includeltsys/socket.hgt int sendto(intsocketfd,
void buff, size_tnbytes, intflag,
Structsockaddr to, socklen_t addrlen)
includeltsys/socket.hgt int recvfrom(int
socketfd, void buff, size_t nbytes, int
flag, Structsockaddr from, socklen_t addrlen)
31
Blocking and Nonblocking
  • Blocking (synchronous) socket
  • In this mode, the program will not resume
    execution until the socket operation has
    completed. Blocking sockets in 16-bit application
    will allow it to be re-entered at a different
    point, and 32-bit applications will stop
    responding to user actions. This can lead to
    complex interactions if there are multiple active
    controls in use by the application.
  • Non-blocking (asynchronous) socket
  • Allow your application to respond to events. For
    example, when the remote system writes data to
    the socket, a Read event is generated for the
    control. Your application can respond by reading
    the data from the socket, and perhaps send some
    data back, depending on the context of the data
    received.

32
Blocking and Nonblocking (cont)
  • A combination of blocking and non-blocking socket
    operations
  • The ability to switch between blocking and
    non-blocking modes "on the fly" provides a
    powerful and convenient way to perform socket
    operations.

33
LAB1- TCP
  • Exercise client.c and server.c
  • Gcc o client.o client.c
  • Gcc o server.o server.c
  • Execute server and client
  • ./server.o
  • ./client.o
  • Modify the client file to sum the number 1 up to
    10 and then send the answer including a string in
    advance to server by TCP socket, for example,
    The sum from 1 to 10 is 55
  • Please show me what your result is

34
LAB2 - UDP
  • Exercise udpserv.c
  • Gcc o udpserv.o server.c
  • Execute ./udpserv
  • Enable service program - netstat ln
  • Part of output as below

35
LAB2 - UDP (cont)
  • Active Internet connections (only servers)Proto
    Recv-Q Send-Q Local Address Foreign Address
    Statetcp 0 0 0.0.0.032768 0.0.0.0 LISTEN
  • Note1 If udp side has 0.0.0.08888, that means
    you are on the way. Then it is ready to receive
    any IP address and port number 8888?
  • Note2 If execute ./udpserv again,you will see
    the information as below
  • bind error Address already in use
  • Exercise udpclient.c
  • Gcc o udpclient.o udpclient.c
  • ./client.o 127.0.0.1

36
LAB2 - UDP (cont)
  • Active client program
  • Hello, World!
  • Hello, World!
  • this is a test
  • this is a test
  • d
  • Please show me what your result is
  • Note1
  • If execute ./udpclient 127.0.0.1 again,you will
    see the information as below
  • testread error Connection refused

37
LAB2 - UDP (cont)
  • Modify the server file to count the number of
    receiving, for example, receive 1 times-gt
    receive 2 times -gt .

38
Homework
  • A. List all files in etc directory of your client
    and then save it to list_client_etc.txt. Finally
    send this file, which name is list_server_etc_1.tx
    t, to server by TCP socket
  • B. Do the same thing by UDP socket. (Result
    list_server_etc_2.txt)
  • C. Where is the difference between
    list_server_etc_1.txt and list_server_etc_2.txt?
  • Note If you can done this in Lab, please show me
    and have a bonus.

39
Reference
  • (ebook) Programming - Teach Yourself TCP-IP in 14
    Days 2nd Edition
  • (ebook) Complete UDP-TCP Port Number List
  • http//en.wikipedia.org/wiki/Client-server
  • http//en.wikipedia.org/wiki/IPv4
  • http//www.developerfusion.co.uk/show/28/8/
  • http//www.alhem.net/project/example9/index.html
  • http//www.cnds.jhu.edu/courses/cs111/lect8_600.11
    1/img5.htm
  • http//www.cnpaf.net/class/UDP/0532918532729212.ht
    ml
Write a Comment
User Comments (0)
About PowerShow.com