*********************************SERVER************************************
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv)
{
int sockfd,newsockfd,clength;
FILE *ptr;
struct sockaddr_in serv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nStart");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nListening...");
printf("\n");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
printf("\nAccepted");
printf("\n");
ptr=fopen("sumit1.txt","w");
read(newsockfd,buffer,4096);
fprintf(ptr,"%s",buffer);
printf("\nClient message:%s",buffer);
write(newsockfd,buffer,4096);
printf("\n");
close(sockfd);
return 0;
}
*******************************CLIENT****************************************
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char*argv[])
{
int sockfd;
FILE *ptr;
ptr=fopen("sumit.txt","r");
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nReady for sending...");
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
fscanf(ptr,"%s",buffer);
// printf("\nEnter the message to send\n");
// printf("\nClient: ");
// fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
//printf("Serverecho:%s",buffer);
printf("File Transfer SuccessFull....\n");
printf("\n");
close(sockfd);
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv)
{
int sockfd,newsockfd,clength;
FILE *ptr;
struct sockaddr_in serv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nStart");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nListening...");
printf("\n");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
printf("\nAccepted");
printf("\n");
ptr=fopen("sumit1.txt","w");
read(newsockfd,buffer,4096);
fprintf(ptr,"%s",buffer);
printf("\nClient message:%s",buffer);
write(newsockfd,buffer,4096);
printf("\n");
close(sockfd);
return 0;
}
*******************************CLIENT****************************************
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char*argv[])
{
int sockfd;
FILE *ptr;
ptr=fopen("sumit.txt","r");
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nReady for sending...");
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
fscanf(ptr,"%s",buffer);
// printf("\nEnter the message to send\n");
// printf("\nClient: ");
// fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
//printf("Serverecho:%s",buffer);
printf("File Transfer SuccessFull....\n");
printf("\n");
close(sockfd);
return 0;
}
No comments:
Post a Comment