Welcome Guestlogin to KGsePGregister at KGsePG email | FAQs

Linux Socket Programming

download

    1 of 39

    Linux Socket Programming



    Linux Socket Programming - Transcript


    LINUX SOCKET PROGRAMMING
    LAB 3

    Home Automation Networking and Entertainment Lab

    Dept of Computer Science and Information Engineering National Cheng Kung University TAIWAN

    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
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 2

    Outline cont


    Blocking and Nonblocking

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 3

    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

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 4

    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
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 5

    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

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 6

    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

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 7

    Socket Attributes


    Socket Domains Socket Types




    Socket Protocols

    Stream Sockets Datagram Sockets

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 8

    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
    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 9

    Socket Attributes Socket Type cont


    Stream Sockets




    Datagram 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 are implemented in the AF INET domain by UDP IP connections and provide a non sequence unreliable service

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 10

    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 We ll concentrate on UNIX network and file system sockets which don t require you to choose a protocol other than the default

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 11

    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
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 12

    TCP IP Components Overview cont


    Transmission Control Protocol User Datagram Protocol Internet Protocol Internet Control Message Protocol

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 13

    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

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 14

    TCP Client Server Structure


    Client




    Server


    Set up socket Connect I O close Set up socket Bind socket Listen Accept I O
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 15

    TCP Client Server Structure cont

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 16

    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

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 17

    UDP Socket Function


    UDP




    Create and bind function socket and bind Connect interface connect I O function recvfrom and sendto

    Connectionless Unreliable Real time

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 18

    UDP Client Server Structure


    UDP client




    UDP sever


    Socket Sendto write Recvfrom read Socket Bind Recvfrom sendto

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 19

    UDP Client Server Structure cont

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 20

    Socket Example TCP Server
    void main sockfd socket AF INET SOCK STREAM 0 server address sun family AF INET server address sin addr s addr inet addr 127 0 0 1 server address sin port 9734 server len sizeof addres bind server sockfd 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 sockfd ch 1 ch write client sockfd close client sockfd Department of Computer Science and Information Engineering HANEL National Cheng Kung University TAIWAN 21

    Socket Example TCP Client cont
    void main char ch A sockfd socket AF INET SOCK STREAM 0 address sun family AF INET address sin addr s addr inet addr 127 0 0 1 address sin port 9734 len sizeof addres result connect sockfd struct sockaddr address if result 1 perror oops client1 exit 1 write sockfd ch 1 read sockfd ch 1 close sockfd HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 22

    Socket Example Socket Function cont
    include sys types h include sys socket h int socket int domain int type int protocol


    Domain




    Type


    AF UNIX AF INET AF ISO SOCK STREAM SOCK DGRAM
    UDP TCP



    Protocol


    0 default protocol setting
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 23

    Socket Example IPV4 Address Structure
    cont
    struct socketaddr in short int sin family unsigned short int sin port struct in addr sin addr struct in addr unsigned long int s addr


    AF INET Port number Internet address

    address sin family AF INET address sin addr s addr inet addr 127 0 0 1




    address sin port 9734 1234 or abcd
    HANEL

    inet addr this function translate IP address into specified format for socket

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 24

    Socket Example Bind Function cont
    include sys types h include sys socket h int bind int socket const struct sockaddr address size t address len




    The bind system call assigns an address to an unbound socket Socket




    Address


    File descriptor



    Address len
    HANEL

    Assign an address to its The length of sockaddr in
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 25

    Socket Example Listen and Connect cont


    Listening socket




    Connected socket


    Usually only one in server It will exit before server close Every client connect to server will built one I O with client When client close the socket will close

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 26

    Socket Example Listen Function cont
    include sys socket h int listen int socket int backlog


    socket




    backlog


    File descriptor The length of queue of unhandled request

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 27

    Socket Example Connect Function cont
    include sys socket h int connect int socket const struct sockaddr address size t address len


    socket




    address


    File descriptor Address of wanted to connection The length of sockaddr in



    address len


    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 28

    Socket Example I O Function cont
    include sys socket h int read int sockfd char buf intlen include sys socket h int write int sockfd char buf intlen


    sockfd




    buf


    File descriptor The location of preparing for reading data into or writing data out The length of above buf
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 29



    intlen


    Socket Example Sendto and Recvfrom cont


    Sendto

    include sys socket h int sendto intsocketfd void buff size tnbytes intflag Structsockaddr to socklen t addrlen



    Recvfrom

    include sys socket h int recvfrom int socketfd void buff size t nbytes int flag Structsockaddr from socklen t addrlen

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 30

    Blocking and Nonblocking


    Blocking synchronous socket




    Non blocking asynchronous 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 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 Department of Computer Science and Information Engineering HANEL
    National Cheng Kung University TAIWAN
    31

    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

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 32

    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
    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 33

    LAB2 UDP


    Exercise udpserv c


    Gcc o udpserv o server c Execute udpserv Enable service program netstat ln Part of output as below

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 34

    LAB2 UDP cont
    Active Internet connections only servers Proto Recv Q Send Q Local Address Foreign Address State tcp 0 0 0 0 0 0 32768 0 0 0 0 LISTEN Note1 If udp side has 0 0 0 0 8888 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
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 35

    HANEL

    LAB2 UDP cont
    Active client program Hello World Hello World this is a test this is a test d




    Note1


    Please show me what your result is

    If execute udpclient 127 0 0 1 again you will see the information as below test read error Connection refused
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 36

    LAB2 UDP cont


    Modify the server file to count the number of receiving for example receive 1 times receive 2 times

    HANEL

    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 37

    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 txt 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
    HANEL
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 38

    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 cnpaf net class UDP 0532918532729212 html
    Department of Computer Science and Information Engineering National Cheng Kung University TAIWAN 39

    http www cnds jhu edu courses cs111 lect8 600 111 img5 htm

    HANEL