NW LAB 1. Basics of Socket Programming in C : TCP and UDP - Program Demo

Cracking Concepts by Kiran Mary Matthew
7 Jul 202227:59

Summary

TLDRThis video tutorial delves into the fundamentals of socket programming in C, showcasing TCP and UDP implementations. It explains sockets as endpoints for network communication, detailing their structure with IP addresses and port numbers. The video distinguishes between datagram and stream sockets, utilizing UDP and TCP respectively, and touches on raw sockets for security applications. It proceeds to illustrate the Berkeley Socket API, covering essential socket functions and their usage in client-server models. The script also includes practical examples of TCP and UDP server-client communication, demonstrating the process from socket creation to data transfer and closure.

Takeaways

  • 🔌 A socket is an endpoint for two-way communication between programs on a network, providing a bi-directional FIFO communication facility.
  • 📍 Each socket has a unique address composed of an IP address and a port number, crucial for establishing connections in client-server applications.
  • 🛠️ There are three main types of sockets: datagram (UDP), stream (TCP), and raw sockets, each with distinct transport layer protocols and use cases.
  • 🔗 TCP provides a connection-oriented, reliable, and bi-directional protocol with acknowledgements and retransmissions, ensuring data integrity.
  • 🌐 UDP is a connectionless protocol without the guarantees of order, acknowledgements, or duplicate protection, offering less overhead.
  • 🔍 Raw sockets allow for direct IP packet manipulation, used in security and diagnostic tools like Nmap, IGMP, and ICMP utilities.
  • 📚 Berkeley sockets, the focus of the script, use the Berkeley Socket API, which is demonstrated through various socket functions in the script.
  • 🔧 Key socket functions include socket creation, binding, listening (for servers), connecting (for clients), and data transfer via send/receive or sendto/recvfrom.
  • 📡 The script explains the importance of network byte order for port and address specifications in socket functions, contrasting with host byte order.
  • 🔄 Functions like htons and inet_addr are used for converting host data to network byte order and IP addresses to a format usable in socket operations.
  • 📝 The script outlines a step-by-step implementation of both TCP and UDP server and client programs, detailing the process of creating sockets, handling connections, and data transfer.
  • 🖥️ The video concludes with demonstrations of running TCP and UDP server and client programs, showing practical application of the discussed concepts.

Q & A

  • What is a socket in the context of network programming?

    -A socket is one endpoint of a two-way communication link between two programs running on a network, providing a bi-directional FIFO (First In, First Out) communication facility over the network.

  • What is the purpose of the 'socket' function in C?

    -The 'socket' function in C is used to create an endpoint for communication and returns a file descriptor that refers to that endpoint.

  • What are the two main types of sockets used in transport layer protocols?

    -The two main types of sockets used in transport layer protocols are datagram sockets, which use UDP, and stream sockets, which use TCP.

  • What is the difference between TCP and UDP protocols?

    -TCP (Transmission Control Protocol) is a connection-oriented protocol that is reliable and bi-directional, ensuring data integrity with acknowledgements and retransmissions. UDP (User Datagram Protocol) is a connectionless protocol that does not guarantee data delivery, allowing packets to arrive out of order and duplicates to occur.

  • What is a raw socket and what is it used for?

    -A raw socket allows direct sending and receiving of IP packets without any protocol-specific transport layer formatting. It is used in security-related applications like network scanners (e.g., nmap) and in routing protocols like IGMP and ICMP.

  • What are the three arguments of the 'bind' function in socket programming?

    -The 'bind' function has three arguments: the socket descriptor, a pointer to the 'sockaddr_in' structure, and the length of that structure.

  • What is the purpose of the 'listen' function in TCP socket programming?

    -The 'listen' function marks a socket as a passive socket that will be used to accept incoming connection requests, with a specified maximum length for the queue of pending connections.

  • How does the 'connect' function in socket programming work?

    -The 'connect' function attempts to establish a connection to a specified socket. If successful, it returns 0; otherwise, it returns -1 indicating an error.

  • What is the role of the 'accept' function in server-side TCP socket programming?

    -The 'accept' function is used on the server side to extract the first connection request from the queue of pending connections for the listening socket and create a new connected socket, returning a new file descriptor for the connection.

  • What are the two flags mentioned for the 'send' function in TCP socket programming?

    -The two flags mentioned for the 'send' function are 'MSG_MORE', which indicates that the caller has more data to send, and 'MSG_EOF', which is used for terminating a record.

  • What is the difference between the 'send' and 'sendto' functions in socket programming?

    -The 'send' function is used in connection-oriented protocols like TCP, while 'sendto' is used in connectionless protocols like UDP. 'sendto' requires additional parameters for the destination address and its length.

  • What is the purpose of the 'inet_addr' function in socket programming?

    -The 'inet_addr' function converts a string in standard IPv4 dotted decimal notation to an integer value suitable for use as an Internet address.

  • What is the significance of network byte order in socket programming?

    -Network byte order is a method of sorting bytes that is independent of specific machine architecture and is used to ensure that data is transmitted in a consistent format across different systems. It is always defined to be big endian.

  • What is the role of the 'close' function in socket programming?

    -The 'close' function is used to deallocate the file descriptor associated with a socket. If executed successfully, it returns 0; otherwise, it returns -1 indicating an error.

Outlines

00:00

🔌 Introduction to Socket Programming in C

This paragraph introduces the concept of socket programming in C, explaining that a socket is an endpoint for two-way communication between programs over a network. It discusses the creation of sockets at each end of the communication link, their specific addresses composed of an IP address and port number, and their role in client-server applications. The paragraph also distinguishes between different types of sockets, such as datagram, stream, and raw sockets, and the protocols they use, namely UDP and TCP for datagram and stream sockets, respectively. Raw sockets are highlighted for their use in security applications, and the Berkeley Socket API is introduced as the basis for the subsequent discussion.

05:01

📚 Socket Functions and Their Usage

The second paragraph delves into the specific functions used in socket programming, focusing on the server and client perspectives for both TCP and UDP protocols. It outlines the functions 'socket', 'bind', 'listen', 'accept', 'connect', 'send', 'receive', and 'close', explaining their purpose and parameters. For TCP, the server uses 'bind', 'listen', and 'accept', while the client uses 'connect'. For UDP, both server and client use 'socket', 'bind', 'sendto', 'recvfrom', and 'close'. The paragraph also describes the 'sockaddr_in' structure for storing address information and the importance of network byte order in socket function calls.

10:03

📝 Detailed Explanation of TCP/IP Functions

This paragraph provides a detailed explanation of the functions used in TCP and UDP communication, including 'send', 'sendto', 'receive', and 'recvfrom'. It discusses the parameters of these functions and the significance of flags in controlling message transmission. The paragraph also introduces auxiliary functions like 'htons' for converting to network byte order, 'inet_addr' for converting IP addresses, and 'strcpy' for string copying. It highlights the structures used for storing address information and the difference between network and host byte order, emphasizing the use of big-endian byte order for network communication.

15:03

🖥️ Implementation of TCP Server and Client Programs

The fourth paragraph outlines the implementation of a TCP server and client program. It describes the process of creating a socket, binding it to an address, listening for connections, accepting client requests, and transferring data using 'send' and 'receive' functions. The paragraph details the steps involved in initializing socket structures, converting port numbers, binding to localhost, and handling data transfer between the server and client. It also explains the use of 'inet_addr' for IP address conversion and the importance of closing the socket after communication is complete.

20:04

🌐 UDP Server and Client Program Implementation

This paragraph focuses on the implementation of UDP server and client programs. It explains the process of creating a UDP socket, binding it, and entering a listening mode to receive messages from the client. The paragraph details the use of 'recvfrom' and 'sendto' functions for message reception and transmission, including handling the source and destination addresses. It also discusses error handling, message sending, and closing the socket, providing a step-by-step guide for both server and client sides of a UDP communication.

25:04

🔄 Running TCP and UDP Programs for Demonstration

The final paragraph concludes the script by demonstrating the running of TCP and UDP server and client programs. It describes the process of compiling and running both server and client programs in separate terminals, emphasizing the need for two terminals since the programs run on the same system. The paragraph illustrates the interaction between the server and client, showing how messages are sent and received, and concludes with a thank you note to the viewers for watching the video.

Mindmap

Keywords

💡Socket

A socket is an endpoint of a two-way communication link between two programs running on a network. In the context of the video, it provides a bi-directional FIFO (First In, First Out) communication facility. The script explains that sockets are essential in client-server applications where the server creates a socket, attaches it to a network port, and waits for a client to connect. The client then creates a socket and attempts to connect to the server's socket, facilitating data transfer.

💡TCP (Transmission Control Protocol)

TCP is a connection-oriented protocol that ensures reliable and bi-directional communication over the network. The video describes it as a protocol used with stream sockets, which guarantees that data packets arrive in the correct order with acknowledgments and retransmissions if needed. This is in contrast to UDP, which is a connectionless protocol.

💡UDP (User Datagram Protocol)

UDP is a connectionless protocol that operates at the transport layer and is used with datagram sockets. The script mentions that UDP does not guarantee packet order, acknowledgments, or retransmissions, which makes it suitable for applications where speed is more critical than reliability, such as streaming media.

💡IP Address

An IP address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. The video script explains that each socket has a specific address composed of an IP address and a port number, which is crucial for establishing connections between networked programs.

💡Port Number

A port number is a 16-bit unsigned integer used to identify specific processes within an IP address. The script clarifies that along with the IP address, the port number uniquely identifies a socket and is used in conjunction with the IP address to establish a connection between a client and a server.

💡Berkeley Sockets

Berkeley Sockets refers to the socket API developed at the University of California, Berkeley. The video script mentions that the sockets being discussed are Berkeley Sockets, which are widely used for network programming and are based on the BSD Unix network programming library.

💡Socket Functions

Socket functions are the set of functions used to create, configure, and manage sockets for network communication. The script details various socket functions such as 'socket', 'bind', 'listen', 'accept', 'connect', 'send', 'receive', and 'close', which are essential for both TCP and UDP communication.

💡Data Transfer

Data transfer refers to the movement of data from one location to another over a network. In the video, data transfer is the primary purpose of establishing a socket connection, where the 'send' and 'receive' functions are used to transmit data between the client and the server.

💡Network Byte Order

Network byte order is the standard byte ordering used for transmitting data over a network, which is always big-endian. The script explains that ports and addresses are specified in network byte order when calling socket functions, ensuring consistency across different architectures.

💡Host Byte Order

Host byte order refers to the byte ordering used by a computer's hardware and software, which can be either little-endian or big-endian. The video script contrasts host byte order with network byte order, highlighting the need for conversion functions like 'htons' to ensure compatibility when transmitting data over a network.

💡Raw Sockets

Raw sockets allow direct sending and receiving of IP packets without any protocol-specific transport layer formatting. The script mentions that raw sockets are used in security-related applications like Nmap and in routing protocols like IGMP and ICMP, which are used for network scanning and control messaging.

Highlights

Introduction to the basics of socket programming in C.

Explanation of sockets as endpoints for network communication with bi-directional FIFO capabilities.

Socket address composition including IP address and port number.

Role of sockets in client-server applications with servers waiting for client connections.

Different types of sockets: datagram, stream, and raw sockets.

UDP and TCP protocols associated with datagram and stream sockets respectively.

TCP's reliability and bi-directionality contrasted with UDP's connectionless nature.

Raw sockets' use in security applications like Nmap and network protocols like IGMP and ICMP.

Berkeley sockets and the use of Berkeley Socket API for implementation.

Illustration of host communication through sockets and routers.

Detailed discussion of socket functions used in both TCP and UDP.

Function definitions for socket creation, binding, listening, connecting, accepting, sending, and receiving.

Use of sys/socket.h header file for socket functions.

Explanation of network byte order and its contrast with host byte order.

Functions htons and inet_addr for network byte order conversion and IP address interpretation.

Implementation of TCP server and client programs with code walkthrough.

Implementation of UDP server and client programs with code walkthrough.

Demonstration of running TCP and UDP server and client programs.

Conclusion and thanks for watching the networking lab video.

Transcripts

play00:03

hello all welcome to this video on

play00:06

networking lab today i'll be talking

play00:08

about the basics of socket programming

play00:10

in c

play00:12

we also show the implementation of the

play00:14

same using tcp and udp

play00:17

let's begin with the topic socket

play00:23

now a socket is

play00:26

one end point of a two-way communication

play00:28

link between two programs running on the

play00:30

network

play00:31

the socket provides bi-directional fifo

play00:33

communication facility over the network

play00:36

a socket connecting to the network is

play00:38

created at each end of the communication

play00:41

each socket has a specific address this

play00:44

address is composed of an iep address

play00:46

and a port number

play00:48

sockets are generally employed in client

play00:50

server applications the servers

play00:53

create a socket attaches it to a network

play00:56

port address and then waits for the

play00:57

client to contact it

play00:59

the client creates a socket and then

play01:01

attempts to connect to the server socket

play01:04

when the connection is established then

play01:06

the transfer of data takes place

play01:11

there are

play01:12

different types of sockets available

play01:17

like datagram socket

play01:19

stream socket and row sockets

play01:24

now datagram socket and

play01:27

stream sockets

play01:29

they vary in the transport layer

play01:31

protocol that they use datagram sockets

play01:33

use udp and stream sockets use tcp

play01:37

tcp is a connection oriented protocol

play01:40

which is reliable and bi-directional

play01:43

whereas udp is a connectionless protocol

play01:46

which has

play01:47

no acknowledgements no retransmission

play01:50

where packets might arrive out of order

play01:53

and duplicates are possible

play01:57

then we have the third type of socket

play02:00

which is raw sockets

play02:02

they allow direct sending and receiving

play02:04

of ip packets without any protocol

play02:06

specific transport layer formatting

play02:09

they are used in security related

play02:11

applications like nmap

play02:14

which is

play02:16

a network scanner that is used to

play02:18

discover hosts and services on a

play02:20

computer network by sending packets and

play02:22

analyzing their responses

play02:24

it is also used in

play02:28

routing protocols like igmp which is

play02:31

internet group management protocol

play02:34

and in icmp which is internet control

play02:37

message protocol

play02:39

that is used in the ping utility

play02:44

the sockets that we are creating here

play02:45

are called the berkeley sockets so we'll

play02:47

be using the berkeley socket api for the

play02:50

same

play02:52

this picture shows how

play02:55

two hosts

play02:56

are communicating with each other

play02:59

through the sockets now each socket will

play03:01

have

play03:02

its ip address and a port number

play03:06

which is connected to each other through

play03:08

a router

play03:12

now we look into detail

play03:13

into the various socket functions that

play03:16

we'll be using here the diagram here

play03:19

shows the different functions that are

play03:21

used both in tcp and udp both at the

play03:24

server and the client side

play03:27

in tcp

play03:30

both server and client use the functions

play03:32

socket

play03:34

send receive and close

play03:36

now the functions bind

play03:38

listen and accept are

play03:41

for server in tcp and for client there's

play03:45

a function called connect

play03:48

as in the case of udp the server and the

play03:51

client have functions socket bind

play03:55

send to receive from and close

play04:00

now we'll start by discussing about the

play04:02

function which is socket

play04:09

also note that all the

play04:12

functions that we will be discussing

play04:14

from now are defined in the header file

play04:17

which is sys socket dot h

play04:22

now looking into the function

play04:25

in sock id

play04:28

socket

play04:30

int domain and type and end protocol

play04:34

now this function socket will create an

play04:37

end point for communication

play04:41

and returns a file descriptor that

play04:43

refers to that endpoint

play04:46

now this file descriptor returned by a

play04:48

successful call will be the lowest

play04:50

numbered file descriptor that is not

play04:52

currently opened for any process

play04:59

now we look into the domain that's the

play05:01

first argument

play05:02

so domain will specify

play05:04

the communication domain so in this case

play05:07

you can have different options i have

play05:10

shown two options here one is af inet

play05:13

which is used for ipv4 internet protocol

play05:16

and af unix which is used for local

play05:20

communication

play05:22

then the second argument is type which

play05:25

is used to refer to communication

play05:27

semantics that too we are using

play05:30

the two commonly used options which is

play05:33

soft stream

play05:35

that is

play05:36

used for

play05:38

tcp connections which is sequenced

play05:41

reliable

play05:42

and

play05:44

two-way connection based

play05:48

then we have sock d gram which is used

play05:51

in udp which supports datagrams

play05:55

then the third option is

play05:57

protocol

play05:59

now the protocol will

play06:02

specify the particular protocol that is

play06:04

to be used with the socket normally only

play06:06

a single protocol exists to support a

play06:08

particular socket type within a given

play06:10

protocol family in which case the

play06:13

protocol field can be specified as

play06:16

0.

play06:19

now the status here is referring to the

play06:21

return value of this function now if it

play06:24

is successful then a value

play06:28

which is

play06:31

returned will be the

play06:36

lowest

play06:39

file descriptor number as we have seen

play06:42

and if this value goes below zero that's

play06:45

that is if it is minus one it means that

play06:47

there was an error in it

play06:50

now we look into the next function which

play06:52

is bind

play06:54

now here bind

play06:58

has

play07:00

three arguments

play07:03

which are the socket descriptor followed

play07:06

by a pointer to the

play07:09

sock header in structure and the length

play07:11

of that particular

play07:14

structure

play07:16

now this bind will assign the address

play07:18

specified by the

play07:20

second argument here to the socket

play07:22

referred to the first argument

play07:27

and we can also see this will store the

play07:30

status of return value

play07:33

and if it is

play07:34

minus 1 it means there was a failure

play07:37

else it will always return the value of

play07:41

0.

play07:45

then we will move on to the next

play07:46

function which is listen

play07:52

now the listen function has two

play07:53

arguments which is

play07:58

saw kefny and backlog now listen marks

play08:01

the socket referred to by sock fd as a

play08:04

passive socket that is as a socket that

play08:06

will be used to accept the incoming

play08:08

connection request

play08:10

and the second argument backlog

play08:14

defines the maximum length to which the

play08:16

queue of pending connections can grow

play08:19

now on successful execution of this

play08:23

function it will return a zero

play08:26

else it will return minus 1.

play08:30

now we look into the next function so

play08:34

here

play08:35

first the client will be sending a

play08:37

connect request to the server then it

play08:40

will

play08:41

execute the accept function first we

play08:44

will see the connect function so looking

play08:46

into connect function we have

play08:51

three arguments here the

play08:53

file descriptor

play08:54

then the

play08:56

pointer to the address structure and its

play08:59

length

play09:03

now

play09:06

the connect function shall attempt to

play09:08

make a connection to the particular

play09:11

socket

play09:13

now if the connection was successful

play09:16

then it will return a zero else it will

play09:19

return a minus one

play09:24

next we will look into the function

play09:26

accept which is run at the server end so

play09:30

this is done when a request is received

play09:32

from the client

play09:35

it has arguments the file descriptor

play09:38

then a pointer to the socket ring

play09:40

structure and its length

play09:48

now this will extract the first

play09:50

connection request on the queue of

play09:52

pending connections for the listening

play09:53

socket

play09:54

and create a new connected socket and

play09:58

return the new file descriptor referring

play10:01

to that socket

play10:02

now that is the value that is stored in

play10:05

this

play10:06

variable s

play10:11

next we look into the

play10:13

functions that are used for sending and

play10:15

receiving data at tcp and udp side

play10:19

first we will move to the send function

play10:22

that is used by

play10:23

tcp

play10:25

now send will have the arguments the

play10:27

file descriptor

play10:29

the buffer where the data to be sent is

play10:32

stored

play10:33

the length of

play10:34

the data and flags

play10:40

now this flags can be the or

play10:43

of zero or more flags like

play10:46

there are some options for available for

play10:49

it

play10:50

so we will see two of them

play10:53

one of them is message mode which is

play10:55

used to say that the caller has more

play10:58

data to send and the other one is

play10:59

message eor which is

play11:02

used for terminating a record

play11:06

now the return value here it is called

play11:08

count which will contain the number of

play11:11

bytes that is sent if there is an error

play11:13

it will return a minus one value

play11:16

now we will see send to that is used in

play11:19

datagram sockets which is udp

play11:23

here also we have the socket descriptor

play11:25

buffer buffer size flags if any

play11:30

and then we have

play11:32

the destination address pointer and the

play11:35

length of the destination address so

play11:37

those two will be the extra fields that

play11:39

we added when you use

play11:41

send to

play11:45

then we have

play11:46

the receive function

play11:48

here also we'll have a file descriptor

play11:51

then the

play11:53

buffer length of the buffer and flags

play11:56

used

play11:57

so here also we are discussing two flags

play12:00

that are used and received

play12:03

now which are

play12:10

message

play12:11

oob that is used to indicate

play12:14

receiving out of band data

play12:19

and then we have

play12:20

message peak

play12:22

which causes the receive operation to

play12:24

return data from the beginning of the

play12:26

received queue without removing that

play12:28

data from the queue

play12:31

here also we have a count variable where

play12:33

the number of bytes received will be

play12:35

stored else

play12:37

that is if there is an error the value

play12:39

will be minus 1.

play12:42

in case of datagram socket it is

play12:43

received from so there will be two extra

play12:46

fields where we have the

play12:48

source address and source address length

play12:51

all the rest will be the same for both

play12:57

now we are going to discuss the

play13:01

functions

play13:02

and the structures that will be using in

play13:05

the implementation

play13:06

of the program that we are going to see

play13:08

shortly

play13:11

so first of all we will be using a

play13:13

structure called sock header in

play13:15

where we will be using the peer address

play13:19

that is used to store the peer address

play13:21

so it is declared in the header file

play13:23

that net inet slash in dot h

play13:26

so this is this format

play13:29

it has

play13:30

three

play13:32

members in it

play13:34

the syn family member is referring to

play13:36

the address family which is

play13:38

in our case afinit

play13:41

then we have sin underscore port which

play13:44

is

play13:44

used to store the port in network byte

play13:47

order

play13:48

then we have the third element which is

play13:50

a structure that is used to store the

play13:53

internet address

play13:54

now the third

play13:57

structure which is in adder

play13:59

contains the element s

play14:01

so that is where the address in network

play14:04

byte order will be stored

play14:06

now we will look into what this network

play14:09

byte order is

play14:14

now the ports and addresses are always

play14:16

specified in calls to the socket

play14:18

functions using the network byte order

play14:20

convention

play14:22

this convention is a method of sorting

play14:24

bytes that is independent of specific

play14:26

machine architecture

play14:28

now host byte order on the other hand

play14:31

sorts bytes in the manner which is most

play14:33

natural to the host

play14:35

software and hardware

play14:37

there are two common host byte methods

play14:40

that are used one is little indian and

play14:42

the other is big indian

play14:44

little indian bite ordering places the

play14:46

least significant bite first followed by

play14:49

the next higher ones

play14:51

and big endian bite ordering places the

play14:53

most significant bite first followed by

play14:56

the

play14:57

less higher ones after that

play15:00

the network byte order is defined always

play15:03

to be big endian

play15:07

next we will be using a function called

play15:09

h tones which is defined either in r

play15:14

slash iron dot h or net inet slashing

play15:17

dot h now this depends on different

play15:19

systems so in the program we are going

play15:21

to discuss i will be including both the

play15:23

header files

play15:30

now

play15:33

we'll be looking what h stance is used

play15:35

for it is used to translate an unsigned

play15:38

short integer into its

play15:41

network byte order

play15:45

then we will be using the function i net

play15:48

adder which is

play15:49

defined in the header file rpa slash

play15:51

init dot h

play15:56

now in editor is a function that will

play15:58

convert the string which is pointed to

play16:00

by

play16:01

cp variable

play16:04

in the standard ipv4 dotted decimal

play16:07

notation to an integer value that is

play16:09

suitable for use as an internet address

play16:13

so in our case this address will be

play16:16

127.0.0.1 which is the localhost or

play16:19

loopback address

play16:21

then we will be also using our structure

play16:24

sock header storage which is defined in

play16:26

slash socket.h

play16:30

now this structure

play16:33

is large enough to accommodate all

play16:35

supported protocol specific address

play16:37

structures

play16:41

then we use a type called socklin

play16:45

underscore team

play16:50

now

play16:51

this is an integer type of width of at

play16:54

least 32 bits

play16:57

then we use the close function which is

play16:59

defined in

play17:02

uni std dot h that is

play17:06

used to

play17:09

deallocate the file descriptor

play17:13

and if it is

play17:15

successfully executed then 0 shall be

play17:17

returned else it is -1 then we have

play17:21

string copy and get s of string.h header

play17:25

file

play17:26

string copy is used to copy the value of

play17:28

string from source to destination gettus

play17:30

is used to get the value from standard

play17:33

input which is the keyboard

play17:36

we will be also using the function exit

play17:38

which is defined in

play17:40

stdlib dot h

play17:42

now it is used to show

play17:46

the exit status so in our case we are

play17:49

using it to show the abnormal

play17:52

termination so i'll be using exit and

play17:56

the

play17:56

status value as

play17:58

1.

play18:00

now we'll look into the implementation

play18:03

first we'll be going through the tcp

play18:05

server program i've included all the

play18:07

header files that are to be used

play18:10

then i'll define

play18:12

a file descriptor server

play18:14

to which the return value of socket

play18:17

function is stored since it's tcp and

play18:19

ipv4 i'm including a finite stock stream

play18:23

and since ip protocol is used i'll

play18:25

include

play18:26

0. then i'll declare a variable for soc

play18:30

header in

play18:31

structure which is server and i'll

play18:33

initialize the three values just family

play18:36

afi net

play18:37

and port number i'll convert this port

play18:41

into the

play18:43

required format using the function h

play18:45

stones

play18:46

then the third

play18:48

member which is a structure

play18:51

i'll take the local host address and

play18:53

convert it using init editor to the

play18:56

internet address and store it there

play19:00

then i'll be binding it that is done by

play19:03

including the file descriptor and a

play19:05

pointer to the socket structure that we

play19:07

created just now and its size

play19:10

then after binding the server enters

play19:12

into listening mode so listen off the

play19:15

file descriptor comma the backlog length

play19:18

which in my case is five now if this is

play19:21

successful we'll print the message that

play19:23

is listening else will print that there

play19:25

is an error

play19:27

now after listening the request is

play19:30

received from the client and the server

play19:32

will be accepting it so

play19:35

accept

play19:36

will contain the file descriptor and the

play19:39

pointer to the structure and its length

play19:42

now this will be the new file descriptor

play19:45

through which data will be

play19:46

transferred

play19:48

now since it's the server it is waiting

play19:50

to receive a message from the client so

play19:52

since it's tcp i'll use the receive

play19:55

function

play19:56

the file descriptor buffer

play19:58

buffer size is

play20:01

given as 1024. that is taken with the

play20:04

assumption that the data transfer

play20:06

size will be 1 kb

play20:10

now i am giving the full size of buffer

play20:12

since i am unaware of the size of data

play20:15

from the client and 0 because there are

play20:18

no flags used then i'm printing the data

play20:20

i received

play20:23

that is stored in buffer

play20:25

then i'll copy the data i need to send

play20:28

to the client to buffer using string

play20:30

copy

play20:31

then i'll print a message showing that i

play20:33

am sending data to client then i will

play20:36

send it using the send function with the

play20:38

file descriptor buffer and the size of

play20:40

the string that i stored in buffer and 0

play20:43

because there are no flags after all

play20:45

this communication is over i'll close

play20:48

both the listening socket and the socket

play20:51

use for data transfer now looking into

play20:53

the tcp client

play20:56

we'll use

play20:57

the same steps

play21:00

where i had the client file descriptor

play21:04

and the

play21:05

sock adder in

play21:07

structure which are initialized with

play21:09

these values

play21:11

now

play21:12

next step is to

play21:14

send a connection request to the server

play21:16

so i'll include the file descriptor

play21:18

pointer to the structure and the size of

play21:21

the structure then i'll print that i'm

play21:23

sending a message to the server

play21:27

then i'll take the string copy to buffer

play21:29

and send it to the

play21:31

server

play21:32

mentioning the file descriptor buffer

play21:34

size of the string and 0 because there

play21:36

are no flags

play21:38

now the client is waiting for a message

play21:40

from the server

play21:42

for that you will be using the receive

play21:44

function you have the file descriptor

play21:46

buffer

play21:47

full size of buffer because i don't know

play21:49

the size of data that is coming from the

play21:51

server side and 0 because no flags are

play21:53

used then i'll print the receive data

play21:55

and finally close the

play21:58

socket

play22:03

next we will see the implementation for

play22:05

udp server and client we'll begin with

play22:07

udp server i have included all the

play22:09

header files

play22:11

here i'll have two variables

play22:14

for socket or instructure and two

play22:16

message

play22:17

variables too

play22:19

then i'll store

play22:20

the client address size to a variable

play22:23

now this is optional you can either give

play22:25

this directly in the function also

play22:28

here

play22:29

i am creating a udp

play22:32

socket so ipv4 refine it soft drum

play22:36

because it's udp

play22:37

and this option here

play22:40

is similar to the zero we saw before so

play22:43

if you are using ip protocol you can

play22:45

either give it as 0

play22:47

or you can give it

play22:49

specifically like this since its udp i

play22:52

gave it as ip proto underscore udp if

play22:54

it's tcp you can give the option ip

play22:57

proto underscore tc

play23:00

now i am checking if

play23:02

this server value is less than 0 it

play23:04

means there is an error and i will give

play23:06

an abnormal termination

play23:08

otherwise what i'll do is i'll print the

play23:11

message socket was created successfully

play23:14

then i will initialize these members of

play23:16

this structure

play23:18

as we did before so i'll be using a

play23:20

different

play23:21

port number here

play23:23

then i'll do the binding taking the file

play23:25

descriptor pointer to the structure and

play23:28

its size now if this is less than 0 then

play23:30

the binding has not happened and there

play23:32

is an abnormal termination otherwise the

play23:35

binding is done then the server enters

play23:37

into a listening mode

play23:39

then the server is waiting for a message

play23:40

from the client so it is received from

play23:43

function

play23:44

the file descriptor variable where the

play23:47

data

play23:48

receivably stored size of it

play23:51

no flags so it's zero

play23:53

then the address of the

play23:56

source and its length now if this is

play23:58

less than zero again there was some

play24:00

error abnormal termination else what it

play24:03

does is it will

play24:05

print the message received

play24:08

and you can copy it to another variable

play24:11

using string copy

play24:12

then i need to send a message to the

play24:14

client so i'll use the send to function

play24:17

i'll mention the file descriptor the

play24:19

message variable its length no flags so

play24:23

it is 0

play24:24

then the address of the sender and size

play24:27

of the address now if it is less than 0

play24:30

then there is an error abnormal

play24:31

termination else the message will be

play24:34

printed at the other side and finally

play24:36

i'll close the socket

play24:39

now looking at the client side the same

play24:42

thing is done

play24:45

creating the file descriptor checking if

play24:47

it's less than zero if it is less

play24:51

then it's terminated

play24:53

otherwise successfully created

play24:55

initializing the structure variables

play24:58

then asking the client to enter a

play25:00

message that is to be sent to the server

play25:04

and reading it through the

play25:06

get this function

play25:09

then we'll have the send to function to

play25:11

send the message to the udp server

play25:15

file descriptor message variable size

play25:17

flag

play25:19

then the

play25:23

destination address its length if less

play25:26

than 0

play25:27

then abnormal termination

play25:30

else

play25:31

it will be waiting for receiving message

play25:34

from the service side same thing as

play25:36

server then if there is an error it is

play25:38

printed else the socket is closed

play25:47

now we'll be looking into

play25:49

the

play25:51

tcp implementation

play25:53

now we have the programs for tcp server

play25:58

which we discussed before

play26:02

then we have the program for tcp client

play26:09

since we are running both on the same

play26:10

system we need two terminals so i'll be

play26:13

running it

play26:14

at the same time one for server and one

play26:16

for client i'll compile the server

play26:19

program

play26:21

then i'll run it first so it will be in

play26:23

the listening mode then i'll combine

play26:26

the

play26:28

client program and run it

play26:33

so it will send the message

play26:42

now here

play26:44

we have

play26:46

the message already set in the program

play26:49

as we have discussed

play26:59

now we look into the same for udp

play27:01

implementation

play27:03

here also we'll have two terminals

play27:06

first of all we'll run the

play27:10

server program we'll first compile it

play27:12

and run it

play27:18

so it will be waiting for the client

play27:20

message

play27:24

then we'll do the same for the client

play27:25

compile it and run it

play27:33

there's a warning i'm ignoring it for

play27:35

the time being

play27:37

now after running the program it is

play27:40

asking me to enter a message to be sent

play27:42

to server i'm sending the message hi

play27:44

so that is print

play27:46

at the server side also

play27:55

that's all for now thank you for

play27:56

watching

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
Socket ProgrammingC LanguageTCP/IPUDPNetworkingClient-ServerBi-directional CommunicationData TransferProtocolsRaw Sockets
Besoin d'un résumé en anglais ?