Você está na página 1de 11

Network programming

The purpose of a network, or an internetwork, is to provide services to users: A


user at a local site wants to receive a service from a computer at a remote site. One
way to achieve this purpose is to run two programs. A local computer runs a
program to request a service from a remote computer; the remote computer runs a
program to give service to the requesting program. This means that two computers,
connected by an internet, must each run a program, one to provide a service and
one torequest a service.
What is a socket?
An interface between application and network which isused for communication
between processes
Once configured the application can pass data to the socket for network
transmission receive data from the socket (transmitted through the network by
some other host)
To the kernel, a socket is an endpoint of communication.
To an application, a socket is a file descriptor that lets theapplication read/write
from/to the network.
Clients and servers communicate with each by readingfrom and writing to socket
descriptors.
Remember: All Unix I/O devices, including networks, aremodeled as files
Two essential types of sockets
1) SOCK_STREAM_ TCP,connection-oriented, reliable delivery, in-order
guaranteed, bidirectional.
2) SOCK_DGRAM_ UDP, no notion of connection appindicates dest. for
eachpacket, unreliable delivery,no order guarantees, can send or receive
What is a Port? A Port Number?
Port numbers are used to identify services on ahost,Port numbers can be
well-known (port 0-1023)
dynamic or private (port 1024-65535) Servers/daemons usually use well-known
ports
any client can identify the server/service
HTTP =80, FTP =21, Telnet =23, ...
/etc/service defines well-known ports Clients usually use dynamic portsassigned
by the kernel at run time
Creating a Socket
int socket(int family,int type,int proto);
family specifies the protocol family(AF_INET for Internet, PF_INET for
TCP/IP).
typespecifies the type of service(SOCK_STREAM, SOCK_DGRAM).
protocol specifies the specific protocol(usually 0, which means the default).
socket()
The socket() system call returns asocket descriptor (small integer) or -1 on
error.
socket() allocates resources needed for acommunication endpoint - but it does
not deal with endpoint addressing.Generic socket addresses
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
};
sa_family specifies the address type.
sa_data specifies the address value
struct sockaddr_in (IPv4)
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
A special kind of sockaddr struct
IPv4 Socket AS: It is defined as follows:
#include <netinet / in.h>
struct in_addr
struct in_addr {
in_addr_t s_addr;
};
in_addr just provides a name for the C type
associated with IP addresses.
Network Byte Order
All values stored in a sockaddr_in must be in network byte order.
sin_port a TCP/IP port number.
sin_addr an IP address
. Network byte order =Big Endian
Assigning an address to a socket
The bind() system call is used to assign anaddress to an existing socket.
int bind( int sockfd,const struct sockaddr *myaddr,int addrlen);
bind returns 0 if successful or -1 on error. bind()
calling bind() assigns the addressspecified by the sockaddr structure to the
socket descriptor.
You can give bind() a sockaddr_instructure:
bind( mysock,(struct sockaddr*) &myaddr,sizeof(myaddr) );
Uses for bind()
There are a number of uses for bind():
Server would like to bind to a well knownaddress (port number).
Client can bind to a specific port.
Client can ask the O.S. to assign any available port number.
What is my IP address ?
How can you find out what your IP address is soyou can tell bind() ?
There is no realistic way for you to know the right IP address to give bind() -
what if the computer has multiple network interfaces?
specify the IP address as: INADDR_ANY, this
tells the OS to take care of things.Other socket system calls
Connecttiion-oriientted(TCP)
connect()
listen()
accept()
read()
write()
close()
Connecttiionlless(UDP)
connect()*
send()
recv()
sendto()
recvfrom()
Methods :
socket() Creates a new socket and returns its descriptor
bind() Associates a socket with a port and address
connect() Establish queue for connection requests
listen() Accepts a connection request
accept() Initiates a connection to a remote host
recv() Receive data from a socket descriptor
send() Sends data to a socket descriptor
System calls used with sockets:
Socket calls are those functions that provide access to the underlying functionality
and utility routines that help the programmer. A socket can be used by client or by
a server, for a stream transfer (TCP) or datagram (UDP) communication with a
specific endpoints address. Following figure shows a time line of the typical
scenario that takes place between client and server. First server is started, then
sometimes later a client is started that connects to the server. The client sends a
request to the server, the server processes the request, and the server sends back
reply to the client. This continues until the client closes its end of the connection,
which sends an end of file notification to the server. The server then closes its end
of the connections and either terminates or waits for a new connection.
socket function:
#include socket (int family, int type, int protocol);
returns negative descriptor if OK & 1 on error.
connect Function : The connect function is by a TCP client to establish a active
connection with a remote server. The arguments allows the client to specify the
remote end points which includes the remote machines IP address and protocol
port number.
#include <sys/socket.h>
int connect (int sockfd, const struct sockaddr * servaddr, socklen_t addrelen)
returns 0 if ok -1 on error.
Bind(): When a socket is created, it does not have any notion of end points
addresses An application calls bind to specify the local endpoint address for a
socket. That is the bind function assigns a local port and address to a socket..
#include <sys/socket.h>
int bind (int sockfd, const strut sockaddr *myaddr, socklen_t addrlen)
The second arguments is a pointer to a protocol specific address and the third
argument is the size of this address structure. Server bind their well known port
when they start. (A TCP client does not bind an IP address to its socket.)
listen Function:
The listen function is called only by TCP server and it performs following
functions.
The listen function converts an unconnected socket into a passive socket,
indicating that the kernel should accept incoming connection requests directed to
this socket. In terms of TCP transmission diagram the call to listen moves the
socket from the CLOSED state to the LISTEN state.
The second argument to this function specifies the maximum number of
connections that the kernel should queue for this socket.
#include <sys/socket.h>
int listen (int sockfd, int backlog); returns 0 if OK -1 on error.
This function is normally called after both the socket and bind functions and must
be called before calling the accept function
accept Function : accept is called by a TCP server to return the next completed
connection from the from of the completed connection queue. If the completed
queue is empty, the process is put to sleep.
# include <sys/socket.h>
int accept ( sockfd, struct sockaddr * cliaddr, socklen_t *addrlen) ;
return non negative descriptor if OK, -1 on error.

Você também pode gostar