CN Lab Program
CN Lab Program
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
int sd;
if(sd==-1){
perror("socket-call"); exit(1);
printf("pid = %u \n",getpid() );
printf("descriptor = %u \n",sd);
while(1){sleep(1);}
------------------------------------------------------------------------------------
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
int sd;
if(sd==-1){
perror("socket-call"); exit(1);
}
printf("pid = %u \n",getpid() );
printf("descriptor = %u \n",sd);
while(1){sleep(1);}
-------------------------------------------------------------------------------------
#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(void){
int sd , n;
sleep(1);
sd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sd==-1)
perror("Socket-call");
exit(1);
printf("pid=%u\n",getpid());
printf("descriptor=%u\n",sd);
n = listen( sd, 1 );
if ( n == -1 ) { perror("listen"); exit(1); }
while(1)
sleep(1);
} return 0;}
--------------------------------------------------------------------------------------
#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(void){
int sd , n;
sleep(1);
sd=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(sd==-1)
perror("Socket-call");
exit(1);
printf("pid=%u\n",getpid());
printf("descriptor=%u\n",sd);
while(1) {sleep(1);}
return 0;
------------------------------------------------------------------------------------------------
5)** TCP ECHO client and server program architechture
// client program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int cd,i,n,j;
char buffer[BUFFERSIZE];
if( argc != 3 )
exit(1);
//if( cd == -1)
client.sin_family = AF_INET;
client.sin_port=htons(atoi(argv[2]));
n = inet_aton( argv[1],&(client.sin_addr));
while (1){
memset(buffer,'\0', BUFFERSIZE );
i=read (0,buffer,BUFFERSIZE);
j=write(cd,buffer,i);
memset(buffer,'\0',BUFFERSIZE );
i= read(cd,buffer,BUFFERSIZE);
write(1,buffer,i);
return 0;
----------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SIZE 200 // arbitrary
int sd,i,newd,n,k;
//
char a[SIZE];
char buffer[SIZE];
socklen_t length;
//if( sd == -1)
server.sin_family = AF_INET;
server.sin_port=htons(atoi(argv[2]));
n = inet_aton( argv[1],&(server.sin_addr));
n=listen(sd,5);
length=sizeof(client);
while (1){
i=read (newd,buffer,SIZE);
for(n=i-1;n>=0;n--){
a[k++]=buffer[n];
a[k]='\0';
printf("%s\n",buffer);
//write(newd,buffer,i);
write(newd,a,i);
return 0;
--------------------------------------------------------------------------------------------------
7) Write a program print client ip address and port number using TCP echo server String given by client
// client program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int sd,i,newd,n,k;
//
char a[SIZE];
char buffer[SIZE];
socklen_t length;
//if( sd == -1)
server.sin_family = AF_INET;
server.sin_port=htons(atoi(argv[2]));
n = inet_aton( argv[1],&(server.sin_addr));
n=listen(sd,5);
length=sizeof(client);
while (1){
i=read (newd,buffer,SIZE);
write(1,"recive from client:",19);
k=0;
for(n=i-1;n>=0;n--){
a[k++]=buffer[n];
a[k]='\0';
printf("%s\n",buffer);
write(newd,a,i);
return 0;
------------------------------------------
-----------------------------------------------------------------------------------
server program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int sd,i,newd,n,k;
//
// char a[SIZE];
char buffer[SIZE];
socklen_t length;
server.sin_family = AF_INET;
server.sin_port=htons(atoi(argv[2]));
n = inet_aton( argv[1],&(server.sin_addr));
length=sizeof(client);
while(1){
if(pid == 0){
while (1){
n=read(newd,buffer,SIZE);
printf("%s\n",buffer);
write(newd,buffer,n);
return 0;
---------------------------------------------------------------------------------------------------------
server program::
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<time.h>
int sd,i,newd,n,k;
char a[SIZE];
time_t t,T;
char *time1;
char buffer[SIZE];
socklen_t length;
char date_string[25];
server.sin_family = AF_INET;
length=sizeof(client);
n = inet_aton( argv[1],&(server.sin_addr));
n=listen(sd,5);
length=sizeof(client);
T=time(&t);
printf("No of seconds=%lu\n",T);
time1=ctime(&t);
43 );
system("/bin/date +%s");
// form, in [date_string]
i=strlen(time1);
write(newd,time1,i);
printf("after [ctime] call, Time zone = %s \n", *tzname );
return 0;
---------------------------------------------------------------------------
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<unistd.h>
int n,i,j;
char buffer[SIZE];
if( argc != 3 )
{
exit(1);
client.sin_family = AF_INET;
sizeof(client) );
j = write( 1, buffer, i );
return 0;
--------------------------------------------------------------------------
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int cd,i,n,j;
char buffer[BUFFERSIZE];
if( argc != 3 )
exit(1);
//if( cd == -1)
client.sin_family = AF_INET;
client.sin_port=htons(atoi(argv[2]));
n = inet_aton( argv[1],&(client.sin_addr));
while (1){
i=read (0,buffer,BUFFERSIZE);
j=write(cd,buffer,i);
i= read(cd,buffer,BUFFERSIZE);
write(1,buffer,i);
return 0;
---------------------------------------------------------------------------------
13))server program::
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<time.h>
int sd,i,newd,n,k;
//
char a[SIZE];
time_t t,T;
char *time1;
char buffer[SIZE];
char date_string[25];
socklen_t length;
//if( sd == -1)
server.sin_family = AF_INET;
// length=sizeof(client);
server.sin_port=htons(atoi(argv[2]));
n = inet_aton( argv[1],&(server.sin_addr));
n=listen(sd,5);
length=sizeof(client);
while(1){
if(pid == 0){
while (1){
n=read(newd,buffer,SIZE);
printf("%s\n",buffer);
write(newd,buffer,n);
T=time(&t);
printf("No of seconds=%lu\n",T);
system("/bin/date +%s");
// form, in [date_string]
time1=ctime(&t);
i=strlen(time1);
write(newd,time1,i);
}
return 0;
----------------------------------------------------------------------------------------------
client program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void)
int cd,i,n,j;
char buffer[BUFFERSIZE];
char address[20];
scanf("%u", &port );
client.sin_family = AF_INET;
client.sin_port=htons(port);
n = inet_aton( address,&(client.sin_addr));
while (1){
n = read(cd,buffer,BUFFERSIZE);
write(1,buffer,n);
n = read(0,buffer,BUFFERSIZE);
write(cd,buffer,n);
if(strstr(buffer,"quit")) break;
}
return 0;
---------------------------------------------------------------------------------
server program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int sd,i,newd,n,k;
char a[SIZE];
char buffer[SIZE];
socklen_t length;
server.sin_family = AF_INET;
server.sin_port=htons(PORT);
n=listen(sd,1);
while (1){
newd=accept(sd,NULL,0);
write(newd,buffer,sizeof(buffer));
while(1){
n=read(newd,buffer,SIZE);
write(1,"From client:",12);
write(1,buffer,n);
write(1,"TO client:",10);
n=read(0,buffer,SIZE);
write(newd,buffer,n);
}
return 0;
-------------------------------------------------------------------------------------------------------------
server program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
int ser_sd;
socklen_t clientlength;
char buffer[BUFFERSIZE];
ssize_t i, j ;
int n;
ser_sd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(SERVER_PORT);
clientlength = sizeof(client_addr);
//while( 1 )
//{
if( i == -1 ) perror("recvfrom-call");
return 0;
---------------------------------------------------------------------------------
client program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int n;
ssize_t i;
char buffer[SIZE];
socklen_t len;
socklen_t len1;
if( argc != 5 )
exit(1);
cd=socket( AF_INET,SOCK_DGRAM,IPPROTO_UDP);
client.sin_port = htons(atoi(argv[4]));
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
len=sizeof(struct sockaddr);
write( 1, buffer, n );
//shutdown( cd, SHUT_RDWR );
return 0;