File Transfer over Sockets


 
Thread Tools Search this Thread
Top Forums Programming File Transfer over Sockets
# 1  
Old 01-05-2009
File Transfer over Sockets

Hello there !!!!!!!!! I got some problems trying to transfer a file through sockets.

The Server must be in Java and the Client in C++

I came up with this code for the server :

BufferedInputStream input;
BufferedOutputStream output;
public void send_data() throws IOException
{
File transfer = new File("tux.png");
InputStream in = new FileInputStream(transfer);


byte[] buff = new byte[sock.getSendBufferSize()];
int bytesRead = 0;

System.out.println(transfer.length()+ " bytes");

while((bytesRead = in.read(buff))>0)
{
output.write(buff,0,bytesRead);
}
in.close();
}
public void recv_data() throws IOException
{
InputStream input = sock.getInputStream();
FileOutputStream wr = new FileOutputStream(new File("Test"));
byte[] outBuffer = new byte[sock.getReceiveBufferSize()];
int bytesReceived = 0;

while((bytesReceived = input.read(outBuffer))>0)
{
wr.write(outBuffer,0,bytesReceived);
}
wr.close();


}



But i can not do the client side in C++. Can anybody give an example??
If the code a give you is very confusing , it would be OK to give a very simple example of how to send/recv a file from/to client(C++)/server/(Java)


Thank you in advance.
# 2  
Old 01-07-2009
Why C++ and not just plain C? Is the client program to run on UNIX or or on Windows? What happens when the program fails to complete the transfer? How soon is the homework assignment due?
# 3  
Old 01-07-2009
The reason why i have chosen C++ is because the assignment is a video mail program , and tha client must communicate with the Gstreamer.Of course the programm will run on Linux!!!!!!!!!!!!

Now i have written new code for the Server

public BufferedOutputStream recv_data() throws IOException
{
BufferedOutputStream f = null;
long tlen = 0;
byte[] buf = new byte[32768];
int[] val = new int[1];

while(true) {
int blen = recv_ints(val, 1);
if(blen==0) break;
tlen += blen;
while(blen>0) {
int len = (blen<32768) ? blen : 32768;
reader.readFully(buf,0,len);
f.write(buf,0,len);
blen -= len;
}
}
return f;
}


What i am trying to do right now is to right the C++ code , but i do not if C++ has something like BufferedOutputStream .Do not worry about the assignment due :-)
# 4  
Old 01-07-2009
You'd need to use exactly the data class gstreamer used anyhow, so this' more a question about gstreamer than C++. C code is still perfectly valid inside C++, by the way, so you can still use the normal socket() calls and all that. What I would do is first write a pure C program that does nothing but connect to and read from the socket, then see how I could fit bits of it into gstreamer rather than trying to build the whole thing at once, that's just an invitation for headaches.

I'm not sure you need to write anything, though. gstreamer comes with the ability to read from TCP sockets. Unless gstreamer needs a special data format on the socket I don't see why it couldn't connect to your Java program.

Last edited by Corona688; 01-07-2009 at 03:22 PM..
# 5  
Old 01-07-2009
My question is how to create the function that sends a file in C++/C . Because Java has a Class like : BufferedOutputStream , but C hasn't.
When i send a string for example over the socket i use the function : send(new_fd, (char *) str, strlen(str), 0) but i do not know what to do when i have a file , how should i transform the "send" function?
The gstreamer part is irrelevant , do not worry about that, just focus on the client/server .
# 6  
Old 01-07-2009
Okay. UNIX uses the open(), read() and write() calls to deal with files, and the recv() and send() calls to deal with sockets. It uses the socket() and bind() calls to create a server socket and give it an address, or the socket() and connect() calls to make and connect a client socket to something. close() works for both files and sockets. There's lots and lots of examples for these all over the internet, and probably manpages on your system, try 'man 2 socket'.

Code:
int send_file(int fd, int sock)
{
    char buf[512];
    ssize_t len;

    while( (len = read(fd, buf, 512)) > 0)
    {
      if(send(sock, buf, len, 0) != len)
      {
        perror("Couldn't send data:");
        return(-1);
      }
    }

  return(0);
}

which will work on UNIX systems in general. You mention Linux in particular, which has an especially easy and efficient way:
Code:
size_t len=512;

while(sendfile(sock, fd, NULL, len) == len);

Note that sendfile is special, it can only read from files and can only send to sockets.

See 'man 2 send', 'man 2 read', etc. for details on these functions like what include files they need.

Last edited by Corona688; 01-07-2009 at 04:00 PM..
# 7  
Old 01-08-2009
If you need to work with a special C++ class or to write C++ code in general, this is probably the wrong site for it. As I understand it (don't let the moderator status fool you, I'm still somewhat new here), the forum deals with high level programming questions that are related and/or specific to UNIX. Beyond what Corona posted above, I'm not sure you can get squeeze much more out of us Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

File transfer

When using FTP to transfer a file from IBM iSeries family of servers client to a non IBM Iseries family server, files might have characters appear in the wrong format Eg | in Iseries and while transferring fro Iseries system to Linux , but instead of | it is showing as ?. Please advise (3 Replies)
Discussion started by: sudhainit
3 Replies

2. Shell Programming and Scripting

File transfer script

Hi, I need a shell script to transfer a file from one server(unix box) to another server(windows box). I have the details of the source and destination Ip's. source path : /home/UNIX/server filename:abc.txt Destination folder: D:/UNIX/test I am using AIX server. Type of shell :... (1 Reply)
Discussion started by: NareshN
1 Replies

3. Shell Programming and Scripting

Avoiding file overwrite during file transfer using scp

Hi, I have written a small script to transfer a file from one unix server to other using scp command which is working fine. As I know with scp, if any file with the same name is already present on destination server, it would get overwritten without any notification to user. Could anyone help me... (14 Replies)
Discussion started by: dsa
14 Replies

4. Programming

File transfer in C

HI Can anyone provide me with codes for file transfer server to client or vice versa? Also please explain how to compile those programs in ubuntu terminal as i am totally new to socket programming. Thanks (0 Replies)
Discussion started by: mayhemtrigger
0 Replies

5. Programming

Sending large file through sockets

Hello, I am trying to send a file (1 MB) through sockets and for some reason I am getting segmentation fault message and receiving in the server an incomplete file. The current file is about 3 pages long and need to send it from the client to the server. I've tried malloc with free(), I am now... (8 Replies)
Discussion started by: sfcddm1
8 Replies

6. Programming

Problem in file transfer using sockets

Hai Friends I am writing a c program to transfer files from one system to another using TCP/IP socket programming.. My Recieve Program #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> extern int errno; extern char *sys_erlist; void... (2 Replies)
Discussion started by: collins
2 Replies

7. Shell Programming and Scripting

File transfer

Hi All, it might not be an sound question, i have two server like A and B.. i want to transfer file from B to A ..here i have some questions.. 1) do we need to create private and public key to connect..and transferring files...from B to A..? 2) i tried with scp options like... (2 Replies)
Discussion started by: Shahul
2 Replies

8. UNIX for Dummies Questions & Answers

Transfer the file

Dear all, Can anybody let me know how to automate a file transfer process to a remote m/c thru SFTP , automate means it will not prmpt for password. how i am going to achive this....and what all methods are available or tools are available ???? (2 Replies)
Discussion started by: manas_ranjan
2 Replies

9. Shell Programming and Scripting

file transfer

hi all how do i copy a file from one server to another thanks bkan77 (4 Replies)
Discussion started by: bkan77
4 Replies

10. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies
Login or Register to Ask a Question