You are on page 1of 2

Photon

TCPECHO

SERVER
#include<
#include<
#include<
#include<
intmain(i
{
so
in
st
ch
lis
if

bz
se
se
se
b

lis
fo
{





}
cl
re
}

SoftSolution
O
<sys/types.h>
<sys/socket.h
<netinet/in.h
<stdio.h>
intargc,char
ocklen_tlen;
ntlistenfd,co
tructsockadd
harbuff[1024
stenfd=sock
(listenfd<0)
perror
zero(&servad
ervaddr.sin_f
ervaddr.sin_a
ervaddr.sin_p
ind(listenfd,

sten(listenfd,
or(;;)
len=siz
connfd
n=read
buff[n
write(
lose(listenfd)
eturn0;
ns,Tirunelveli
Co
>
h>
>
r**argv)
onnfd,n;
dr_inservadd
4];
ket(AF_INET,
r(Cantcreat
ddr,sizeof(se
family=AF_I
addr.s_addr=
port=htons(2
(structsocka

,5);
zeof(cliaddr);
d=accept(liste
d(connfd,buf
n]=0;
connfd,buff,
);
listen()list
SYNOPSIS:
DESCRIPTIO
The listen()
accepting c
queuetoth
Iflisten()is
thesocket's
RETURN VA
errnoisset
i
mputerNet
dr,cliaddr;
SOCK_STREA
teasocket);
ervaddr));
NET;
=htonl(INADD
2000);
addr*)&serva
sizeof(servad
;
enfd,(struct
ff,1024);
,sizeof(buff))
A
S
C
C
A
a
T
tenforsocketco
intlisten(intso
ON
() function mar
connections, an
hevaluespecifie
calledwithaba
slistenqueueto
ALUE Upon su
toindicatethe
tworksLab
AM,0);
;
DR_ANY);
addr,
ddr));
sockaddr*)&
);
Bou
I N
kno
or,
inte
any
I N
oft
Acceptsan
int accept
*address_l
socketSp
boundtoa
listen().
addressE
wherethe
address_le
length of t
thelength
RETURNVA
nonnegativ
returneda
Bin
stru
soc
bou
add
add
the
add
poi
AF_INET address
SOCK_STREAM)
Connectionorien
Connectionless A
AF_INETsockets
pplication conn
TCPorUDPtrans
onnectionsand
ocket,intbacklo
ks a connectio
d limits the nu
edbythebacklog
acklogargumen
o0.
uccessful comple
error.
bPrograms
&cliaddr,&len
und the listen
ADDR_ANY. Th
owing the IP ad
in the case
erfaces, it allow
y of the interfac
ADDR_ANYbin
thelowestnum
newconnection
t (int socket,
len);
pecifiesasocket
anaddresswith
Eitheranullpoin
addressofthec
en Points to a
he supplied soc
ofthestoredad
ALUEUponsuc
vefiledescriptor
nderrno issetto
nd a name to as
uctsockaddr*a
cket Specifies
und.
dress Points t
dresstobebou
eaddressdepen
dress_lenSpec
intedtobythea
s family sockets
or they can b
nted AF_INET so
AF_INET socket
canalsouseat
ects directly to
sports.
limitthequeue
og);
nmode socket,
mber of outsta
gargument.
tvaluethatisle
etions, listen() r
n);
ing socket to
his allows the p
dress of the ma
of a machine
ws server to rec
ces. When send
dstothedefaul
beredinterface.
onasocket.Sy
struct sockadd
thatwascreate
bind(),andhas
nter,orapointe
connectingsocke
socklen_t whic
ckaddr structure
ddress.
ccessfulcomplet
roftheaccepted
oindicatetheer
socket. Syntax:
address,socklen
the file descrip
to a sockaddr
ndtothesocke
dontheaddres
cifiesthelength
address argumen
can be either c
e connectionles
ockets use TCPa
ts use UDP as
typeofSOCK_RA
the IP layer and
ofincomingcon
, specified by t
nding connectio
essthan0,thef
returns 0. Othe
the special IP
program to wor
achine it was ru
with multiple
ceive packets de
ing, a socket bo
tIPaddress,wh
.
ntax:
dr *address,
edwithsocket(),
issuedasuccess
ertoasockaddr
etwillbereturn
ch on input spe
e, and on output
tion,accept()ret
dsocket.Otherw
rror.
int bind(int sock
_taddress_len)
ptor of the soc
structure conta
t.Thelength&
sfamilyofthes
ofthesockaddr
nt.
connectionorien
ss (type SOCK_
as the transport
the transport
AW.Ifthistype
d does not use
nnections
the socket argu
ons in the sock
unctionsetsthe
rwise, 1 is retu
P address
k without
unning on,
network
estined to
ound with
hichisthat
socklen_t
,hasbeen
sfulcallto
rstructure
ed.
ecifies the
t specifies
turnsthe
wise,1is
ket, const
;
ket to be
aining the
formatof
ocket.
rstructure
nted (type
_DGRAM).
t protocol.
protocol.
isset,the
either the
ument, as
et's listen
elengthof
urned and

Photon

CLIENT
#include<
#include<
#include<
#include<
intmain(i
{
so
in
ch
ch
st
so
if

bz
se
se
se
co
sc
w
n
b
fp
cl
re
}

SoftSolution
<sys/types.h>
<sys/socket.h
<netinet/in.h
<stdio.h>
intargc,char
ocklen_tlen;
ntsockfd,n;
harbuff[1024
harstr[1024]
tructsockadd
ockfd=socke
(sockfd<0)
perror
zero(&servad
ervaddr.sin_f
ervaddr.sin_a
ervaddr.sin_p
onnect(sockf
canf(%s,str
write(sockfd,s
=read(sockfd
uff[n]=0;
puts(buff,std
lose(sockfd);
eturn0;
ns,Tirunelveli
>
h>
>
r**argv)
4];
;
dr_inservadd
et(AF_INET,S
r(unabletoc
ddr,sizeof(se
family=AF_I
addr.s_addr=
port=htons(
d,(structsoc
r);
str,strlen(str
d,buff,1024);
dout);
Pro
#i n
st r




};
st r

};
i
dr,cliaddr;
SOCK_STREAM
createasock
ervaddr));
NET;
=inet_addr(ar
2000);
ckaddr*)&se
r));
;
connect()
intconnec
DESCRIPTI
Theconne
takesthef
socketSp
address
andforma
address_le
addressar

Ifthesock
anaddres
address.
Iftheinitia
address,b
socket'spe
If the initi
connection
RETURNV
Upon succ
errnoisse
ototype:
ncl ude <net
r uct sockad
shor t
unsi gned
st r uct i n
char
r uct i n_add
unsi gned
M,0);
ket);
rgv[1]);
rvaddr,sizeo
hto
byt
Syn
connectasock
ct(intsocket,co
ION
ect()functionreq
followingargum
pecifiesthefiled
Points to a soc
atoftheaddress
en Specifies t
rgument.
kethasnotalrea
swhich,unless
atingsocketisn
butnoconnectio
eeraddresswill
iating socket is
ntotheaddress
VALUE
cessful complet
ettoindicatethe
bzero
SYNO
DESCR
value
PARA

i net / i n. h>
ddr _i n {
sin
shor t sin
_addr sin
sin
dr {
l ong s_addr
of(servaddr));
onsconvert16
teorderandhos
ntax:uint16_th
ket.SYNOPSIS:
nststructsocka
questsaconnec
ments:
descriptorassoc
ckaddr structure
sdependonthe
the length of th
adybeenbound
thesocket'sadd
otconnectionm
onismade.Ifad
bereset.
connectionmod
sspecifiedbythe
tion, connect()
eerror.
ocopyzeroesin
OPSIS:voidbzero
RIPTION:Thebz
ofzero,intostr
AMETERSsPo
nIsthenum
n_family;
n_port;
n_addr;
n_zero[ 8] ;
r; / / l oad

bitand32bitq
stbyteorder.
htons(uint16_th
addr*address,s
ctiontobemad
ciatedwiththes
e containing the
addressfamilyo
he sockaddr st
dtoalocaladdre
dressfamilyisA
mode,thenconn
ddressisanullad
de, then connec
eaddressargum
returns 0. Othe
ntostring
o(char*s,intn);
zero()functionc
ings.
intstoabuffert
mberofzerosto
/ / e. g. AF
/ / e. g. ht
/ / see st r
/ / zer o t h
wi t h i net _
uantitiesbetwe
hostshort);
socklen_taddre
eonasocket.T
ocket.
e peer address.
ofthesocket.
ructure pointed
ess,connect()w
AF_UNIX,isanu
nect()setstheso
ddressforthep
ct() attempts to
ment.
erwise, 1 is re
;
opiesnbytes,e
thatzerosareco
obecopiedinto
F_I NET
t ons( 3490)
r uct i n_addr
hi s i f you w
_at on( )
ennetwork
ess_len);
Thefunction
The length
d to by the
willbinditto
unusedlocal
ocket'speer
rotocol,the
establish a
eturned and
achwitha
opiedinto.
thebuffer.
r , bel ow
want t o

You might also like