473,566 Members | 2,958 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_f amily = server->h_addrtype;
memcpy((char *) &serv_addr.sin_ addr.s_addr, server->h_addr_list[0],
server->h_length);
serv_addr.sin_p ort = htons(55555);

if (connect(sockfd , (struct sockaddr *) &serv_addr, sizeof(serv_add r)) < 0) {
printf("Error connecting\n");
return 1;
}
//~Connection

//FIRST FILE
file_to_send = fopen ("../left.jpg","r");

if(!file_to_sen d) {
printf("Error opening file\n");
return 0;
}

send(sockfd, "FBEGIN", 6, 0);

while((ch = getc(file_to_se nd)) != 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_sen d) {
printf("Error opening file\n");
return 0;
}

send(sockfd, "FBEGIN", 6, 0);

while((ch = getc(file_to_se nd)) != 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_f amily = AF_INET;
serv_addr.sin_a ddr.s_addr = htonl(INADDR_AN Y);
serv_addr.sin_p ort = htons(PORT);

if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_add r)) < 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(hea der_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(hea der_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 2121
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
2256
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 environment and so we are in the process of imposing a WebService gateway between our application client and the repository. (As a starting point, the...
1
4143
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 data in VB.Net so that I can plot it. Presently I am creating an equally sized array in VB and copying the data from the DLL's array into the VB...
2
1537
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 like the data to be transferred automatically to the second page how do I do that ? thanks
15
5063
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 form2....and there the data that i key in form1 will appear to form2 in textbox2 Thanks
9
5235
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 computer (win98se) runs a database in MS Access 97 pro, with all the attendant permissions etc. I can work on the database without problems, once...
2
1629
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 continue using Remoting without problems with great quantity of data to pass through boundaries. TIA --
5
3280
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. PC-OS windows DB2 on AIX platform. Thanks.
7
5590
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 code============ using System; using System.Net; using System.Net.Sockets; using System.Threading;
2
16791
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 figured out and have been able to transfer files between client and server. Now I have some doubts about which is the best method to transfer a file?...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
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 we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.