Hello all , i have a homework that is related with tcp/ip server/client. The project says :
We sent from client to server , 2 file names , server opens them and sends back the data from the two files back to client and client prints them on the screen.
The code i made is
//client
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER "127.0.0.1"
#define TCP_PORT 6000
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, n;
struct sockaddr_in serv_addr;
char buffer[256];
/*if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(SERVER);
serv_addr.sin_port = htons(TCP_PORT);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
//printf("Please enter the message: ");
bzero(buffer,256);
//fgets(buffer,255,stdin);
strcpy(buffer,"file1.txt");
strcat(buffer," ");
strcat(buffer,"file2.txt");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}
//server
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define server 6000
void dostuff(int);
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = server;
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("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(sockfd);
dostuff(newsockfd);
exit(0);
}
else close(newsockfd);
} /* end of while */
close(sockfd);
return 0; /* we never get here */
}
void dostuff (int sock)
{
int n;
char buffer[256];
FILE *pFile1,*pFile2;
long lSize1,lSize2;
char * buffer1,*buffer2,*buffer3;
size_t file1str,file2str;
char *file1,*file2;
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
sscanf(buffer,"%s %s",file1,file2);
//opening and writing the first file in a buffer1
pFile1 = fopen ( file1, "rb" );
if (pFile1==NULL) {fputs ("File error",stderr); }
fseek (pFile1 , 0 , SEEK_END);
lSize1 = ftell (pFile1);
rewind (pFile1);
buffer1 = (char*) malloc (sizeof(char)*lSize2);
if (buffer1 == NULL) {fputs ("Memory error",stderr);}
file1str = fread (buffer1,1,lSize2,pFile1);
if (file1str!= lSize2) {fputs ("Reading error",stderr);}
fclose (pFile1);
//opening and writing the second file in buffer2
pFile2 = fopen ( file2, "rb" );
if (pFile2==NULL) {fputs ("File error",stderr); }
fseek (pFile2 , 0 , SEEK_END);
lSize2 = ftell (pFile2);
rewind (pFile2);
buffer2 = (char*) malloc (sizeof(char)*lSize2);
if (buffer2 == NULL) {fputs ("Memory error",stderr); }
file2str = fread (buffer2,1,lSize2,pFile2);
if (file2str != lSize2) {fputs ("Reading error",stderr); }
fclose(pFile2);
printf("%s %s",buffer1, buffer2);
strcpy(buffer3,buffer1);
strcat(buffer3,buffer2);
n = write(sock,buffer3,256);
if (n < 0) error("ERROR writing to socket");
}
Now my problem is . Server is up and running! No problem with this. When i try to connect the client , client returns me an empty line and after it closes.
Can you help me with this? I think i have made some mistakes passing the string in fopen or something and i am not familiar with debugging >.>