Question: Structures sockaddr and sockaddr _ in are used to handle the addresses of network traffic. In various system calls or functions, these two structures are
Structures sockaddr and sockaddrin are used to handle the addresses of network traffic. In various
system calls or functions, these two structures are used whenever dealing with network addresses.
SOCKADDR uses the remaining bytes to represent Sadata, while sockaddrin splits bytes into
Sinport, sinaddr, and Sinzero
Sockaddr and sockaddrin contain the same data, but they differ in their use:
Programmers should not operate SOCKADDR, as it is for the operating system.
Programmers should use sockaddrin to represent addresses, sockaddrin distinguish between
addresses and ports, and are more convenient to use.
include
struct sockaddr
unsigned short safamily; bytes address family, AFxxx
char sadata; bytes of protocol address
;
IPv AFINET sockets:
struct sockaddrin
short sinfamily; bytes eg AFINET, AFINET
unsigned short sinport; bytes eg htons
struct inaddr sinaddr; bytes see struct inaddr, below
char sinzero; bytes zero this if you want to
;
struct inaddr
unsigned long saddr; bytes load with inetpton
;
server.c
A simple server in the internet domain using TCP
The port number is passed as an argument
#include
#include
#include
#include
#include
#include
#include
void errorchar msg
perrormsg;
exit;
int mainint argc, char argv
int sockfd, newsockfd, portno, clilen;
char buffer;
struct sockaddrin servaddr, cliaddr;
int n;
if argc
fprintfstderr"ERROR, no port provided
;
exit;
sockfd socketAFINET, SOCKSTREAM, ;
if sockfd
errorERROR opening socket";
bzerochar &servaddr, sizeofservaddr;
erases the data in the n bytes of the memory
portno atoiargv;
servaddr.sinfamily AFINET;
servaddr.sinaddr.saddr INADDRANY;
servaddr.sinport htonsportno;
Host to net byte order for short int
if bindsockfdstruct sockaddr &servaddr, sizeofservaddr
errorERROR on binding";
listensockfd;
outstanding connections
clilen sizeofcliaddr;
newsockfd acceptsockfdstruct sockaddr &cliaddr, &clilen;
if newsockfd
errorERROR on accept";
bzerobuffer;
n readnewsockfd buffer, ;
if n errorERROR reading from socket";
printfHere is the message: s
buffer;
n writenewsockfd "I got your message", ;
if n errorERROR writing to socket";
return ;
client.c
#include
#include
#include
#include
#include
#include
#include
#include
void errorchar msg
perrormsg;
exit;
int mainint argc, char argv
int sockfd, portno, n;
struct sockaddrin servaddr;
struct hostent server;
The hostent structure is used by functions to store information about a
given host, such as host name, IPv address, and so forth
char buffer;
if argc
fprintfstderr"usage s hostname port
argv;
exit;
portno atoiargv;
sockfd socketAFINET, SOCKSTREAM, ;
if sockfd
errorERROR opening socket";
server gethostbynameargv;
gethostname system call returns a nullterminated hostname
if server NULL
fprintfstderr"ERROR, no such host
;
exit;
bzerochar &servaddr, sizeofservaddr;
servaddr.sinfamily AFINET;
bcopychar serverhaddr,
char &servaddr.sinaddr.saddr,
serverhlength;
servaddr.sinport htonsportno;
if connectsockfdstruct sockaddr &servaddr,sizeofservaddr
errorERROR connecting";
printfPlease enter the message: ;
bzerobuffer;
fgetsbuffer stdin;
n writesockfd buffer, strlenbuffer;
if n
errorERROR writing to socket";
bzerobuffer;
n readsockfd buffer, ;
if n
errorERROR reading from socket";
printfs
buffer;
return ;
Compilation
$ gcc o server server.c
$ gcc o client client.c
Execution
$ server
Here is the message: CIS HI SERVER!!!!!
$ client
Please enter the message: Hi Server, Good morning!!!!!!
I got your message
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
