I am using the sample from Microsoft's Async Socket Listener. I got everything working fine, I am able to send files and data etc. However, I am struggling with trying to keep the socket open. I do not want to close the socket and re-open for every file I am sending. I want to keep it open until all files are sent. I assume the issue is in the 'SendCallback' but I can not seem to get it to work.
.Net Code
Private Shared Sub SendCallback(ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim handler As Socket = DirectCast(ar.AsyncState, Socket)
' Complete sending the data to the remote device.
Dim bytesSent As Integer = handler.EndSend(ar)
'trying to receive again - I added this after commenting out the below lines but am missing something.
Dim state As StateObject = DirectCast(ar.AsyncState, StateObject)
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
'The below lines are what was there, I tried to comment out and use the above lines trying to receive again but it does not work. I assume I am missing something simple but can not find anything useful. Any help would be appreciated.
'handler.Shutdown(SocketShutdown.Both)
'handler.Close()
Catch e As Exception
PubVars.ServerStatus = e.ToString
End Try
End Sub
I know I do not want to close the socket until all is sent but I am not sure what i am missing. I want to send the first file, then send back to the client a status update which all works well. Then when the client receives the status update, I want to send the next file and so on.
Android Code:
Socket socket = null;
try {
socket = new Socket("localhost", 11000);
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);
String FileName = "my.jpg";
String FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
File f= new File(FilePath);
byte[] data= readFileToByteArray(f);
String strbase64 = Base64.encodeToString(data, Base64.DEFAULT);
//I chose to pass the filename as part of my header string then parse it out on server
output.println("<HEADER>" + FileName + "</HEADER>" + strbase64 + "<EOF>");
output.flush();
InputStream in = socket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder returnString = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
returnString.append(line).append('\n');
}
Log.i("INPUT",returnString.toString());
//If true run the next one
//SEND NEW FILE
Log.i("NEXT","STARTING NEXT FILE");
FileName = "my2.png";
FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
f= new File(FilePath);
data= readFileToByteArray(f);
strbase64 = Base64.encodeToString(data, Base64.DEFAULT);
output.println("<HEADER>" + FileName + "</HEADER>" + strbase64 + "<EOF>");
output.flush();
in = socket.getInputStream();
r = new BufferedReader(new InputStreamReader(in));
returnString = new StringBuilder();
while ((line = r.readLine()) != null) {
returnString.append(line).append('\n');
}
Log.i("INPUT",returnString.toString());
//END ANOTHER FILE
output.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
Related
While reading data from a stream I am writing to a newly created temporary XML file. But the program hangs.
What have I missed here?
if (deviceSocket.isConnected()) {
OutputStream os = deviceSocket.getOutputStream();
os.write(xmlStr.getBytes());
os.flush();
// Read Response from Socket
respFromDevice = new BufferedReader(new InputStreamReader(deviceSocket.getInputStream()));
FileWriter fileWriter = null;
// Write into temp xml file
String response;
while ((response = respFromDevice.readLine()) != null) {
sb.append(response);
File newTextFile = new File("tmp.xml");
fileWriter = new FileWriter(newTextFile);
fileWriter.write(sb.toString());
//Files.write(Paths.get("temp.xml"), sb.toString().getBytes());
System.out.println(response);
}
}
I am writing data in file continuously in append mode using FileOutputStream. Everything is working fine but I want to separate each appended stream from file while reading it.
Here is how I created file and writing it in Android
FileOutputStream outputStream = service.openFileOutput("text.txt", Context.MODE_APPEND);
outputStream.write(measurement.toString().getBytes());
outputStream.close();
It is appending data successfully but when I am reading it I do not know how to find end point between appended strings.
Here is my code to read the string from file
StringBuilder total = new StringBuilder();
FileInputStream inputStream = service.openFileInput("text.txt");
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
Log.d(TAG, "File Size: "+total.length());
For future references, it is silly that I have not tried but thanks to #greenapps
I just had to add another write statement to append
FileOutputStream outputStream = service.openFileOutput("text.txt", Context.MODE_APPEND);
outputStream.write(measurement.toString().getBytes());
outputStream.write("\n".getBytes());
outputStream.close();
for reading saparated string I simply used split for java
String[] parts = total.toString().split("\n");
for(int i = 0; i < parts.length; i++) {
Log.d(TAG, parts[i]);
}
deleteFile("text.txt");
Hi My ultimate aim is to transfer files using wifi direct api in android between two devices. Once device act as a client another one act as a server as in the wifi direct sdk demo. For this, Creating a socket from the client side by using server port and host address. I want to transfer multiple files. In the receiver side while accepting the client socket connection, I have to create the file with the filename of the file which sent from the client side. But I dont know that filename from the server side.
So how to send file name using socket connection for this wifi direct transfer mode for multiple file transfer.
Creating socket from client side using server port and host address:
fileUris = intent.getExtras().getParcelableArrayList(EXTRA_STREAM);
String host = intent.getExtras().getString(
EXTRAS_GROUP_OWNER_ADDRESS);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
try {
Log.d(WifiDirectActivity.TAG, "Opening client socket - ");
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)),
SOCKET_TIMEOUT);
Log.d(WifiDirectActivity.TAG,
"Client socket - " + socket.isConnected());
OutputStream stream = socket.getOutputStream();
ContentResolver cr = context.getContentResolver();
InputStream is = null;
for (int i = 0; i < fileUris.size(); i++) {
Uri uri = fileUris.get(0);
try {
is = cr.openInputStream(Uri.parse(uri.toString()));
} catch (FileNotFoundException e) {
Log.d(WifiDirectActivity.TAG, e.toString());
}
DeviceDetailFragment.copyFile(is, stream);
Log.d(WifiDirectActivity.TAG, "Client: Data written");
}
Accepting client socket connection form server side:
ServerSocket serverSocket = new ServerSocket(8988);
Log.d(WifiDirectActivity.TAG, "Server: Socket opened");
Socket client = serverSocket.accept();
Log.d(WifiDirectActivity.TAG,
"Server: connection done with client");
final File f = new File(
Environment.getExternalStorageDirectory() + "/"
+ context.getPackageName() + "/wifip2pshared-"
+ "sample");
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
Log.d(WifiDirectActivity.TAG,
"server: copying files " + f.toString());
InputStream inputstream = client.getInputStream();
copyFile(inputstream, new FileOutputStream(f));
serverSocket.close();
Really struck on giving file name at the receiver side at file creation. Is there any way to send file name along with that.
please Help me on this. Thanks in advance.
You could create a bundle object that embeds both the file name and the actual data. Something like this:
public class WiFiDirectBundle extends Serializable {
private String fileName;
private String mimeType;
private Long fileSize;
private byte[] fileContent;
public WiFiDirectBundle() {}
// adds a file to the bundle, given its URI
public void setFile(Uri uri) {
File f = new File(Uri.parse(uri.toString()));
fileName = f.getName();
mimeType = MimeTypeMap.getFileExtensionFromUrl(f.getAbsolutePath());
fileSize = f.length();
FileInputStream fin = new FileInputStream(f);
fileContent = new byte[(int) f.length()];
fin.read(fileContent);
}
// restores the file of the bundle, given its directory (change to whatever
// fits you better)
public String restoreFile(String baseDir) {
File f = new File(baseDir + "/" + fileName);
try {
FileOutputStream fos = new FileOutputStream(f);
if (fileContent != null) {
fos.write(fileContent);
}
fos.close();
return f.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getFileName() {
return fileName;
}
public String getMimeType() {
return mimeType;
}
public Long getFileSize() {
return fileSize;
}
}
Then, you can pass an instance of a WiFiDirectBundle back and forth by simply using the input and output streams. When you receive an object, you must explicitly cast it to the WiFiDirectBundle class.
I know it's not elegant, but it actually works.
I'm trying to read some text from a .txt file, here's my code:
String filePath = bundle.getString("filepath");
StringBuilder st = new StringBuilder();
try {
File sd = Environment.getExternalStorageDirectory();
File f = new File(sd, filePath);
FileInputStream fileis = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileis));
String line = new String();
while ((line = buf.readLine()) != null) {
st.append(line);
st.append('\n');
}
Log.i("egor", "reading finished, line is " + line);
} catch (FileNotFoundException e) {
Log.i("egor", "file not found");
} catch (IOException e) {
Log.i("egor", "io exception");
}
reader.setText(st.toString());
The text looks like this:
This is a sample text to test
The .txt file is created in Windows notepad.
And here's what I'm getting:
What's wrong with my code? Thanks in advance.
Is the file in utf-8 (unicode) format? For some reason, Notepad always adds a byte-order mark to unicode files, even when the byte-order is irrelevant. When interpreted as ASCII or ANSI, the BOM will be seen as several characters. It's possible this is what's causing your problem.
If so, the solution is to use a more competent text editor than Notepad, or write code that checks for a BOM first in all unicode files.
If none of this makes sense to you, try googling 'unicode' and 'byte-order mark'.
Wrap a FileReader object in the BufferedReader object instead.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
st.append(line);
st.append("\n");
}
br.close();
Try with the folowing code
File f = new File(str);
FileInputStream fis = new FileInputStream(f);
byte[] mydata1 = new byte[(int) f.length()];
fis.read(mydata1);
System.out.println("...data present in 11file..."+new String(mydata1));
The following code works, but takes way too long (over a minute) to open a small file. The LogCat shows a lot of instances of "GC_FOR_MALLOC freed #### objects / ###### bytes in ##ms". Any suggestions?
File dirPath = new File(Environment.getExternalStorageDirectory(), "MyFolder");
String content = getFile("test.txt");
public String getFile(String file){
String content = "";
try {
File dirPathFile = new File(dirPath, file);
FileInputStream fis = new FileInputStream(dirPathFile);
int c;
while((c = fis.read()) != -1) {
content += (char)c;
}
fis.close();
} catch (Exception e) {
getLog("Error (" + e.toString() + ") with: " + file);
}
return content;
}
Update:
This is what it looks like now:
File dirPath = new File(Environment.getExternalStorageDirectory(), "MyFolder");
String content = getFile("test.txt");
public String getFile(String file){
String content = "";
File dirPathFile = new File(dirPath, file);
try {
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(dirPathFile));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
content = new String(text);
} catch (Exception e) {
getLog("Error (" + e.toString() + ") with: " + file);
}
return content;
}
Thank you all!!
Using += on a String is extremely inefficient - it will constantly allocate and deallocate memory, something you need to avoid!
If you need to constantly add characters, use a StringBuilder and give it a sufficiently big buffer up front.
However, it's even better to just read the entire file as a byte array and then create a string from that byte array. Use the String(byte[]) constructor.
content += (char)c;
Well, here's your problem. String concatenation is slow if you have to do it repeatedly. And you're reading the file one character at a time, which is also really slow.
You want to be using the read(byte[] buffer) method to read the file into a buffer efficiently. And then you can stringify the buffer if need be.
Rather than reading a single byte at a time, you should read multiple using read(byte[]).
Also, Strings are immutable, so every time you do String s = s + "a"; there is the possibility that you are creating a new String object. You can use StringBuilder instead to build up a larger string.
Schlemiel the painter strikes again!
try to read with buffer read(byte[] buff)
The reasons are:
You are creating too many String objects with content += (char)c; - use StringBuilder instead to append with read data, then in the end call toString() on the StringBuilder.
You don't use a byte[] (or char[], it depends on implementation) buffer to read from file. Usually 1KB buffer is optimal instead of reading one by one byte.