DISPLAYING NAME AND ADDRESS USING SOCKETS
By karthick
, in
DISPLAYING NAME AND ADDRESS USING SOCKETS
,
0 Comments
SERVER
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
main()
{
int st,st2,nst,sport,len;
char sendmsg[20],recmsg[20];
struct sockaddr_in servaddr,cliaddr;
printf("enter the port number :");
scanf("%d",&sport);
st=socket(AF_INET,SOCK_STREAM,0);
if(st<0)
printf("\n CANNOT CREATED
SOCKET\n");
else
printf("\nSOCKET IS CREATED SUCC
FULLY");
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(sport);
servaddr.sin_addr.s_addr=inet_addr("192.168.1.134");
st2=bind(st,(struct
sockaddr*)&servaddr,sizeof(servaddr));
if(st2<0)
printf("\n ERRORING
BINDED\n");
else
printf("\nBINDED\n");
listen(st,5);
len=sizeof(cliaddr);
nst=accept(st,(struct
sockaddr*)&cliaddr,&len);
if(nst<0)
printf("\nERRORING
ACCEPTING\n");
else
printf("\n ACCEPTING\n");
printf("\n\t The client address is
%s",inet_ntoa(cliaddr.sin_addr.s_addr));
}
CLIENT
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
main()
{
int st,cport;
char sendmsg[20],recmsg[20];
struct sockaddr_in servaddr,cliaddr;
printf("enter the port number :");
scanf("%d",&cport);
st=socket(AF_INET,SOCK_STREAM,0);
if(st<0)
printf("\n CANNOT CREATED
SOCKET\n");
else
printf("\nSOCKET IS CREATED
SUCCESSFULLY");
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(cport);
servaddr.sin_addr.s_addr=inet_addr("192.168.1.134");
if(connect(st,(struct
sockaddr*)&servaddr,sizeof(servaddr))
<0)
printf("\n ERRORING
CONNECTION\n");
else
printf("\nCONNECTION
SUCCESSFULLY\n");
}
OUTPUT
SERVER
[11ca013@mcalinux network]$ cc
exno2server.c
[11ca013@mcalinux network]$ ./a.out
enter the port number :4251
SOCKET IS CREATED SUCC FULLY
BINDED
ACCEPTING
The client address is 192.168.1.134
CLIENT
[11ca013@mcalinux network]$ cc
exno2client.c
[11ca013@mcalinux network]$ ./a.out
enter the port number :4251
SOCKET IS CREATED SUCCESSFULLY
CONNECTION SUCCESSFULLY
0 Response to "DISPLAYING NAME AND ADDRESS USING SOCKETS"
Post a Comment