hi i transferred an image from server socket to client socket between two android devices. i got the image but it is 0 bytes in size.here is my code
sender:
// LISTEN FOR INCOMING CLIENTS
Socket client = serverSocket.accept();
File myFile = new File("/sdcard/DCIM/d.png");
byte [] mybytearray = new byte[(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = client.getOutputStream();
serverStatus.setText("sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
client.close();
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Connected.");
}
});
receiver:
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
byte[] mybytearray = new byte[filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/sdcard/j.png");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do{
bytesRead = is.read(mybytearray,current,(mybytearray.length-current));
if(bytesRead > 0){
current +=bytesRead;
}
}while(bytesRead > 0);
bos.write(mybytearray,0,current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
socket.close();
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// WHERE YOU ISSUE THE COMMANDS
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
is anything wrong in this code? thanks in advance..