473,499 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transferring several files through sockets

2 New Member
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;
}
}
}
Oct 23 '07 #1
1 2111
kyosti
2 New Member
it seems to me that second read(newsockfd, header_buffer, 6); at server side doesnt wait for input...
Oct 23 '07 #2

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

Similar topics

24
2238
by: Joseph Geretz | last post by:
Up to this point, our application has been using Windows File Sharing to transfer files to and from our application document repository. This approach does not lend itself toward a secure...
1
4139
by: Michael Fitzpatrick | last post by:
Transferring arrays from C DLL's to VB.Net I have a DLL written in C. This DLL reads a text file and creates a several very large arrays, 500,000 points and even larger. I would like the get the...
2
1533
by: Rani | last post by:
hi guys I don't know if this is the right place for it but I created 2 pages in page one there is a text box in which the user enters his name, in page 2 there is a the same text box. I would...
15
5058
by: http://www.visual-basic-data-mining.net/forum | last post by:
Does anyone have any idea how to transferring data from TextBox1 in form1 to textBox2 in form2..... That means after i fill in any data in textBox1 and click Next button... It will bring me to...
9
5231
by: John | last post by:
Tried this on microsoft.public.access.gettingstarted - no response - perhaps more appropriate here. I'm not a database user, simply helping someone get started with a new computer. The old...
2
1626
by: Tommaso Caldarola | last post by:
I'm using a Remoting to manage small set of data, now the customer wants to transfer big binary files (up to 10 Gb) between 2 computers. It's better to use socket or other technology or I can...
5
3258
by: meetalps | last post by:
Hi All, Can you please help me with a step by step procedure to transfer files from my PC to DB2-AIX and vice versa. I am new to both. Example transferring a sql file to run from PC to DB2 on AIX....
7
5580
by: Lee | last post by:
Hey all, I'm using the following code to send stuff accross the network, appologies for it being in full, but I've really no idea exactly where this error is occuring. =======network...
2
16787
yashg
by: yashg | last post by:
I am building a data backup application in C# using Sockets. It has a server component and a client component. The client is going to upload files to the server through TCP sockets. I've got all...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.