You are on page 1of 5

;-c . ... ... .

i •inc u e <std~o.h;
2 #~nc~ude<sys/socket.h>
~ 3 #inctudecsys/types.h>
, 4 #incl.ude<netinet/in. h>
S #inc~udecstring.h>
6 #ihc~ude<stdlib.h>
7
a int main()
I 9 {
10 struct sockaddr- in saddr·I
11 saddr.sin_ family = AF INET;
12 saddr.sin_port = htons( 5000 );
13 saddr.sin_addr.s_addr = INADDR ANY;
14 int fd = socket(AF INET, SOCK STREAM, 8 );
15 if (fd < 0 ) - -
16 {
:t: 17 printf( "Building socket fail -e d\n " );
:, 18 exit ( - 1 );
·1: 19 }
;
20 printf ( ''Socket Creation Sucessful l \n " );
121 if (connect(fd, ( struct sockaddr*)&saddr, sizeof (saddr)) < 0)
~22 {
c23 printf( "Co,nnectiien Failed\n "' );
J24 exit ( - 1 );
J
! 25 }
; 26 printf ( "Connected to server\n 11
);

27 int nl,fact;
28 printf ( "Ente r number\n " );
29 scanf ( ' %d ,&nl);
1 11

30 send(fd, &nl, sizeof (nl), 0 );


31 int n = recv(fd, &fact, sizeof (fact), 0 );
_. - • .._, Ill.I -~IL.I .L11g • 0>
111111,
1
6 #incl.ude<stdl.ib. h>
--" 7
!B fr -.-,c
s int
{
main ( )
,_· (, 9
10 struct sockaddr in saddr:
11 saddr.sin_ famity = AF_ INET;
12 saddr.sin_port = htons( 5000 );
13 saddr.sin addr.s addr = INADDR ANY;
14 int fd = socket(AF INET, SOCK STREAM, 0 );
15 if (fd < 0 ) - -
16 {
17 printf( " IBuilding socket failed \n " );
18 exit ( - 1 );
19 }
I•
20 printf( Socket Creation Sucessfull\n " );
11

'i - 21 if (connect(fd, ( struct sockaddr*)&saddr, sizeof (saddr)) < 0)


{
- 22
llldl:23 printf( Connection Failed\n " );
11

24 exit ( - 1 );
25 }
26 printf( "Connected to server\n 11
);

27 int nl,fact;
28 printf ( "Enter number\n " );
29 scanf( " %d " ,&nl);
30 send(fd, &nl, sizeof (nl), 0 );
31 int n = recv(fd, &fact, sizeof (fact), 0 ):
32 printf ( t• Facto rial: %d\n ° , fact) ;
33 close(fd);
34 return 0 ;
35 ·. ,
·- -·- ··- ----

~ ; #incl.ude<stdio. h>
2 #incl.ude<sys/socket. h>
3 #inc1.ude<sys/types.h>
4 #incl.ude<netinet/in.h>
S #incl.ude<string.h>
6 #inc~ude<std1.ib.h>
7
s int main ()
9 {
10 struct sockaddr_ in saddr, caddr;
11 saddr.sin_ famity = AF_ INET;
, 12 saddr.sin_port = htons( 5000 );
'; 13 saddr.sin_addr.s_addr = INADDR_ANY;
14 int fd = socket(AF_INET, SOCK_STREAM, 0 );
1 15
,I
if (fd < 0 )
i l6 {
· 17 printf( "Building s ocket fa i led\n " );
~ 18 exit ( - 1 );
~ 19 }
20 printf( Socket Creation Sucessfull \n " );
11

21 if (bind(fd, ( struct sockaddr*)&saddr, sizeof (saddr)) < 0 )


·22 {
23 printf( "Binding failed \n 11
);

24 exit ( - 1 );
25 }
26 printf( Bind Sucessfull\n " );
11

27 if (tisten(fd, 5 ) != 0 )
28 {
29 printf ( "Listen Fai led\n J, );
30 exit ( - 1 );
31 }
t4,M1•M4i4t i&frU1~
..., . ~-. .
0

'- \ e::11
18 exit(- 1 ):
19 }
20 printf( "Socket Creation Sucessfull\n " ):
21 if (bind(fd, ( struct sockaddr*)&saddr, sizeof (saddr)) < 0 )
22 {
23 printf( Binding failed \n " );
11

24 exit(- 1 );
- 25 }
26 printf( "Bind Sucessfull\n 11
):

27 if (listen(fd, 5 ) != 0 )
28 {
29 printf( Listen Failed\n
11 11
);

30 exit(- 1 );
31 }
32 printf ( "Server Listining\n " );
. 33 int len = sizeof (caddr);
l:' \>_ -- ~ 34 int aid= accept(fd, ( struct sockaddr*)-&caddr, &len);

- 36 -·-: ~•3s

37
38
if (aid < 0 )
{
printf( "Accept Failde\n " );
exit (- 1 );
le· 39 }
I
c- 40 printf ( "Accepted\n 11
) ;

E 41 int num,fact=l ;
· 42 int n = recv(aid, &num, sizeof (num), 0 );
F._ 43 for ( int i=l ;i<=num;i++)fact*=i;
4 send(aid, &fact, sizeof (fact), 0 );
5 close(aid);
6 close(fd);
7 return e;

11 Jr>Lll n s o ■ a a ~ cl d 11
ile Edit View Search Te rm inal Help
l i.t@ki.tt-Vi.rtua1Sox : - $ gee en'labsessi.onalel'lent . e
1l11bsessi.on ■\c\i.ent.c: In fun c ti.on ' fllat.n• :
1labsessi.on11\cU.ent.c:SS: S : warni ng: 'll'lpli.ci.t declarati.on of functi.on ' close• ; di.d you !'lean •pc\ose
? [ -Wi.l'lpti.c i.t-funeti.on-dee\arati.on ]
~~~~: ( fd );
pclosc>
Li.t@ki.i.t- vtrtualeox : - s . /a.out
c kc t Crea ti.on Sucessfull
nnected to s erve r
t e r nul'lber
e6306
ctori.al : e
it@kii.t• Vi.rtua\eox : - $ I

You might also like