This is swift code (client side):
let text: String = "neslihan"
var data = NSData(data: text.dataUsingEncoding(NSASCIIStringEncoding)!)
outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
This is the android code (server side)
public class SocketServerThread extends Thread {
DataInputStream dataInputStream = null;
ServerSocket serverSocket;
public int socketServerPORT = 3671;
Socket socket = null;
#Override
public void run() {
try {
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(socketServerPORT));
while (true) {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
Log.d("messageFromClient = " , dataInputStream.readUTF());
}
}
}
There is connection but i can't see log. How can i read string data?
Use flush to commit write data
outputStream.flush()
...
while(true){
socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("messageFromClient = " , br.readLine());
}
...
Related
Use case:
NodeMCU connects to Android over TCP socket, then Android plays a sound, when user presses a button, which is connected to NodeMCU.
I couldn't find a way (that I could believe as reliable) to have Android's ServerSocket to notice a reset from NodeMCU client socket, right at once, when NodeMCU's socket is disconnected, or trying to reconnect.
This is my newbee socket code bellow, which I got it working also with the help of this forum. I tried to make it as short as possible when posting here, keeping only relevant parts, but if you need I can just drop all the code, so please let me know in case.
Please suggest me a way. Any other comment to help me understand more of sockets is very welcome.
I'd like to finally learn what sockets are!
public class MainActivity extends AppCompatActivity {
ServerSocket serverSocket;
Thread socketServerThread = null;
protected void onCreate(Bundle savedInstanceState) {
//...
Globals.snd = MediaPlayer.create(this, R.raw.bike_horn);// Globals - static class
//...
socketServerThread = new Thread(new SocketServerThread());
socketServerThread.start();
}
protected void onDestroy() {
if (serverSocket != null) {...//try close it}
}
private class SocketServerThread extends Thread {
//...
static final int SocketServerPORT = 8080;
public void run() {
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
BufferedReader input = null;
try {
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(SocketServerPORT));
while (true) {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
while(socket.isConnected()){
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
message = input.readLine();
if(message == null) break;
int x = Integer.valueOf(message);
if(x == 1) Globals.snd.start();
}
}
}catch (IOException e) {// print error stack
}finally{//close socket, dataInputStream, dataOutputStream
}
NodeMCU code:
#include <ESP8266WiFi.h>
//...variables defined
void setup() {
//...connect to wifi
client.connect(host, port);
}
void loop() {
if(!client.connected()){
if (!client.connect(host, port)) {
return;
}
}
while(digitalRead(beepBtnPin) == LOW && client.connected()){
client.println("2");
client.flush();
delay(500);
}
}
You should put all code after
socket = serverSocket.accept();
In a so called client thread. This is normally done to handle more clients at once.
The server then immediately waits for the next client to connect.
I'm playing around ServerSocket on Android as the server part. I don't understand how it behaves. Here are what I tested :
A1. Instantiates a ServerSocket on Android
A2. ServerSocket sends "hello" to client
A3. Client can read the "hello" and can answer back to ServerSocket
A4. ServerSocket on Android receives the answer from the client
=> That works perfectly
Now I want the client to be the first to send a message to ServerSocket :
B1. Instantiates a ServerSocket on Android
B2. Client sends data to ServerSocket
B3. ServerSocket receives the data from client
B4. IMPOSSIBLE TO REPLY to the client
May that be a possible normal behaviour ?
Thanks
here is the source code
public void startServer()
{
log("startServer");
UUID uuid = UUID.randomUUID();
final String sessionId = uuid.toString().replace("-", "");
log("Session ID = " + sessionId);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
while (stopServer == false) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7777);
final Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
String strFromClient = "";
int i = 0;
while (i != -1) {
try {
i = inputStream.read();
if (i != -1)
strFromClient += (char) i;
}catch (Exception e){
break;
}
}
inputStream.close();
OutputStream outputStream = socket.getOutputStream();
String strToClient = "test";
byte[] cArray = strToClient.getBytes();
outputStream.write(cArray);
outputStream.flush();
outputStream.close();
socket.close();
serverSocket.close();
log("end server");
} catch (Exception e) {
//log(e.toString());
}
}
}
});
t.start();
}
Ok I found the solution ! The error was because the C# client was not sending the "-1" value (this is only triggered after closing a stream or stuff like that).
The solution is on the Android side, and the reading of the data from the client is now done as follow :
InputStream inputStream = socket.getInputStream();
String strFromClient = "";
int available = inputStream.available();
Log.d("intelsms", "available from client:" + available);
for (int i=0;i<available;i++){
int c = inputStream.read();
strFromClient+=(char)c;
}
I use the "available()" method in order to know how many bytes are available for reading from the client.
OUF !
I'm new in developing for Android. My app is client whith outside TCP server. I need to send text data from EditText to server in background thread with Socket. Answer from server i need to add to TextView. I see many examples, but don't see example which i could understand. So i need simple example specially for me, as that possible. Sorry for my English.
AsyncTask only for one-time using. I need only Thread. As i understand i need: Thread(Runnable), Handler, Message, Looper.. But i don't understand how use all this classes for me.
ublic class ServerWork extends Thread{
public static final Character PREFIX = ###;
public static final Character SUFFIX = ###;
public static final int SERVERPORT = ###;
public static final String SERVER_IP = ###;
private Socket socket;
String response;
String inputStr;
public ServerWork(String inputStr){
this.inputStr = inputStr;
}
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
String str = PREFIX + inputStr + SUFFIX;
PrintWriter printOut = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
BufferedReader inputBuffer = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
printOut.println(str);
printOut.flush();
char buf[] = new char[ 1000 ];
int count = inputBuffer.read( buf );
response = new String( buf, 0, count );
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I need to use the tcp socket connection to get the data from a bluebox, if I input a comment, such as "getcolor", the bluebox will send me the information like"red, blue".
In this case the bluebox as a server and I do not need to program on it, but I have problem to show the information on the EditText.
public class sender {
public static void main(String[] args)throws IOException{
Socket socket = new Socket("192.168.1.176",14111);
OutputStream out = socket.getOutputStream();
BufferedReader msg = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true);
String buffer = new String("");
String ms = "";
while(true)
{
while(in.ready())
buffer+= in.readLine()+ "\n";
String[] line = buffer.split("\n");
while(msg.ready())
ms = msg.readLine();
if(ms.equals("exit"))
{
break;
}
if(!ms.equals(""))
{
ou.println(ms);
ou.flush();
ms = "";
}
if(!buffer.equals(""))
{
System.out.print(buffer);
buffer = "";
}
}
in.close();
out.close();
socket.close();
}
}
this java code works, but it fails in the android code below:
public class BlueBoxApp extends Activity {
/** Called when the activity is first created. */
Context appInstance = this;
private EditText info;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
info = (EditText)findViewById(R.id.EditText01);
try{
InetAddress serverAddr = InetAddress.getByName("192.168.1.176");//TCP服务器IP地址
Log.d("TCP", "server,receiving...");
Socket socket = new Socket(serverAddr,14111);
try {
OutputStream out = socket.getOutputStream();
BufferedReader msg = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true);
String buffer = new String("");
String ms = "getsensorno";
Log.d("TCP", "sending:'"+ms+"'");
while(true)
{
while(in.ready())
buffer+= in.readLine()+ "\n";
while(msg.ready())
ms = msg.readLine();
if(ms.equals("exit"))
{
break;
}
if(!ms.equals(""))
{
ou.println(ms);
ou.flush();
ms = "";
}
if(!buffer.equals(""))
{
info.setText(buffer);
buffer = "";
}
}
} catch (Exception e) {
Log.e("TCP", "error",e);
}finally{
socket.close();
}
}catch(Exception e){
Log.e("TCP", "error",e);
}
}
}
what is the problem and how to set a thread for it? Thanks!
The problem, as you indicated, is that you are doing the networking part on the main thread.
Setting a new thread is easy, consider using AsyncTask. Please read the documentation (which is very good) before jumping to implement it, it will make it much easier IMHO.
Also, make sure you have internet permission in your AndroidManifest.xml
i am new to android and need simple http connection codes for client server(local server in the network) communication in android application.the connection starts when the application is started and if there is any update in the server it should be notified on the client and the server response must be based on the client request.
please help.
thanks
Socket socket;
InputStream is;
OutputStream os;
String hostname;
int port;
public void connect() throws IOException {
socket = new Socket(hostname, port);
is = socket.getInputStream();
os = socket.getOutputStream();
}
public void send(String data) throws IOException {
if(socket != null && socket.isConnected()) {
os.write(data.getBytes());
os.flush();
}
}
public String read() throws IOException {
String rtn = null;
int ret;
byte buf[] = new byte[512];
while((ret = is.read(buf)) != -1) {
rtn += new String(buf, 0, ret, "UTF-8");
}
return rtn;
}
public void disconnect() throws IOException {
try {
is.close();
os.close();
socket.close();
} finally {
is = null;
os = null;
socket = null;
}
}
connect, send, read, disconnect :)