Receive string in socket programming android - android

I am working on client server android application here my client is my application . I am sending string to server through wireless router . Sending of string is working fine but I am not getting how to deal with response string from server.
here is my code for sending and receiving strings
for sending
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(str);

Try this
InputStream is=socket.getInputStream();
int ch;
StringBuffer sb=new StringBuffer();
while((ch=(is.read))!=-1)
{
byte b=(byte)ch;
sb.append(b);
}
String response=sb.toString();

Related

How OkHttp send text data

I have a java code that send text file data to Label printer
through IP of LAN, It's Works fine
Can this code be executed on android using okhttp instead?
try {
FileReader fileReader = new FileReader("./src/print.txt");
BufferedReader bufferedReader=new BufferedReader(fileReader);
StringBuilder stringBuilder = new StringBuilder();
String string= "";
while((string=bufferedReader.readLine())!=null){
stringBuilder.append(string).append("\n");
}
bufferedReader.close();
Socket socket=new Socket("10.1.1.1",9100);
OutputStream outputStream=socket.getOutputStream();
outputStream.write(stringBuilder.toString().getBytes());
socket.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
No, OkHttp is a HTTP library. It uses Okio + java's Socket/SSLSocket for most of the low level IO.
The request above is just a simple TCP socket. You could use Okio operations on top of the socket if you want a nicer API.
Why not just run the code above as is on Android?

Socket input stream and Unicode

I try to write an app that sends text from Windows computer to Android cellphone.
The text I send can be in English or Hebrew (for example). The connection is via Socket. The code I use on the Windows side (Visual studio 2012):
String buffer = // Some text
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes(buffer + "\n");
// Send the data through the socket.
int bytesSent = socketSender.Send(msg);
And on the Android side:
//After I establish the Socket
String text = "";
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
while ((inputText = in.readLine()) != null)
{
text = inputText;
}
All this works perfectly when sending English text.
When I try to send Hebrew text I replace to this line:
byte[] msg = Encoding.Unicode.GetBytes(buffer + "\n");
But on the Android side I can't "read" it.
I tried to use CharsetEncoder but didn't work (or I did it the wrong way).
Any ideas?
Ok, so the answer is:
on the Windows side:
byte[] msg = Encoding.UTF8.GetBytes(buffer + "\n");
And on the Android side:
InputStreamReader isr = new InputStreamReader(is, "UTF-8");

UTF-8 chars reading in Android socket connection

I've prepared socket client connection like this:
Socket socket = new Socket();
socket.setSoTimeout(500);
hostname = "XX.YY.ZZ.XX";
socket.connect(new InetSocketAddress(hostname, port), 6000);
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
false);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
and I read data using i.e. this code:
if (in.ready() == true) {
String message = in.readLine();
Log.e("MyApplication", "data=" + message);
}
When I look in logcat I see ? char instead of every national characters.
I'm sure data sending to my android application is in utf-8 charset therefore I tried to use code like this:
UTF8Str = new String(message.getBytes(),"UTF-8");
or creating BufferReader like this:
in = new BufferedReader(new InputStreamReader(
socket.getInputStream(),"UTF-8"));
but everything without success.
Can somebody help me resolve problem conversion chars to display them in logcat as nationals (my main goal is to display them in TextView). Everything worked two weeks ago but after some Ecclipse reconfiguration (I can't back to old settings) stoped.
~Artik

How can I receive data packets in GPRS in my android mobile?

I need to receive data packets send from a GSM modem through GPRS with my android mobile...
Can any one help me how to send and receive..
What protocol has to be used?
Create a Thread in server starts and do the following
ServerSocket serverSocket = new ServerSocket(8000); //8000 - port number
Socket client = serverSocket.accept(); // thread will wait here till a client connects
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(client.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
client.getOutputStream());
while ((inputLine = inFromClient.readLine()) != null) {
System.out.println("Server: " + inputLine);
if(inputLine.equals("END"))
{
outToClient.writeBytes("END\n");
}
else
{
outToClient.writeBytes("Received\n");
}
}
inFromClient.close();
outToClient.close();
Its a TCP connection you need to handle

Socket connection getting closed while reading data from socket

I am writting simple program to connect server by socket in android.
But when i try to read data from socket's outputstream it will send automatically RST request. so my connection gets closed. but i want my connection to open always.
Please any one help me.
Thank you.
try {
Socket socket = new Socket("xxx.xxx.x.xx", 9083);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
out.println("Testing");
InputStream inputStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
String readObject = reader.readLine();
System.out.println(readObject);
} catch (Exception e) {
e.printStackTrace();
}
The most usual reason for 'connection reset' is that you have written to a connection that has already been closed by the other end. In other words, an application protocol error.

Categories

Resources