0% found this document useful (0 votes)
5 views15 pages

Socket Programming

Uploaded by

Suhas Annigeri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Socket Programming

Uploaded by

Suhas Annigeri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Socket

Programming in C
• Using TCP/IP sockets, write a client – server
program to make the client send the file name
and to make the server send back the contents
of the requested file if present.
Socket
• One socket(node) listens on a particular port at an IP, while the other
socket reaches out to the other to form a connection.
• The server forms the listener socket while the client reaches out to
the server.
void bzero(void *s, size_t n);
• The bzero() function shall place n zero-valued bytes in the area pointed to
by s.

htonl, htons, ntohl, ntohs - convert values between host and network byte
order
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
The htonl() function converts the unsigned integer hostlong from host byte
order to network byte order.
The htons() function converts the unsigned short integer hostshort from host
byte order to network byte order.
Stages for Server
• 1. Socket Creation
int sockfd = socket(domain, type, protocol)
• sockfd: socket descriptor, an integer (like a file handle)
• domain: integer, specifies communication domain. We use AF_ LOCAL as defined in the
POSIX standard for communication between processes on the same host. For
communicating between processes on different hosts connected by IPV4, we use
AF_INET and AF_INET6 for processes connected by IPV6.
• type: communication type
• SOCK_STREAM: TCP(reliable, connection-oriented)
• SOCK_DGRAM: UDP(unreliable, connectionless)
• protocol: Protocol value for Internet Protocol(IP), which is 0. This is the same number
that appears on the protocol field in the IP header of a packet.
• 2. Setsockopt
• This helps in manipulating options for the socket referred by the file
descriptor sockfd.
• This is completely optional, but it helps in reuse of address and port.
Prevents error such as: “address already in use”.

int setsockopt(int sockfd, int level, int optname, const void *optval,
socklen_t optlen);
• 3. Bind
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

• After the creation of the socket, the bind function binds the socket to
the address and port number specified in addr(custom data
structure).
• To bind the server to the localhost, we use INADDR_ANY to specify
the IP address.
• 4. Listen
int listen(int sockfd, int backlog);
• It puts the server socket in a passive mode, where it waits for the
client to approach the server to make a connection.
• The backlog, defines the maximum length to which the queue of
pending connections for sockfd may grow. If a connection request
arrives when the queue is full, the client may receive an error with an
indication of ECONNREFUSED.
• 5. Accept
int new_socket= accept(int sockfd, struct sockaddr *addr,
socklen_t *addrlen);
• It extracts the first connection request on the queue of pending
connections for the listening socket, sockfd, creates a new connected
socket, and returns a new file descriptor referring to that socket. At
this point, the connection is established between client and server,
and they are ready to transfer data.
Stages for Client
• 1. Socket connection: Exactly the same as that of server’s socket creation

• 2. Connect: The connect() system call connects the socket referred to by


the file descriptor sockfd to the address specified by addr. Server’s
address and port is specified in addr.

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);


read() and write()
• The read() and write() functions are used to send and receive between client
and server.
• The syntax of read() function is,
ssize_t read(int file_descriptor, void *buffer, size_t size);
• The data of specified size is read and stored in the buffer.

• The syntax of the write function is,


ssize_t write(int file_descriptor, const void *buffer, size_t size);
• The data of specified size is written from the buffer. On successful read or
write, the read() and write() functions return the number of bytes of data that
has been read or written or return the value of -1 on failure.
• The close() function is used to terminate the socket connection.
ssize_t send(int socket, const void *buffer, size_t length, int flags);

• The send() function shall initiate transmission of a message from the


specified socket to its peer.
• The send() function shall send a message only when the socket is
connected (including when the peer of a connectionless socket has
been set via connect()).
• int inet_pton(int af, const char *src, void *dst);

• The inet_pton() function converts an Internet address in its standard text


format into its numeric binary form. The argument af specifies the family of
the address.
• The input argument src is a null terminated string. It points to the string
being passed in.
• The argument dst points to a buffer into which inet_pton() stores the
numeric address. The address is returned in network byte order. The caller
must ensure that the buffer pointed to by dst is large enough to hold the
numeric address.

You might also like