mattiaswesterberg
Programmer
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 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();
}
}
}
}