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 sockaddr_in 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 14 bytes to represent Sa_data, while sockaddr_in splits 14 bytes into
Sin_port, sin_addr, and Sin_zero
Sockaddr and sockaddr_in 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 sockaddr_in to represent addresses, sockaddr_in distinguish between
addresses and ports, and are more convenient to use.
include
struct sockaddr {
unsigned short sa_family; //2 bytes address family, AF_xxx
char sa_data[14]; //14 bytes of protocol address
};
// IPv4 AF_INET sockets:
struct sockaddr_in {
short sin_family; //2 bytes e.g. AF_INET, AF_INET6
unsigned short sin_port; //2 bytes e.g. htons(3490)
struct in_addr sin_addr; //4 bytes see struct in_addr, below
char sin_zero[8]; //8 bytes zero this if you want to
};
struct in_addr {
unsigned long s_addr; //4 bytes load with inet_pton()
};
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 error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc <2){
fprintf(stderr,"ERROR, no port provided
");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd <0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
/* erases the data in the n bytes of the memory*/
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Host to net byte order for short int*/
if (bind(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
error("ERROR on binding");
listen(sockfd,5);
/*5 outstanding connections*/
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd <0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd, buffer, 255);
if (n <0) error("ERROR reading from socket");
printf("Here is the message: %s
",buffer);
n = write(newsockfd, "I got your message", 18);
if (n <0) error("ERROR writing to socket");
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#include
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
/*The hostent structure is used by functions to store information about a
given host, such as host name, IPv4 address, and so forth*/
char buffer[256];
if (argc <3){
fprintf(stderr,"usage %s hostname port
", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd <0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
/* gethostname() system call returns a null-terminated hostname*/
if (server == NULL){
fprintf(stderr,"ERROR, no such host
");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255, stdin);
n = write(sockfd, buffer, strlen(buffer));
if (n <0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if (n <0)
error("ERROR reading from socket");
printf("%s
",buffer);
return 0;
}
Compilation
$ gcc -o server server.c
$ gcc -o client client.c
Execution
$ ./server 4444
Here is the message: CIS457 HI SERVER!!!!!
$ ./client 127.0.0.14444
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!