Socket Programming
Socket Programming
PROGRAMMING
WHAT IS COMPUTER NETWORKS?
Computer network is a communication network in which a collection of
computers are connected together to facilitate data exchange
The connection between the computers can be wired or wireless.
A computer network basically comprises of 5 components:
Sender
Receiver
Message
Transmission medium
Protocols
• The bind call then binds the well-known port number of the
server to the socket.
• The listen call turns the socket into a listening socket that can
accept incoming connections from clients.
• The accept call puts the server process to sleep until the arrival
of a client connection.
• The client does an active open(A client opens the communications
channel using the IP address of the remote host and the well-
known port address of the specific server program running on that
machine).
• The socket call creates a socket on the client side, and the connect
call establishes the TCP connection to the server with the specified
destination socket address.
• The sin_addr structure has only one field s_addr which identifies the
server side IP address (32-bits) to which various clients can connect.
• The sin_port and sin_addr fields are network byte ordered i.e., a
format that is permissible for a given network protocol.
Consider a 16-bit integer that is made up of 2 bytes. There are
two ways to store the two bytes in memory: with the low-
order byte at the starting address, known as little-endian byte
order, or with the high-order byte at the starting address,
known as big-endian byte order.
So that machines with different byte order conventions can
communicate, the Internet protocols specify a canonical byte
order convention for data transmitted over the network. This
is known as network byte order or big endian byte order.
BIG-ENDIAN BYTE ORDER IS
ALSO REFERRED TO LITTLE-ENDIAN BYTE
AS NETWORK BYTE ORDER ORDER IS ALSO
REFERRED TO
AS HOST BYTE ORDER
“BIND” SYSTEM CALL
BYTE ORDERING ROUTINES
• The various byte ordering routines that provide the
necessary conversion from host byte ordering to network
byte ordering and vice versa are as follows –
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
• htonl and ntohl convert from host-to-network and network-
to-host byte order respectively and return a long integer
value representing the converted ordering.
“BIND” SYSTEM CALL
MORE ON BYTE ORDERING ROUTINES…
• htons and ntohs convert from host-to-network and
network-to-host byte order respectively and return a short
integer value representing the converted ordering.
• htonl and ntohl can be used while converting network IP
addresses which are passed to sin_addr.s_addr field of
the sockaddr_in structure.
• htons and ntohs can be used while converting the 16-bit
port number that is passed to the sin_port field of the
sockaddr_in structure.
“BIND” SYSTEM CALL
ADDRESS CONVERSION ROUTINES
• An internet address is usually written in dotted-decimal
notation i.e., in the form of A.B.C.D. Thus it is necessary
that this address be converted into proper format before
passing the value to the sin_addr.s_addr field of the
sockaddr_in structure.
• The following functions provide the necessary address
conversion –
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
• The inet_addr function converts the dotted-decimal
address string into binary data in network byte order.
“BIND” SYSTEM CALL
MORE ON ADDRESS CONVERSION ROUTINES…
For example:
struct sockaddr_in sin;
char msg[10000];
int ret;
int sin_length;
sin_length = sizeof(sin);
ret = recvfrom(s, msg, 10000, 0, (struct sockaddr *)sin,
&sin_length);
printf("%d bytes received from %s (port %d)\n", ret,
inet_ntoa(sin.sin_addr), sin.sin_port);
A Simple TCP server program
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#define MAXSIZE 50
main()
{
int sockfd,newsockfd,retval;
socklen_t actuallen;
int recedbytes,sentbytes;
struct sockaddr_in serveraddr,clientaddr;
char buff[MAXSIZE];
int a=0;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1) {
cout<<"\nSocket creation error";
exit(-1); }
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(3387);
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
/*With IPv4, the wildcard address is specified by the constant INADDR_ANY, whose
value is normally 0. This tells the kernel to choose the IP address .*/
retval=bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
if(retval==-1) {
cout<<"Binding error"; close(sockfd);
exit(0); }
retval=listen(sockfd,1);
if(retval==-1) { close(sockfd);
exit(0); }
actuallen=sizeof(clientaddr);
newsockfd=accept(sockfd,(struct sockaddr*)&clientaddr,&actuallen);
if(newsockfd==-1) {
close(sockfd);
exit(0); }
recedbytes=recv(newsockfd,buff,sizeof(buff),0);
if(recedbytes==-1) {
close(sockfd);
close(newsockfd);
exit(0); }
puts(buff);
cout<<"\n";
gets(buff);
sentbytes=send(newsockfd,buff,sizeof(buff),0);
if(sentbytes==-1) {
close(sockfd);
close(newsockfd);
exit(0);
}
close(newsockfd);
close(sockfd);
}
Header Functions defined
arpa/inet.h inet functions: inet_aton,
inet_ntoa, and inet_addr
unistd.h System call: close, fork, exec
sys/socket.h basic socket definitions
recv, send
sys/types.h basic system data types
netinet/in.h sockaddr_in{} and other
Internet definitions
sys/stat.h for S_xxx file mode constants
fcntl.h for nonblocking