UNIT 2
SOCKET PROGRAMMING
INTRODUCTION:
Programming with TCP/IP sockets:
Step 1. Create a socket:
Cont…
DEMO TO CREATE SOCKET:
#include <stdio.h> /* defines printf */
#include <stdlib.h> /* defines exit and other sys calls */
#include <sys/socket.h>
main(int argc, char **argv)
{
int fd;
/* create a tcp/ip socket */
/* request the Internet address protocol */
/* and a reliable 2-way byte stream (TCP/IP) */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot create socket");
return 0;
}
printf("created socket: descriptor=%dn", fd);
exit(0);
}
Step 2. Identify (name) a socket:
Cont…
Cont..
Cont…
Cont…
Cont…
Code:
#include <stdlib.h> /* defines system calls */
#include <stdio.h> /* needed for printf */
#include <string.h> /* needed for memset */
#include <sys/socket.h>
#include <netinet/in.h> /* needed for sockaddr_in */
main(int argc, char **argv)
{
struct sockaddr_in myaddr; /* our address */
int fd; /* our socket */
unsigned int alen; /* length of address (for getsockname) */
/* create a tcp/ip socket */
/* request the Internet address protocol */
/* and a reliable 2-way byte stream (TCP/IP) */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot create socket");
return 0;
}
printf("created socket: descriptor = %dn", fd);
Cont…
/* bind to an arbitrary return address */
/* in this case, assume it's the client side, so we won't
/* care about the port number as no application will connect here */
/* INADDR_ANY is the IP address and 0 is the socket */
/* htonl converts a long integer (e.g. address) to a network */
/* representation (IP-standard byte ordering) */
memset((void *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(0);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
return 0;
}
alen = sizeof(myaddr);
if (getsockname(fd, (struct sockaddr *)&myaddr, &alen) < 0) {
perror("getsockname failed");
return 0;
}
printf("bind complete. Port number = %dn", ntohs(myaddr.sin_port));
exit(0);
}
Number Conversion:
Cont..
Step 3a. Connect to a server from a client
Cont…
Cont…
Cont…
Cont…
Cont…
Step 3b. Accept connections on the server
Cont…
Step 4. Communicate
Cont…
Cont…
Step 5. Close the connection
Synchronous or Asynchronous?
POSIX DATATYPES:
Posix Datatypes:
Cont…
Socket Addresses:
Cont…
Cont…
Cont…
Assigning Address To Socket:
Cont…
JAVA Socket Programming:
Socket Class:
Server Socket Class:
Example Of JAVA Server Programming:
Thread Programming:
Lifecycle Of Thread:
Following are the stages of the life cycle −
• New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the
thread. It is also referred to as aborn thread.
• Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is
considered to be executing its task.
• Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to
perform a task. A thread transitions back to the runnable state only when another thread signals the waiting
thread to continue executing.
• Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in
this state transitions back to the runnable state when that time interval expires or when the event it is waiting
for occurs.
• Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise
terminates.
Thread Priorities:
Create a Thread by Implementing a Runnable
Interface:
• Step 3
Once a Thread object is created, you can start it by calling start() method, which executes a call to run( )
method. Following is a simple syntax of start() method −
void start();
Create a Thread by Extending a Thread Class:
Berkeley Sockets:
Socket API Functions:
Cont…
Cont…
Berkeley sockets can operate in one of two
modes: blocking or non-blocking.
Socket Address Structure:
IPv4 Socket Address Structure:
Datatypes:
Cont..
Generic Socket Address Structure
IPv6 Socket Address Structure:
Cont…
Comparison of socket address structure:
Value Result Arguments:
Cont…
Cont…
Cont…
With variable length socket address structure, the value returned can be less than the maximum size of
the structure.
Byte Order functions:
Cont…
Cont…
Byte Manipulation Functions:
Cont…
Cont…
Cont..
Address Conversion Functions:
Cont…
Cont…
Cont…
Elementary socket system calls:
Cont…
Bind:
Connect:
Listen:
Accept:
send, sendto, recv and recvfrom:
Close :
TCP ports (ephemeral, reserved):
Asynchronous I/O
Asynchronous I/O advantages:
Advantages :
Asynchronous I/O API’s:
Cont…
How asynchronous I/O works?
Cont…
Asynchronous I/O structure
I/O Multiplexing Models:
Cont…
Select Function :
Cont…
Poll Function:
Cont…
Figure Shows Input events and returned revents
for poll.
fcntl Function:
Cont…
Cont…
Cont…
Unix Domain Protocols:
Cont…
Cont…
Unix Domain Socket Address Structure:
Cont…

Socket Programming Intro.pptx