This is simple example of tcp server and client. Here client write something on server socket and server write same message back to client.
Few observation:
- What happen if client tries to write 40 byte of date on server socket and reads 40 byte while server writes 20 and reads 20.
- fork process share its parent file descriptor.
- In the forked server socket try to print on terminal some message without use of '\n' it will not print
e.g.
void str_echo(int sockfd)
{
char buff[20];
ssize_t n;
while(1)
{
if((n=read(sockfd,buff,20))>=0)
write(sockfd,buff,20);
else
write(sockfd,"blank",20);
//else
printf("%s\n",buff); // if i replace it with printf("%s",buff) then it wont work
// write(sockfd,buff,20);
//return;
//else
// writen(sockfd,buff,n);
}
}
echo server:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include"str_echo.c"
int main()
{
int sockfd,connfd,childpid,clilen;
struct sockaddr_in cliaddr,servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(6060);
bind(sockfd,(struct sockaddr*) &servaddr,sizeof(servaddr));
listen(sockfd,5);
while(1)
{
clilen=sizeof(cliaddr);
connfd=accept(sockfd,(struct sockaddr*)&cliaddr,&clilen);
printf("connection accepted\n");
if((childpid=fork())==0)
{
close(sockfd);
printf("child process\n");
str_echo(connfd);
// str_echo(connfd);
exit(0);
}
printf("connection established\n");
close(connfd);
}
}
echo client:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include"str_cli.c"
int main()
{
int sockfd;
char buff[20];
ssize_t n;
struct sockaddr_in servaddr;
printf("before socket");
sockfd=socket(AF_INET,SOCK_STREAM,0);
printf("socket created");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr);
servaddr.sin_port=htons(6060);
//bind(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr));
printf("before connection");
if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==0)
{
printf("inside loop");
str_cli(sockfd);
/*write(sockfd,"sdfhsdkf",20);
printf("request send");
n=read(sockfd,buff,20);
printf("%s",buff);*/
}
printf("after connection");
close(sockfd);
}
Few observation:
- What happen if client tries to write 40 byte of date on server socket and reads 40 byte while server writes 20 and reads 20.
- fork process share its parent file descriptor.
- In the forked server socket try to print on terminal some message without use of '\n' it will not print
e.g.
void str_echo(int sockfd)
{
char buff[20];
ssize_t n;
while(1)
{
if((n=read(sockfd,buff,20))>=0)
write(sockfd,buff,20);
else
write(sockfd,"blank",20);
//else
printf("%s\n",buff); // if i replace it with printf("%s",buff) then it wont work
// write(sockfd,buff,20);
//return;
//else
// writen(sockfd,buff,n);
}
}
echo server:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include"str_echo.c"
int main()
{
int sockfd,connfd,childpid,clilen;
struct sockaddr_in cliaddr,servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(6060);
bind(sockfd,(struct sockaddr*) &servaddr,sizeof(servaddr));
listen(sockfd,5);
while(1)
{
clilen=sizeof(cliaddr);
connfd=accept(sockfd,(struct sockaddr*)&cliaddr,&clilen);
printf("connection accepted\n");
if((childpid=fork())==0)
{
close(sockfd);
printf("child process\n");
str_echo(connfd);
// str_echo(connfd);
exit(0);
}
printf("connection established\n");
close(connfd);
}
}
echo client:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include"str_cli.c"
int main()
{
int sockfd;
char buff[20];
ssize_t n;
struct sockaddr_in servaddr;
printf("before socket");
sockfd=socket(AF_INET,SOCK_STREAM,0);
printf("socket created");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
inet_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr);
servaddr.sin_port=htons(6060);
//bind(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr));
printf("before connection");
if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==0)
{
printf("inside loop");
str_cli(sockfd);
/*write(sockfd,"sdfhsdkf",20);
printf("request send");
n=read(sockfd,buff,20);
printf("%s",buff);*/
}
printf("after connection");
close(sockfd);
}
very good work...keep it up.
ReplyDelete