akash priyadarshi

Greenhorn
+ Follow
since May 18, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by akash priyadarshi

thanks .

I tried to create my protocol , but it did not worked . But by adjusting buffer size i make it working now.
9 years ago
I have written java code using socket programming to transfer files between android(aakash tablet) it works fine with files of small size . but for large file it shows :
java.net.SocketException: Connection reset.

Please suggest any change to so, that i can even transfer large file (like introducing buffer(but i do not know how to use that ))

here is code for sending file :


package javaapplication1;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.io.File ;
/**
*
* @author akash
*/
public class Transfer {
public static void fileTransfer(Socket client, File file ,DataOutputStream out) {
try {


System.out.println(file.length());
String length= ""+file.length();
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream socketOutputStream = client.getOutputStream();
long startTime = System.currentTimeMillis();

byte buffer[]= new byte[64] ;
int read;
int readTotal = 0;
out.writeUTF(length);

System.out.println(file.length());
for(int j =(int)file.length();j>0;j-=64) {
read = fileInputStream.read(buffer);
socketOutputStream.write(buffer, 0, read);
readTotal += read;
}


long endTime = System.currentTimeMillis();
System.out.println(readTotal + " bytes written in " + (endTime - startTime) + " ms.");
Thread.sleep(10000);

//socketOutputStream.close();
fileInputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}
}


here is code for receiving the file :

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Accept {

public void FileAccept(InputStream clientInputStream, String fileName,
DataInputStream in) {

FileOutputStream file;
boolean flag = true;
System.out.print("hi file transfer start in few sec...");

try {
long startTime = System.currentTimeMillis();

file = new FileOutputStream(fileName, true);
System.out.println("file trsnsfer start of file");
String s = in.readUTF();
byte[] buffer = new byte[64];
int read;
int totalRead = 0;

int i = Integer.parseInt(s);
System.out.println(i + " " + s);
for (int j = i; j > 0; j -= 64) {

{
if(j<64){buffer=new byte[j];}
read = clientInputStream.read(buffer);
file.write(buffer);
}
totalRead += read;
}
System.out.println(fileName);
file.flush();
file.close();
long endTime = System.currentTimeMillis();
System.out.println(totalRead + " bytes read in "
+ (endTime - startTime) + " ms.");
} catch (FileNotFoundException f) {
System.out.println("error two " + f);
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}

}
}

thanks in advance.

9 years ago
I have written an application which works fine on system but now i want to port that application on android can someone suggest me good book in android for socket programming . I am new in android and only know the basics of android.
9 years ago
I have written an application which works fine on system but now i want to port that application on android can someone suggest me good book in android for socket programming . I am new in android and only know the basics of android.
hi, i am new in android development . i have written this code to get my ip address but it is returning null . please point out my mistake in the code.


package quiz.invigilator;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class QuizActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String s = localip();
TextView tv = new TextView(this);
tv.setText("Hello, Android" + s);
setContentView(tv);


}

protected String localip()
{
String localip = "a";
int i = 0;
try{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();i++) {
localip += i ;
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

InetAddress inetAddress = enumIpAddr.nextElement();
localip += 'c' ;
if (!inetAddress.isLoopbackAddress()) {
localip+= 'd';
return inetAddress.getHostAddress().toString();
}
}
}
}catch (Exception e) {

Log.e("ServerActivity", e.toString());

}
return localip ;


}
}
9 years ago
I want code for secure socket connection between two android (Aakash tabet) can any one please suggest me the code to perform this operation.
9 years ago