Professional Documents
Culture Documents
Connection oriented
Rely on TCP to provide reliable two way
connectedcommunication
The server is listening and waiting for a connection from the cli-
ent.these server can process 10 clients by using child processes for
each new client.
When the client connects to the server the server creates a new file
descriptor using accept() and creates a new child process using
fork()
Now using send() the server sends a “ Hello,World!” message to the
client
which is accepted by the client using recv()
Now the client displays this “Hello,World!” message on the screen
If there is a error in any of these processes than a error message is
displayed
Basic server operation
struct sockaddr_in {
struct sockaddr:
Holds socket address information for
many types of sockets
struct sockaddr {
unsigned short sa_family; //address family AF_xxx
unsigned short sa_data[14]; //14 bytes of protocol addr
}
struct sockaddr_in:
A parallel structure that makes it
easy to reference elements of the socket address
struct sockaddr_in {
short int sin_family; // set to AF_INET
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; //set to all zeros
}
cont..
struct addrinfo
struct addrinfo {
int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
int ai_protocol; // use 0 for "any"
size_t ai_addrlen; // size of ai_addr in bytes
struct sockaddr *ai_addr; // struct sockaddr_in or _in6
char *ai_canonname; // full canonical hostname
struct addrinfo *ai_next; // linked list, next node
};
struct addrinfo is used to prep the socket address structures for
subsequent use. It's also used in host name lookups, and service name
lookups.
Initialization code
int status;
struct addrinfo hints;
hints.ai_family = AF_UNSPEC;
don't care IPv4 or Ipv6
hints.ai_socktype = SOCK_STREAM;
TCP stream sockets
hints.ai_flags = AI_PASSIVE;
fill in my IP for me
getaddrinfo()
int getaddrinfo(
const char *node, // "www.example.com" or IP
const char *service, // e.g. "http" or port number
const struct addrinfo *hints,
struct addrinfo **res);
syntax:
int socket(int domain, int type, int protocol);
sizeof(struct sockaddr)
returns -1 on error
accept() gets the pending connection on the port you are listen()ing on
accept returns a new socket file descriptor to use for this accepted
connection and -1 on error
working
// make a socket, bind it, and listen on it:
listen(sockfd, BACKLOG);
int send(int sockfd, const void *msg, int len, int flags);
syntax:
int recv(int sockfd, void *buf, int len, int flags);
freeaddrinfo(servinfo);
all done with this structure free the linked-list
struct sockaddr_storage their_addr; // connector's address informa-
tion
char s[INET6_ADDRSTRLEN];//space to hold the IPv4 string
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
close(new_fd);//close connection
exit(0);//exit
}
close(new_fd); // parent doesn't need this
}
return 0;
}
Basic client operation
The client gets the server ip address from the command prompt
Using getaddrinfo() the important structs are filled out with relev-
ant info.the info is server ip address and port no to connect to.
Now the socket is formed using the domain,type & protocol and
stored in socket descriptor
The recv func returns the no. of bytes actually read in the buffer,0 if conn is
closed,-1 if error.
{
perror("recv");//if error occurs.
exit(1);exit.
}
buf[numbytes] = '\0';
This statement stores '\0' that is end of string character in buf array when
transfer of data is over.