Socket Programming Question:
Download Job Interview Questions and Answers PDF
How to Put a Host Program Address Together?
![Socket Programming Interview Answer Socket Programming Interview Question](https://globalguideline.com/public/images/interview/6.png)
Answer:
Once a host address and port number are known then the complete process address must be put together in a form that may be used by the system calls already covered. The structures set up to allow this follow:
#include <netinet/in.h>
/*
* Internet address
(a structure for historical reasons)
*/
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
#define s_addr S_un.S_addr
/* can be used for most tcp & ip code */
};
/*
* Socket address, internet style.
*/
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
Filling in the fields for sockaddr_in will produce an Internet version of a socket address.
#include <netinet/in.h>
/*
* Internet address
(a structure for historical reasons)
*/
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
#define s_addr S_un.S_addr
/* can be used for most tcp & ip code */
};
/*
* Socket address, internet style.
*/
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
Filling in the fields for sockaddr_in will produce an Internet version of a socket address.
Download Socket Programming Interview Questions And Answers
PDF
Previous Question | Next Question |
How to Selecte a Service Port? | How to finding a Machine Address? |