socketProgramming-TCP-and UDP-overview.pdf

Shilpachaudhari10 18 views 12 slides Oct 14, 2024
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Socket programming


Slide Content

Socket Programming

TCP Sockets

UDP Sockets

Socket
ints = socket(domain, type, protocol);
where
s: socket descriptor, an integer (like a file-handle)
domain: integer, communication domain
e.g.,AF_INET(IPv4 protocol)orAF_UNIX
type: communication type
SOCK_STREAM: reliable, 2-way, connection-based service
SOCK_DGRAM: unreliable, connectionless
protocol: e.g., TCP or UDP
use IPPROTO_TCP or IPPROTO_UDP to send/receive TCP or UDP packets
Example :s = socket(AF_INET, SOCK_STREAM, 0);
ints = socket(domain, type, protocol);
where
s: socket descriptor, an integer (like a file-handle)
domain: integer, communication domain
e.g.,AF_INET(IPv4 protocol)orAF_UNIX
type: communication type
SOCK_STREAM: reliable, 2-way, connection-based service
SOCK_DGRAM: unreliable, connectionless
protocol: e.g., TCP or UDP
use IPPROTO_TCP or IPPROTO_UDP to send/receive TCP or UDP packets
Example :s = socket(AF_INET, SOCK_STREAM, 0);

Bind
associatesan IP address andport for use by the socket
intstatus =bind(s,&addrport, size)
status: return status, 0 if successful,-1 otherwise
s:socket being used
addrport: address structureusessockaddr_in
size: thesize (in bytes) of theaddrportstructure
structsockaddr_in
{
sa_family_tsin_family; /* address family: AF_INET */
u_int16_tsin_port; /* port in network byte order */
structin_addrsin_addr; /* internet address*/
};
structin_addr/* Internet address */
{
u_int32_ts_addr; /* address in network byte order*/
};
associatesan IP address andport for use by the socket
intstatus =bind(s,&addrport, size)
status: return status, 0 if successful,-1 otherwise
s:socket being used
addrport: address structureusessockaddr_in
size: thesize (in bytes) of theaddrportstructure
structsockaddr_in
{
sa_family_tsin_family; /* address family: AF_INET */
u_int16_tsin_port; /* port in network byte order */
structin_addrsin_addr; /* internet address*/
};
structin_addr/* Internet address */
{
u_int32_ts_addr; /* address in network byte order*/
};

Listen
The listensystem callallows the process to listen on the socket for connections.
intstatus =listen(s,queuelen)
status: return value, 0 if listening,-1 if error
s:socket being used
queuelen: number of active participants that can“wait”for a connection
Example:listen(s, 5);
The listensystem callallows the process to listen on the socket for connections.
intstatus =listen(s,queuelen)
status: return value, 0 if listening,-1 if error
s:socket being used
queuelen: number of active participants that can“wait”for a connection
Example:listen(s, 5);

accept
Use the accept function to accept a connection request from a remotehost
The function returns a socket corresponding to the acceptedconnection
intns= accept(sock, &cliaddr, &addrlen)
ns: new socket used for data-transfer
sock: original socket being listened on (e.g., server)
cliaddr: address structure of the active participant (e.g., client)
The acceptfunction updates/returns thesockaddrstructure with the client's address
information
addrlen: size (in bytes) of the clientsockaddrstructure
The accept function updates/returns this value
Use the accept function to accept a connection request from a remotehost
The function returns a socket corresponding to the acceptedconnection
intns= accept(sock, &cliaddr, &addrlen)
ns: new socket used for data-transfer
sock: original socket being listened on (e.g., server)
cliaddr: address structure of the active participant (e.g., client)
The acceptfunction updates/returns thesockaddrstructure with the client's address
information
addrlen: size (in bytes) of the clientsockaddrstructure
The accept function updates/returns this value

Connect
The connect function is used by a client program to establish
communication with a remote entity
intstatus =connect(sock,&servaddr,addrlen);
status: return value, 0 if successful connect,-1 otherwise
sock: client’s socket to be used in connection
servaddr: server’s address structure
addrlen: size (in bytes) of theservaddrstructure
The connect function is used by a client program to establish
communication with a remote entity
intstatus =connect(sock,&servaddr,addrlen);
status: return value, 0 if successful connect,-1 otherwise
sock: client’s socket to be used in connection
servaddr: server’s address structure
addrlen: size (in bytes) of theservaddrstructure

Sending / Receiving Data
Send data
int count =send(int s, const void * msg, int len, unsigned int falgs);
Where:
count: number of bytes transmitted (-1 if error)
sock:socket being used
buf: buffer to be transmitted
len: length of buffer (in bytes) to transmit
flags: special options, usually just 0
Receive data
int count =recv(int s, void *buf, int len, unsigned int flags);
Where:
count: number of bytes received (-1 if error)
sock:socket being used
buf: stores received bytes
len: number of bytes received
flags: special options, usually just 0
Send data
int count =send(int s, const void * msg, int len, unsigned int falgs);
Where:
count: number of bytes transmitted (-1 if error)
sock:socket being used
buf: buffer to be transmitted
len: length of buffer (in bytes) to transmit
flags: special options, usually just 0
Receive data
int count =recv(int s, void *buf, int len, unsigned int flags);
Where:
count: number of bytes received (-1 if error)
sock:socket being used
buf: stores received bytes
len: number of bytes received
flags: special options, usually just 0

close
When finished using a socket, the socket should be closed:
status = close(s);
status: return value, 0 if successful,-1 if error
s: the file descriptor (socket being closed)

TCP echo Server TCP echo Client
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
intmain(intargc,char**argv) {
intsockfd,newsockfd,clength;
structsockaddr_inserv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nStart");
bind(sockfd,(structsockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nListening...");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(structsockaddr*)&cli_addr,&clength);
printf("\nAccepted");
read(newsockfd,buffer,4096);
printf("\nClientmessage:%s",buffer);
write(newsockfd,buffer,4096);
close(sockfd);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
intmain(intargc,char*argv[]) {
intsockfd;
structsockaddr_inserv_addr;
structhostent*server;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nReadyfor sending...");
connect(sockfd,(structsockaddr*) &serv_addr,sizeof(serv_addr));
printf("\nEnterthe message to send\n");
printf("\nClient: ");
fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
printf("Serverecho:%s",buffer);
close(sockfd);
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
intmain(intargc,char**argv) {
intsockfd,newsockfd,clength;
structsockaddr_inserv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nStart");
bind(sockfd,(structsockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nListening...");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(structsockaddr*)&cli_addr,&clength);
printf("\nAccepted");
read(newsockfd,buffer,4096);
printf("\nClientmessage:%s",buffer);
write(newsockfd,buffer,4096);
close(sockfd);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
intmain(intargc,char*argv[]) {
intsockfd;
structsockaddr_inserv_addr;
structhostent*server;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nReadyfor sending...");
connect(sockfd,(structsockaddr*) &serv_addr,sizeof(serv_addr));
printf("\nEnterthe message to send\n");
printf("\nClient: ");
fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
printf("Serverecho:%s",buffer);
close(sockfd);
return 0;
}

ENDEND
Tags