By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

server client model

P: 2
(problem in writing a string into a file on client side.) hey i have written a code of server and client.what i want to do is to transfer the contents of file on server side to the client side (line by line).. the code for server is given as

/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <iostream.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>

int main(int argc, char *argv[])
{
const int N=80;
char line [N];
int sockfd, newsockfd, portno, clilen;
char *ch;
struct sockaddr_in serv_addr, cli_addr;
char buffer[256];
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,(socklen_t *)&clilen);
if (newsockfd < 0)
("ERROR on accept");
n=send(newsockfd,"here is the list of files:1 for ankur.txt,2 for a.txt,3 for b.txt",255,0);
bzero(buffer,256);
n = recv(newsockfd,buffer,255,0);
if (n < 0) ("ERROR reading from socket");
printf("Here is the message: %c\n",buffer[0]);
//ch=buffer;
switch (buffer[0])

{
case '1':
{ n=send(newsockfd,"file exists",18,0);
ifstream fin;
fin.open("ankur.txt");
while(fin)
{
fin.getline(line,N);
printf("Read line is :%s\n",line);
n=send(newsockfd,line,sizeof(line),0);
}fin.close();
if (n < 0)
{
printf("ERROR writing to socket");
return 0;
}
}
break;


case '2': { n=send(newsockfd,"file exists",18,0);

ifstream fin;
fin.open("a.txt");
while(fin)
{
fin.getline(line,N);
printf("Read line is :%s\n",line);
n=send(newsockfd,line,sizeof(line),0);

}fin.close();
if (n < 0) ("ERROR writing to socket");
return 0;
}break;
case '3':{ n=send(newsockfd,"file exists",18,0);

ifstream fin;
fin.open("b.txt");
while(fin)
{
fin.getline(line,N);
printf("Read line is :%s\n",line);
n=send(newsockfd,line,sizeof(line),0);

}fin.close();
if (n < 0) ("ERROR writing to socket");
return 0;
}break;
default:
{ n = send(newsockfd,"file doesnot exists",18,0);
}break;

}
}

and the code for client is:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
int main(int argc, char *argv[])
{
int sockfd, portno,n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
("ERROR connecting");
n = recv(sockfd,buffer,255,0);
printf("the list of files:%s\n",buffer);
printf("Please enter the file no:");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = send(sockfd,buffer,strlen(buffer),0);
if (n < 0)
("ERROR writing to socket");
bzero(buffer,256);
n = recv(sockfd,buffer,255,0);
if (n < 0)
("ERROR reading from socket");
printf("%s\n",buffer);

fstream fout;
fout.open("d.txt",ios::out);

n=recv(sockfd,buffer,255,0);


printf("content of buffer:%s\n",buffer);
if (n < 0)
printf("ERROR reading from socket");
fout.write((char*)&buffer,sizeof(buffer));



fout.close();
return 0;
}

now if i run this code i am facing a problem on my client side in writing the text in d.txt file......so plz help me out....
Jul 31 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.