Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending files with sockets 1

Status
Not open for further replies.

mattiaswesterberg

Programmer
Jul 27, 2005
5
SE
Hello!

I have problem to send a .exe file with socket. The copy of the file gets the same size as the original file. But it does not seem to have the same content. What is wrong with my code?

I want you to reply only if you know the answer on this specific problem. I dont want links to webpages about sockets.

Thank you.

Mattias Westerberg

The client code:

import java.net.*;
import java.io.*;

class Client
{
public static void main(String a[])
{
Socket s = null;

try
{
s = new Socket("localhost", 3000);

DataOutputStream os = new DataOutputStream(s.getOutputStream());
PrintWriter out = new PrintWriter(os, true);

FileInputStream stream = new FileInputStream("1.exe");
File f = new File("1.exe");

System.out.println("Filesize: " + (int)f.length());

byte[] b = new byte[(int)f.length()];

stream.read(b);
out.println(f.length());
os.write(b, 0, b.length);

stream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
s.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}


The server code:

import java.net.*;
import java.io.*;

class Server
{
public static void main(String a[])
{
Socket s = null;
ServerSocket ss = null;

try
{
ss = new ServerSocket(3000);
s = ss.accept();
ss.close();

DataInputStream is = new DataInputStream(s.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
FileOutputStream stream = new FileOutputStream("2.exe");

String fl = in.readLine();
long fileLength = Long.parseLong(fl);

System.out.println("Filesize: " + fileLength);

byte[] b = new byte[(int)fileLength];

is.read(b);
stream.write(b);

stream.flush();
stream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
s.close();
ss.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
 
I want you to reply only if you know the answer on this specific problem. I dont want links to webpages about sockets.

Asking for help and specifying how one should reply.[rofl]

On a site that gives you free advice, I wouldn't be so demanding in the future - you'll not make many friends that way.

However, I'm bored, so I will reply helpfully.

I think you are messing the stream up by using a PrintWriter aswell as a DataOutputStream. The following is a little closer to how I would write your code.
If you need to send extra details like file names etc, then I would NOT do it on one write - but use a couple of read/writes on each side of the socket - eg you send a header, read the header, send an ack, then send the real data. This I leave to you.
Your code is also not a great "server", because it only handles one client and then exits. I have shown you how to run a multithread server. Good for me.

Server.java :
Code:
import java.net.*;
import java.io.*;

class Server {
    	public static void main(String a[]) throws IOException {
		new Server().listen(3000);
    	}

	public void listen(int port) throws IOException {
		ServerSocket ss = new ServerSocket(port);
		while (true) {
			new SocketHandler(ss.accept()).start();
		}
	}

	class SocketHandler extends Thread {
		Socket s = null;
		SocketHandler(Socket s) {
			this.s = s;
		}

		public void run() {
			try {
				File f = new File("2.exe");
				OutputStream fos = new FileOutputStream(f);
				BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
				byte[] buffer = new byte[8192];
				int i = -1;
		
				while (true) {
					i = bis.read(buffer, 0, buffer.length);
					if (i == -1) break;
					fos.write(buffer, 0, i);
				}
		
				fos.flush();
				fos.close();
	
				bis.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} finally {
				try { s.close(); } catch (IOException ioe) { ioe.printStackTrace(); }
			}
		}
	}
}
Client.java :
Code:
import java.net.*;
import java.io.*;

class Client
{
    public static void main(String a[])
    {
        Socket s = null;

        try
        {
            s = new Socket("localhost", 3000);

            DataOutputStream os = new DataOutputStream(s.getOutputStream());

            FileInputStream fis = new FileInputStream("1.exe");
            File f = new File("1.exe");

            System.out.println("Filesize: " + (int)f.length());
            
            byte[] b = new byte[(int)f.length()];
	    fis.read(b);
	    fis.close();

            os.write(b);

	    os.flush();
            os.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                s.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();    
            }
        }
    }
}



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top