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.

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?

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 :slight_smile:

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.

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 .

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'.

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:

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.

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 :slight_smile:

It's ok :slight_smile: . Every help i can get is useful :slight_smile: