Reputation: 4917
I'm writing an android app that sends an image to a server running a java app, and it's working in a very weird way!
Here is what I do
Following is the code for the RECEIVE part of the Java app:
class ProjectServer
{
ServerSocket serSock;
Socket sock;
BufferedReader in;
PrintWriter out;
public static void main(String ar[])
{
try
{
ProjectServer cs=new ProjectServer();
cs.startServer();
}
catch(Exception e)
{
}
}
public void startServer()
{
try
{
serSock=new ServerSocket(8070);
System.out.println("Waiting for client...");
sock=serSock.accept();
System.out.println("Connections done");
//Accept File
System.out.println("Connected");
//receive code
int filesize=450660;
int bytesRead;
int current=0;
// receive file
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\Project Server\\Capture.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 > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("end-start");
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
}
Following is the code for the SEND part on the Android app:
package com.site.custom;
public class Act2 extends Activity
{
private ProgressDialog pd;
private String serverIP="58.146.100.187";
private BufferedReader in;
private PrintWriter out;
private String path;
private Socket cliSock;
public void onCreate(Bundle onCreateInstance)
{
super.onCreate(onCreateInstance);
setContentView(R.layout.act2);
this.setTitle("This has started");
path=getIntent().getStringExtra("path");
//Establish Connection
try
{
cliSock=new Socket(serverIP,8070);
in=new BufferedReader(new InputStreamReader(cliSock.getInputStream()));
((TextView)findViewById(R.id.tview)).setText(path);
}
catch(Exception e)
{
Log.v("MERA MSG",e.toString());
}
//Send file
ProgressDialog pd=ProgressDialog.show(this, "Sending image", "Image chosen:"+path.substring(path.lastIndexOf("//")+1),false,true);
try
{
File myFile = new File (path);
System.out.println((int)myFile.length());
byte[] mybytearray = new byte[450560];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = cliSock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
System.out.println("Completed");
pd.dismiss();
System.out.println("Done");
}
catch(Exception e)
{
Log.v("MERA MSG",e.toString());
}
}
}
Upvotes: 3
Views: 2506
Reputation: 8774
You're not closing any of the connections. Your Server is probably waiting until the connection is closed before it knows that data has finished being sent. When your Client runs the second time, the first connection is probably closed automatically by Android, this allowing your Server to process the image. However, if your Server doesn't process the image fast enough, maybe your Client gets 'stuck' on the second run because its waiting for the successful port connection to the Server but the Server is busy.
Long story short, how about you try to close()
the Client connection when you're finished with it. Calling flush()
doesn't really tell anything to the Server to say that the data has finished being sent.
If you want the socket to stay open even though you close the OutputStream
, you could write a custom Socket
that just overwrites the close()
method, something like this...
public class MySocket extends Socket {
public MySocket(String ipAddress, int port){
super(ipAddress,port);
}
public void close(){
// do nothing
}
public void reallyClose(){
super.close();
}
}
In your code, change cliSock=new Socket(serverIP,8070);
to cliSock=new MySocket(serverIP,8070);
When you call OutputStream.close()
, it calls close()
on MySocket
, but as you can see it doesn't actually do anything. When you've completely finished with the Socket
, you can call MySocket.reallyClose();
and it'll close it off properly.
Upvotes: 1