Really Simple TCP Client

I want my application to go to the URL of my server, for example. http://192.168.1.8/ and port, for example. 1234 . When my server receives a โ€œTCP Requestโ€ message, it sends back the file (the server is already implemented).

I think that I donโ€™t need something complicated like AsyncTask, since I donโ€™t want to maintain a connection. After receiving a response from the server, my connection should be closed.

Any indication of a way forward or a hint is welcome.

+15
android tcp
source share
4 answers

Here is a simple TCP client that uses the Sockets I worked with based on the code for this lesson (the lesson code can also be found in this GitHub repository ).

Please note that this code is designed to send strings between the client and server, usually in JSON format.

Here is the TCP client code:

 import android.util.Log; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; public class TcpClient { public static final String TAG = TcpClient.class.getSimpleName(); public static final String SERVER_IP = "192.168.1.8"; //server IP address public static final int SERVER_PORT = 1234; // message to send to the server private String mServerMessage; // sends message received notifications private OnMessageReceived mMessageListener = null; // while this is true, the server will continue running private boolean mRun = false; // used to send messages private PrintWriter mBufferOut; // used to read messages from the server private BufferedReader mBufferIn; /** * Constructor of the class. OnMessagedReceived listens for the messages received from server */ public TcpClient(OnMessageReceived listener) { mMessageListener = listener; } /** * Sends the message entered by client to the server * * @param message text entered by client */ public void sendMessage(final String message) { Runnable runnable = new Runnable() { @Override public void run() { if (mBufferOut != null) { Log.d(TAG, "Sending: " + message); mBufferOut.println(message); mBufferOut.flush(); } } }; Thread thread = new Thread(runnable); thread.start(); } /** * Close the connection and release the members */ public void stopClient() { mRun = false; if (mBufferOut != null) { mBufferOut.flush(); mBufferOut.close(); } mMessageListener = null; mBufferIn = null; mBufferOut = null; mServerMessage = null; } public void run() { mRun = true; try { //here you must put your computer IP address. InetAddress serverAddr = InetAddress.getByName(SERVER_IP); Log.d("TCP Client", "C: Connecting..."); //create a socket to make the connection with the server Socket socket = new Socket(serverAddr, SERVER_PORT); try { //sends the message to the server mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); //receives the message which the server sends back mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); //in this while the client listens for the messages sent by the server while (mRun) { mServerMessage = mBufferIn.readLine(); if (mServerMessage != null && mMessageListener != null) { //call the method messageReceived from MyActivity class mMessageListener.messageReceived(mServerMessage); } } Log.d("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'"); } catch (Exception e) { Log.e("TCP", "S: Error", e); } finally { //the socket must be closed. It is not possible to reconnect to this socket // after it is closed, which means a new socket instance has to be created. socket.close(); } } catch (Exception e) { Log.e("TCP", "C: Error", e); } } //Declare the interface. The method messageReceived(String message) will must be implemented in the Activity //class at on AsyncTask doInBackground public interface OnMessageReceived { public void messageReceived(String message); } } 

Then declare TcpClient as a member variable in your Activity:

 public class MainActivity extends Activity { TcpClient mTcpClient; //............ 

Then use AsyncTask to connect to the server and receive responses in the user interface stream (note that messages received from the server are processed in an override of the onProgressUpdate() method in AsyncTask):

 public class ConnectTask extends AsyncTask<String, String, TcpClient> { @Override protected TcpClient doInBackground(String... message) { //we create a TCPClient object mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() { @Override //here the messageReceived method is implemented public void messageReceived(String message) { //this method calls the onProgressUpdate publishProgress(message); } }); mTcpClient.run(); return null; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); //response received from server Log.d("test", "response " + values[0]); //process server response here.... } 

To establish a connection to your server, run AsyncTask:

 new ConnectTask().execute(""); 

Then, by sending a message to the server:

 //sends the message to the server if (mTcpClient != null) { mTcpClient.sendMessage("testing"); } 

You can close the connection to the server at any time:

 if (mTcpClient != null) { mTcpClient.stopClient(); } 
+34
source share

Thanks for the code. In my case, I had problems getting data, since I am not using a linear protocol. I wrote an alternative implementation that works with my specific server setup:

  1. In the TcpClient.java file, the "run ()" command should be replaced with the code fragment below

    public void run () {

     mRun = true; try { InetAddress serverAddr = InetAddress.getByName(SERVER_IP); Log.e("TCP Client", "C: Connecting..."); Socket socket = new Socket(serverAddr, SERVER_PORT); try { mBufferOut = new PrintWriter(socket.getOutputStream()); Log.e("TCP Client", "C: Sent."); mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); int charsRead = 0; char[] buffer = new char[1024]; //choose your buffer size if you need other than 1024 while (mRun) { charsRead = mBufferIn.read(buffer); mServerMessage = new String(buffer).substring(0, charsRead); if (mServerMessage != null && mMessageListener != null) { mMessageListener.messageReceived(mServerMessage);} mServerMessage = null; } Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'"); 

// the rest of the code is fine, see original

  1. doInBackgroud in your MainActivity.java publishes the received message in onProgressUpdate, you can display it in another object, for example TextView

     @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); Log.d("test", "response " + values[0]); response.setText(response.getText() + "/n" +values[0]); } 

// "response" is a TextView object declared in your function

 public class MainActivity extends AppCompatActivity { TextView response; //...so on 

and function

protected void onCreate(Bundle savedInstanceState) { response = (TextView) findViewById(R.id.textView);//..so on

+7
source share

Try this code in TcpClient:

  public void run() { mRun = true; try { InetAddress serverAddr = InetAddress.getByName(SERVER_IP); Log.e("TCP Client", "C: Connecting..."); Socket socket = new Socket(serverAddr, SERVER_PORT); try { mBufferOut = new PrintWriter(socket.getOutputStream()); Log.e("TCP Client", "C: Sent."); mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); int charsRead = 0; char[] buffer = new char[2024]; //choose your buffer size if you need other than 1024 while (mRun) { charsRead = mBufferIn.read(buffer); mServerMessage = new String(buffer).substring(0, charsRead); if (mServerMessage != null && mMessageListener != null) { Log.e("in if---------->>", " Received : '" + mServerMessage + "'"); } mServerMessage = null; } Log.e("-------------- >>", " Received : '" + mServerMessage + "'"); } catch (Exception e) { Log.e("TCP", "S: Error", e); } finally { //the socket must be closed. It is not possible to reconnect to this socket // after it is closed, which means a new socket instance has to be created. socket.close(); Log.e("-------------- >>", "Close socket " ); } } catch (Exception e) { Log.e("TCP", "C: Error", e); } } 

This is working correctly. This line in the other code above to call incorrectly.

mMessageListener.messageReceived (mServerMessage);

delete this line and your application will work well. You can control your entrance to Android Studio too.

You can use this code for your server in the Golang. This is my server:

 package main import ( "bufio" "flag" "fmt" "net" "strconv" ) var addr = flag.String("addr", "", "The address to listen to; default is \"\" (all interfaces).") var port = flag.Int("port", 37533, "The port to listen on; default is 37533.") func main() { flag.Parse() fmt.Println("Starting server...") src := *addr + ":" + strconv.Itoa(*port) listener, _ := net.Listen("tcp", src) fmt.Printf("Listening on %s.\n", src) defer listener.Close() for { conn, err := listener.Accept() if err != nil { fmt.Printf("Some connection error: %s\n", err) } go handleConnection(conn) } } func handleConnection(conn net.Conn) { remoteAddr := conn.RemoteAddr().String() LocalAddr :=conn.LocalAddr().String() fmt.Println("Client LocalAddr " + LocalAddr) fmt.Println("Client connected from " + remoteAddr) scanner := bufio.NewScanner(conn) for { ok := scanner.Scan() if !ok { break } handleMessage(scanner.Text(), conn) } fmt.Println("Client at " + remoteAddr + " disconnected.") } func handleMessage(message string, conn net.Conn) { fmt.Println("> " + message) if len(message) > 0 { conn.Write([]byte("This is from Golang.\n")) fmt.Println("----------> we send it....") } } 
0
source share

the only code that works for me is the code above, fixed by Moh_beh, but the original idea was to respond to the received message in MainActivity.

The Moh_beh code uses only the log:

 if (mServerMessage != null && mMessageListener != null) { Log.e("in if---------->>", " Received : '" + mServerMessage + "'"); } 

Can someone help me and give an example: how to pass a message to MainActivity and display in any TextView?

0
source share

All Articles