Java sending and receiving file (byte[]) over sockets
A. BufferedOutputStream
Program Server
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(4444);
Socket server = null;
server = serverSocket.accept();
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));
byte[] bytes = new byte[1024];
in.read(bytes);
System.out.println(bytes);
FileOutputStream fos = new FileOutputStream("C:\\Users\\Derry\\Documents\\MATLAB\\RAA\\sukses1.tx t");
fos.write(bytes);
}
}
B. Download all code here
Program Server
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(4444);
Socket server = null;
server = serverSocket.accept();
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));
byte[] bytes = new byte[1024];
in.read(bytes);
System.out.println(bytes);
FileOutputStream fos = new FileOutputStream("C:\\Users\\Derry\\Documents\\MATLAB\\RAA\\sukses1.tx t");
fos.write(bytes);
}
}
B. Download all code here
Comments
Post a Comment