Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Send Data form Socket to ServerSocket in Android?
    primarykey
    data
    text
    <p>I want to send a file from client to Server by using Socket Programming. I unable to transfer this file, client side is giving message OK, server get freeze at serverClient.accept,and only dispalys Listening on<br> Ip: 10.81.81.125, I am so confused, Kindly help. Thanks in advance.</p> <pre><code>Client Code: public class uploadData extends AsyncTask&lt;String, String, String&gt; { @Override public void onPreExecute() { } catch (Exception e) { } } @Override protected String doInBackground(String... arg0) { try { InetAddress serverAddr = InetAddress.getByName(serverIpAddress); Log.d("ClientActivity", "C: Connecting..."); Socket socket = new Socket(serverAddr, Constants.SERVERPORT); socket.setSoTimeout(90000); connected = true; if (connected) { try { Log.d("ClientActivity", "C: Sending command."); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket .getOutputStream())), true); try { // where you issue the commands File sFile = new File(filePath); BufferedInputStream buffIn = null; buffIn = new BufferedInputStream( new FileInputStream(sFile)); out.print(buffIn); } catch (Exception e) { // TODO: handle exception } // setText(); // out.println("Hey Server!"); Log.d("ClientActivity", "C: Sent."); } catch (Exception e) { Log.e("ClientActivity", "S: Error", e); } } socket.close(); Log.d("ClientActivity", "C: Closed."); } catch (Exception e) { e.printStackTrace(); Toast.makeText(SynServer.this,getString(R.string.noServer), Toast.LENGTH_SHORT).show(); connected = false; } return null; } @Override protected void onProgressUpdate(String... progress) { // TODO Auto-generated method stub super.onProgressUpdate(progress); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } } @Override protected void onDestroy() { Runtime.getRuntime().gc(); super.onDestroy(); } } </code></pre> <p>Server Code:</p> <pre><code>public class Socket_File_ServerActivity extends Activity { private TextView serverStatus; // default ip public static String SERVERIP = "10.0.2.15"; // designate a port public static final int SERVERPORT =12345; private Handler handler = new Handler(); private ServerSocket serverSocket; Socket client=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); serverStatus = (TextView) findViewById(R.id.server_status); SERVERIP = getLocalIpAddress(); // Thread fst = new Thread(new ServerThread()); fst.start(); } public class ServerThread implements Runnable { public void run() { try { Looper.prepare(); if (SERVERIP != null) { handler.post(new Runnable() { public void run() { serverStatus.setText("Listening on IP: " + SERVERIP); } }); serverSocket = new ServerSocket(SERVERPORT); handler.post(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString() , Toast.LENGTH_LONG).show(); serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString()); } }); Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString() , Toast.LENGTH_LONG).show(); serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString()); while (true) { // listen for incoming clients Socket client = serverSocket.accept(); handler.post(new Runnable() { public void run() { serverStatus.setText("Connected."); } }); try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String line = null; while ((line = in.readLine()) != null) { Log.d("ServerActivity", line); final String myline=new String(line); handler.post(new Runnable() { public void run() { // tv_chatbox.setText("Client said:="+myline); // do whatever you want to the front end // this is where you can be creative } }); } break; } catch (Exception e) { handler.post(new Runnable() { public void run() { serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones."); } }); e.printStackTrace(); } } } else { handler.post(new Runnable() { public void run() { serverStatus.setText("Couldn't detect internet connection."); } }); } } catch (final Exception e) { handler.post(new Runnable() { public void run() { serverStatus.setText("Error"+e.getMessage()); } }); e.printStackTrace(); } } } // gets the ip address of your phone's network private String getLocalIpAddress() { try { for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration&lt;InetAddress&gt; enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("ServerActivity", ex.toString()); } return null; } @Override protected void onStop() { super.onStop(); try { // make sure you close the socket upon exiting serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload