After an exhaustive search on the internet, I managed to assemble a code that sends data to a server socket created in VB.NET.
' Here is my VB.NET code
Imports System.Net.Sockets
Imports System.Text
Imports System.Net
Public Module MainModule
Private TcpListener As New TcpListener(IPAddress.Parse("10.0.0.100"), 11000)
Dim TcpClient As New TcpClient
Dim NetworkStream As NetworkStream
Public Sub Main()
TcpListener.Start()
While (True)
TcpClient = TcpListener.AcceptTcpClient()
NetworkStream = TcpClient.GetStream()
Dim r_byt(TcpClient.ReceiveBufferSize) As Byte
Dim r_byt_size As Integer = NetworkStream.Read(r_byt, 0, TcpClient.ReceiveBufferSize) - 1
Dim data As String = Encoding.ASCII.GetString(r_byt, 0, r_byt_size)
Console.WriteLine(data)
Dim s_byt() As Byte = Encoding.ASCII.GetBytes("testing")
NetworkStream.Write(s_byt, 0, s_byt.Length)
NetworkStream.Flush()
NetworkStream.Close()
TcpClient.Close()
End While
TcpListener.Stop()
End Sub
End Module
// Here is my Android code
package com.javacodegeeks.android.androidsocketclient;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Client extends Activity {
private static final String SERVER_IP = "10.0.0.100";
private static final int SERVER_PORT = 11000;
private Socket socket;
private EditText InputText = null;
private Button ButtonSend = null;
private TextView LabelReceived = null;
private Thread thread = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputText = (EditText)findViewById(R.id.InputText);
ButtonSend = (Button)findViewById(R.id.ButtonSend);
LabelReceived = (TextView)findViewById(R.id.LabelReceived);
thread = new Thread(new ClientThread());
if (thread != null) {
thread.start();
}
ButtonSend.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
if (socket != null) {
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println(InputText.getText().toString());
out.flush();
out.close();
} else {
LabelReceived.setText("socket is null!");
}
} catch (Exception e) {
LabelReceived.setText(e.getMessage());
}
}
});
}
class ClientThread implements Runnable {
public void run() {
try {
InetAddress IAddress = InetAddress.getByName(SERVER_IP);
socket = new Socket(IAddress, SERVER_PORT);
} catch (Exception e) {
socket = null;
}
}
}
}
The two are working, when I press the "send button" it goes to the server, but when I try to do it again nothing is received.
Related
Following is "TCP SERVER" code, when any client connect first time the code is run very well, but once that client disconnect and again connect with TCP SERVER, than client connects successfully but can not send data.
How to detect client is disconnected with server?
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
public class MainActivity extends AppCompatActivity {
TextView tvStatus;
EditText etPort, etMsg, etTransmit;
Button btnListen, btnServerSend;
Handler UIHandler;
private int SERVERPORT1;
private InetAddress addr;
private ServerSocket serverSocket;
private Socket clientSocket = null;
private BufferedReader in;
private PrintWriter out;
// public static final int SERVERPORT = 3000;
private WifiManager wifiManager = null;
private WifiManager.WifiLock lock = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi();
UIHandler = new Handler();
tvStatus = (TextView) findViewById(R.id.tvStatus);
etMsg = (EditText) findViewById(R.id.etMsg);
etTransmit = (EditText) findViewById(R.id.etTransmit);
etPort = (EditText) findViewById(R.id.etPort);
btnListen = (Button) findViewById(R.id.btnListen);
btnServerSend = (Button) findViewById(R.id.btnServerSend);
// ******************* Register controls end ******************************
btnListen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ServerClass().execute(etPort.getText().toString());
}
});
}
public String getLocalIpAddress() throws Exception {
String resultIpv6 = "";
String resultIpv4 = "";
for (Enumeration en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements(); ) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if (inetAddress instanceof Inet4Address) {
resultIpv4 = inetAddress.getHostAddress().toString();
} else if (inetAddress instanceof Inet6Address) {
resultIpv6 = inetAddress.getHostAddress().toString();
}
}
}
}
return ((resultIpv4.length() > 0) ? resultIpv4 : resultIpv6);
}
private class ServerClass extends AsyncTask<String, Object, String> {
private String input;
#Override
protected String doInBackground(String... params) {
try {
SERVERPORT1 = Integer.parseInt(params[0]);
addr = InetAddress.getByName(getLocalIpAddress());
serverSocket = new ServerSocket(SERVERPORT1, 0, addr);
while (true) {
clientSocket = serverSocket.accept();
clientSocket.setTcpNoDelay(true);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
tvStatus.setText(clientSocket.getInetAddress().toString() + " Client connected");
Toast.makeText(MainActivity.this, "Server started...", Toast.LENGTH_SHORT).show();
}
});
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
if (!Thread.currentThread().isInterrupted()) {
input = in.readLine();
if (input != null) {
UIHandler.post(new updateUIThread1(input.toString()));
out.println("Received from client: " + input);
} else {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
new ServerClass().execute(etPort.getText().toString());
}
});
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
private class updateUIThread1 implements Runnable {
private String input = null;
public updateUIThread1(String input) {
this.input = input;
}
#Override
public void run() {
if (input.getBytes().length != 0) {
etMsg.append("\n" + input.toString());
Toast.makeText(MainActivity.this, "Answer: " + input, Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Empty", Toast.LENGTH_SHORT).show();
}
}
}
}
Log shows like:
04-22 12:57:05.799 10857-10931/com.chat.admin.clientserver I/System.out: [CDS]shutdownInput in read
04-22 12:57:05.799 10857-10931/com.chat.admin.clientserver I/System.out: [CDS]shutdownInput in read
04-22 12:57:05.799 10857-10931/com.chat.admin.clientserver I/System.out: [CDS]shutdownInput in read
04-22 12:57:05.799 10857-10931/com.chat.admin.clientserver I/System.out: [CDS]shutdownInput in read
04-22 12:57:05.799 10857-10931/com.chat.admin.clientserver I/System.out: [CDS]shutdownInput in read
read: unexpected EOF!
Do not create a new server if a client disconnects.
Instead you should have a loop so you can call .accept() again to wait for a new client.
I would like to add a webserver to my android application for uploading small files to the phone.
The user would start the webserver from the phone by hitting a button. He would then see an ip address that can be accessed by any browser from a pc. The website behind this ip address should show a file upload opportunity.
My question is: Is there an open source project similar to my needs? Or how would you recommend doing this?
you can use NanoHttpd link it's very weight android web server that is nicely embbedible..
package .....;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import android.app.Activity;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class AndroidWebServerActivity extends Activity {
private static final int PORT = 8765;
private TextView hello;
private MyHTTPD server;
private Handler handler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
protected void onResume() {
super.onResume();
try {
server = new MyHTTPD();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onPause() {
super.onPause();
if (server != null)
server.stop();
}
private class MyHTTPD extends NanoHTTPD {
public MyHTTPD() throws IOException {
super(PORT, null);
}
#Override
public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
final StringBuilder buf = new StringBuilder();
for (Entry<Object, Object> kv : header.entrySet())
buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
handler.post(new Runnable() {
#Override
public void run() {
}
});
final String html = "<html><head><head><body><h1>Hello, World</h1></body></html>";
return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, html);
}
}
}
I'm trying to make a client in Android. This clients runs a thread that creates the client socket and launches another thread that is always listening to the socket to receive Strings. Everything is OK when I send a String from the client to a Java server running in a PC, but when I send a String from the server to the Android client the app finishes. Why do I get this error?
Here is the code of the main Activity of the client:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView chatHistorial;
EditText msg;
Socket client;
DataInputStream in;
DataOutputStream out;
Boolean cerrar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t = new Thread(new Runnable(){
#Override
public void run() {
try{
client = new Socket("192.168.1.33", 4444);
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
cerrar = false;
chatHistorial = (TextView) findViewById(R.id.chatHistorial);
msg = (EditText) findViewById(R.id.msg);
ThreadLectura tl = new ThreadLectura(in, cerrar, chatHistorial);
tl.start();
}
catch(Exception e){
// ...
}
}
});
t.start();
findViewById(R.id.enviar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String cadena = msg.getText().toString();
try {
out.writeUTF(cadena);
} catch (IOException e) {
// ...
}
if(cadena.equals("exit"))
cerrar = true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
class ThreadLectura extends Thread{
DataInputStream in;
String cadena;
TextView chatHistorial;
Boolean cerrar;
public ThreadLectura(DataInputStream in, Boolean cerrar, TextView tv){
this.in = in;
this.cerrar = cerrar;
chatHistorial = tv;
}
#Override
public void run(){
try{
while(!cerrar){
cadena = in.readUTF();
chatHistorial.append("Has recibido: " + cadena);
}
}
catch(IOException ioe){
System.out.println("Error de entrada/salida: "+ioe.getMessage());
}
}
}
It's hard to say without seeing your logcat output, but I'm betting it's because you are attempting to modify the UI from within your background thread. This line within ThreadLectura:
chatHistorial.append("Has recibido: " + cadena);
is probably the issue, as chatHistorial is a TextView. You need to only modify the UI from within the main UI thread.
I need to show toast message when the server is not responding
when I press the login button, some parameters are passed to AgAppMenu screen which use url connection to server and get xml response in AgAppHelperMethods screen. The
probelm is when the server is busy or the network is not avaibale, I can't show toast message on catch block although it shows the log message.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent ;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginScreen extends Activity implements OnClickListener {
EditText mobile;
EditText pin;
Button btnLogin;
Button btnClear;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.agapplogin);
TextView lblMobileNo = (TextView) findViewById(R.id.lblMobileNo);
lblMobileNo.setTextColor(getResources()
.getColor(R.color.text_color_red));
mobile = (EditText) findViewById(R.id.txtMobileNo);
TextView lblPinNo = (TextView) findViewById(R.id.lblPinNo);
lblPinNo.setTextColor(getResources().getColor(R.color.text_color_red));
pin = (EditText) findViewById(R.id.txtPinNo);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnClear = (Button) findViewById(R.id.btnClear);
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
postLoginData();
}
});
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
cleartext();
}
});
/*
*
* btnClear.setOnClickListener(new OnClickListener() { public void
* onClick(View arg0) {
*
* } });
*/
}
public void postLoginData()
{
if (pin.getTextSize() == 0 || mobile.getTextSize() == 0) {
AlertDialog.Builder altDialog = new AlertDialog.Builder(this);
altDialog.setMessage("Please Enter Complete Information!");
} else {
Intent i = new Intent(this.getApplicationContext(), AgAppMenu.class);
Bundle bundle = new Bundle();
bundle.putString("mno", mobile.getText().toString());
bundle.putString("pinno", pin.getText().toString());
i.putExtras(bundle);
startActivity(i);
}
}
#Override
public void onClick(View v) {
}
public void cleartext() {
{
pin.setText("");
mobile.setText("");
}
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AgAppMenu extends Activity {
String mno, pinno;
private String[][] xmlRespone;
Button btnMiniStatement;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agappmenu);
mno = getIntent().getExtras().getString("mno");
pinno = getIntent().getExtras().getString("pinno");
setTitle("Welcome to the Ag App Menu");
AgAppHelperMethods agapp =new AgAppHelperMethods();
// xmlRespone = AgAppHelperMethods.AgAppXMLParser("AG_IT_App/AgMainServlet?messageType=LOG&pin=" + pinno + "&mobile=" + mno + "&source=" + mno + "&channel=INTERNET");
xmlRespone = agapp.AgAppXMLParser("AG_IT_App/AgMainServlet?messageType=LOG&pin=" + pinno + "&mobile=" + mno + "&source=" + mno + "&channel=INTERNET");
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnKeyListener;
public class AgAppHelperMethods extends Activity {
private static final String LOG_TAG = null;
private static AgAppHelperMethods instance = null;
public static String varMobileNo;
public static String varPinNo;
String[][] xmlRespone = null;
boolean flag = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agapphelpermethods);
}
protected AgAppHelperMethods() {
}
public static AgAppHelperMethods getInstance() {
if (instance == null) {
instance = new AgAppHelperMethods();
}
return instance;
}
public static String getUrl() {
String url = "https://demo.accessgroup.mobi/";
return url;
}
public String[][] AgAppXMLParser(String parUrl) {
String _node, _element;
String[][] xmlRespone = null;
try {
String url = AgAppHelperMethods.getUrl() + parUrl;
URL finalUrl = new URL(url);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(finalUrl.openStream()));
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName("*");
_node = new String();
_element = new String();
xmlRespone = new String[list.getLength()][2];
// this "for" loop is used to parse through the
// XML document and extract all elements and their
// value, so they can be displayed on the device
for (int i = 0; i < list.getLength(); i++) {
Node value = list.item(i).getChildNodes().item(0);
_node = list.item(i).getNodeName();
_element = value.getNodeValue();
xmlRespone[i][0] = _node;
xmlRespone[i][1] = _element;
}// end for
throw new ArrayIndexOutOfBoundsException();
}// end try
// will catch any exception thrown by the XML parser
catch (Exception e) {
Toast.makeText(AgAppHelperMethods.this,
"error server not responding " + e.getMessage(),
Toast.LENGTH_SHORT).show();
Log.e(LOG_TAG, "CONNECTION ERROR FUNDAMO SERVER NOT RESPONDING", e);
}
// Log.e(LOG_TAG, "CONNECTION ERROR FUNDAMO SERVER NOT RESPONDING", e);
return xmlRespone;
}
`
AgAppHelperMethods isn't really an Activity. You've derived this class from Activity, but then you've created Singleton management methods (getInstance()) and you are instantiating it yourself. This is bad. Don't do this.
Normally Android controls the instantiation of activities. You don't ever create one yourself (with new).
It looks to me like AgAppHelperMethods just needs to be a regular Java class. It doesn't need to inherit from anything. Remove also the lifecycle methods like onCreate().
Now you will have a problem with the toast, because you need a context for that and AgAppHelperMethods isn't a Context. To solve that you can add Context as a parameter to AgAppXMLParser() like this:
public String[][] AgAppXMLParser(Context context, String parUrl) {
...
// Now you can use "context" to create your toast.
}
When you call AgAppXMLParser() from AgAppMenu just pass "this" as the context parameter.
I have an Activity that retrieves information from a url. I have 2 textviews which are multi-line (txtTitle & txtContent). If I populate them without using thread, it displays as multi-line. However, when I use a thread, the text views become single-line.
Is there anything I'm missing? Is the UI not completely ready when I tried putting text in it?
My sample code is below:
package com.mysite.app;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;
public class ViewNewsActivity extends Activity implements Runnable{
private ProgressDialog progressDialog = null;
private String apiUrl = "";
private String title = "";
private String content = "";
private Runnable viewNews;
private TextView txtTitle = null;
private TextView txtContent = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_news);
Intent myIntent = getIntent();
apiUrl = "http://www.mysite.com" + myIntent.getStringExtra("api_url");
Thread thread = new Thread(this);
thread.start();
progressDialog = ProgressDialog.show(ViewNewsActivity.this,
"Please wait...", "Retrieving News ...", true);
}
public void run() {
txtTitle = (TextView)findViewById(R.id.view_news_title);
txtContent = (TextView)findViewById(R.id.view_news_content);
getNews(apiUrl);
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
progressDialog.dismiss();
txtTitle.setText(title);
Spanned contentHtml = Html.fromHtml(content);
txtContent.setText(contentHtml.toString());
}
};
private void getNews(String apiUrl) {
String result = JSONUtils.getJSONResponse(apiUrl);
JSONObject jsonResponse;
try {
jsonResponse = new JSONObject(result);
title = jsonResponse.getString("title");
content = jsonResponse.getString("content");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}