Hello,
I'm writing in c++ on linux.
I'm trying to send several JPG-files through sockets one after another and I fail(
I'm doing it in this way: first client sends the server "FBEGIN" message, after that client sends JPG-file byte by byte. Server receives it byte by byte and writes to a file. After that I'm trying to send the second file the same way. But I fail. The code simplified. It is here:
CLIENT:
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
int main() {
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
FILE * file_to_send;
int ch;
char buffer[1];
char header_buffer[6];
//Connection
server = gethostbyname("localhost");
if (server == NULL) {
printf("Error interpreting host\n");
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error openning socket\n");
return 1;
}
serv_addr.sin_family = server->h_addrtype;
memcpy((char *) &serv_addr.sin_addr.s_addr, server->h_addr_list[0],
server->h_length);
serv_addr.sin_port = htons(55555);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("Error connecting\n");
return 1;
}
//~Connection
//FIRST FILE
file_to_send = fopen ("../left.jpg","r");
if(!file_to_send) {
printf("Error opening file\n");
return 0;
}
send(sockfd, "FBEGIN", 6, 0);
while((ch = getc(file_to_send)) != EOF) {
buffer[0] = ch;
send(sockfd, buffer, 1, 0);
}
fclose(file_to_send);
//~FIRST FILE
//SECOND FILE
file_to_send = fopen ("../right.jpg","r");
if(!file_to_send) {
printf("Error opening file\n");
return 0;
}
send(sockfd, "FBEGIN", 6, 0);
while((ch = getc(file_to_send)) != EOF) {
buffer[0] = ch;
send(sockfd, buffer, 1, 0);
}
fclose(file_to_send);
//~SECOND FILE
return 0;
}
SERVER:
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define PORT 55555
int main() {
int sockfd, newsockfd;
socklen_t clilen;
struct sockaddr_in cli_addr, serv_addr;
char buffer[1];
char header_buffer[6];
FILE * file_to_write;
//Establishing socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error openning socket\n");
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("Error binding socket\n");
return 1;
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
printf("Error on accept\n");
return 1;
}
//~Established
read(newsockfd, header_buffer, 6);
if(!strncmp(header_buffer, "FBEGIN", 6)) {
buffer[0] = 0;
file_to_write = fopen ("1.jpg","w");
while( recv(newsockfd, buffer, 1, 0) != 0) {
fwrite (buffer , sizeof(buffer[0]), 1, file_to_write);
buffer[0] = 0;
}
fclose(file_to_write);
}
read(newsockfd, header_buffer, 6);
if(!strncmp(header_buffer, "FBEGIN", 6)) {
buffer[0] = 0;
file_to_write = fopen ("2.jpg","w");
while( recv(newsockfd, buffer, 1, 0) != 0) {
fwrite (buffer , sizeof(buffer[0]), 1, file_to_write);
buffer[0] = 0;
}
}
}