Text

socket Programing

How can u communicate between two computer by your own C code. Here is the solution......

First run server program and then socket program and follow instructions and its done.

TCP

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

i n t s o c k e t ( i n t domain , i n t t y p e , i n t p r o t o c o l ) ;

Returns a file descriptor (called a socket ID) if successful, -1

otherwise. Note that the socket returns a socket descriptor which

is the same as a file descriptor.

The domain is AF INET.

The type argument can be:

SOCK STREAM: Establishes a virtual circuit for stream

SOCK DGRAM: Establishes a datagram for communication

SOCK SEQPACKET: Establishes a reliable, connection based,

two way communication with maximum message size. (This is

not available on most machines.)

protocol is usually zero, so that type defines the connection

within domain.

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

i n t bind ( i n t sid , s t r u c t sockaddr ∗addrPtr , i n t l e n )

Where

sid: is the socket id

addrPtr: is a pointer to the address family dependent

address structure

len: is the size of *addrPtr

Associates a socket id with an address to which other processes

can connect. In internet protocol the address is [ipNumber, portNumber]

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

int l i s t e n ( int sid , int size );

Where size it the number of pending connection requests allowed

(typically limited by Unix kernels to 5).

Returns the 0 on success, or -1 if failure.

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

i n t send ( i n t sid , const char ∗ b u f f e r P t r ,i n t len , i n t f l a g )

Send a message. Returns the number of bytes sent or -1 if failure.

(Must be a bound socket).

flag is either

0: default

MSG OOB: Out-of-band high priority communication

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

i n t recv ( i n t sid , char ∗ b u f f e r P t r ,i n t len , i n t f l a g s )

Receive up to len bytes in bufferPtr. Returns the number of

bytes received or -1 on failure.

flags can be either

0: default

MSG OOB: out-of-bound message

MSG PEEK: look at message without removing

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

i n t connect ( i n t sid , s t r u c t sockaddr ∗addrPtr , i n t l e n )

Specifies the destination to form a connection with (addrPtr), and

returns a 0 if successful, -1 otherwise.

UDP variations

It is not necessary for both sockets to bind

The receiver gets the address of the sender

It is possible for a UDP socket to connect

In this case, send/recv (or write/read) must be used instead of sendto/recvfrom.

Asynchronous errors can be returned (using ICMP)

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

int sendto(int sid , const void ∗bufferPtr,size_t bufferLength , int flag ,struct sockaddr ∗addrPtr , socklent addrLength )

Send a buffer, bufferPtr, of length bufferLength to address

specified by addrPtr of size addrLength. Returns number of

bytes sent or -1 on error.

#i n c l u d e <s y s / t y p e s . h>

#i n c l u d e <s y s / s o c k e t . h>

int recvfrom ( int sid , void ∗bufferPtr , int bufferLength ,int flag , sockaddr ∗addrPtr , int ∗addrLengthPtr )

Receive a buffer in bufferPtr of maximum length bufferLength from an unspecified sender.

Sender address returned in addrPtr, of size *addrLengthPtr.

Returns number of bytes receive or -1 on error.