//Server methods
public static void searchMethodCatcher()
{
try
{
clientSocket = serverSocket.accept();
isr = new InputStreamReader(clientSocket.getInputStream(),charset);
br = new BufferedReader(isr);
String str = br.readLine();
if(str.charAt(0) == '{')
{
extendetSearchSystem(str);
}
else {simpleSearchListener(str);}
}catch(IOException e){e.printStackTrace();}
}
public static void simpleSearchListener(String str)
{
String result = str;
System.out.println(line);
try {
clientSocket.shutdownInput();
pw = new PrintWriter(clientSocket.getOutputStream(),true);
pw.write("DONE");
pw.flush();
pw.close();
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void serverListener(int port)
{
new Thread() {
@Override
public void run() {
super.run();
try
{
serverSocket = new ServerSocket(PORT_WORK);
}catch(IOException e){
e.printStackTrace();
System.out.println("Could not listen on port"+ PORT_WORK);
}
while(isRun)
{
searchMethodCatcher();
// simpleSearchListener();
}
}
}.start();
}
//Client asyncTast clas
class ServerWorker extends AsyncTask<Void,Void,Wrapper> {
public int flag = 0, i = 0;
public String[] tarr;
public Wrapper w = new Wrapper();
public String jsonResult,temp;
public JSONObject jsonObject;
@Override
protected Wrapper doInBackground(Void... params) {
try {
try {
clientSocket = new Socket(ip, port);
if (line.isEmpty()) {
flag = 2;
} else {
printWriter = new PrintWriter(clientSocket.getOutputStream());
printWriter.write(line);
printWriter.flush();
clientSocket.shutdownOutput();
// clientSocket.shutdownOutput();
isr = new InputStreamReader(clientSocket.getInputStream(),"UTF-8");
br = new BufferedReader(isr);
Log.e("READY?",br.ready()+" INPUT?" + clientSocket.isInputShutdown());
String message = br.readLine();
if(br.readLine()!=null)
Log.e("LINE IS",br.readLine()+"");
else{ Log.e("ERROR","br.readLine is null")
}
clientSocket.close();
}
} catch (ConnectException e) {
e.printStackTrace();
flag = 1;
}
}catch (IOException e) {e.printStackTrace();}
return w;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Wrapper wrapper) {
super.onPostExecute(wrapper);
}
}
}
So.When I run the server on the pc and client on the phone , and then sends the message I get the following. Server get message normal: Server logs
But response from server to phone is always null client logs
I think i do something wrong with Input/Output streams. Pls help me to solve this issue./
Client Logs 11-15 22:29:31.750 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Can't open keycharmap file 11-15 22:29:31.750 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Error loading keycharmap file 11-15 22:29:31.750 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Using default keymap 11-15 22:29:33.257 16011-16011/com.kiskin.vlad.kvsearchsystem I/ApplicationPackageManager﹕ cscCountry is not German : SEK 11-15 22:29:33.359 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ No keyboard for id -1 11-15 22:29:33.359 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Using default keymap 11-15 22:29:35.289 16011-16011/com.kiskin.vlad.kvsearchsystem I/ApplicationPackageManager﹕ cscCountry is not German : SEK 11-15 22:29:40.578 16011-25796/com.kiskin.vlad.kvsearchsystem E/READY?﹕ false INPUT?false 11-15 22:29:41.976 16011-25796/com.kiskin.vlad.kvsearchsystem E/ERROR﹕ br.readLine is null
Server logs /192.168.0.8 iphone Apple iPhone 5S 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-space-gray/ Диапазон цен: 7 895 - 14 500 грн Apple iPhone 6 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-space-gray/ Диапазон цен: 13 199 - 30 200 грн Apple iPhone 6s 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-space-gray/ Диапазон цен: 20 043 - 27 816 грн Apple iPhone 5 16GB (Black): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-black/ Диапазон цен: 6 099 - 8 299 грн Apple iPhone 6 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-space-gray/ Диапазон цен: 15 782 - 25 457 грн Apple iPhone 6 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-gold/ Диапазон цен: 14 271 - 20 999 грн Apple iPhone 5S 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-gold/ Диапазон цен: 8 555 - 13 640 грн Apple iPhone 6s 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-space-gray/ Диапазон цен: 17 220 - 23 520 грн Apple iPhone 5 16GB (White): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-white/ Диапазон цен: 6 099 - 10 417 грн Apple iPhone 6s 64GB (Rose Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-rose-gold/ Диапазон цен: 20 160 - 29 551 грн Apple iPhone 5S 32GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-32gb-space-gray/ Диапазон цен: 9 018 - 15 750 грн Apple iPhone 6s 16GB (Rose Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-rose-gold/ Диапазон цен: 17 349 - 23 520 грн Apple iPhone 6s 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-gold/ Диапазон цен: 17 145 - 23 520 грн Apple iPhone 6s Plus 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-plus-64gb-space-gray/ Диапазон цен: 24 630 - 33 450 грн Apple iPhone 5C 16GB (White): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5c-16gb-white/ Диапазон цен: 6 048 - 11 250 грн Apple iPhone 6s 128GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-128gb-space-gray/ Диапазон цен: 22 200 - 31 152 грн Apple iPhone 6 16GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-silver/ Диапазон цен: 14 250 - 22 015 грн Apple iPhone 6s 64GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-silver/ Диапазон цен: 20 050 - 27 816 грн Apple iPhone 5S 16GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-white/ Диапазон цен: 8 399 - 14 750 грн Apple iPhone 6 64GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-silver/ Диапазон цен: 15 531 - 21 649 грн Results count is 20 Results product is [Apple iPhone 5S 16GB (Space Gray) , Apple iPhone 6 16GB (Space Gray) , Apple iPhone 6s 64GB (Space Gray) , Apple iPhone 5 16GB (Black) , Apple iPhone 6 64GB (Space Gray) , Apple iPhone 6 16GB (Gold) , Apple iPhone 5S 16GB (Gold) , Apple iPhone 6s 16GB (Space Gray) , Apple iPhone 5 16GB (White) , Apple iPhone 6s 64GB (Rose Gold) , Apple iPhone 5S 32GB (Space Gray) , Apple iPhone 6s 16GB (Rose Gold) , Apple iPhone 6s 16GB (Gold) , Apple iPhone 6s Plus 64GB (Space Gray) , Apple iPhone 5C 16GB (White) , Apple iPhone 6s 128GB (Space Gray) , Apple iPhone 6 16GB (Silver) , Apple iPhone 6s 64GB (Silver) , Apple iPhone 5S 16GB (Silver) , Apple iPhone 6 64GB (Silver) ] Results price is [7 895 - 14 500 грн , 13 199 - 30 200 грн , 20 043 - 27 816 грн , 6 099 - 8 299 грн , 15 782 - 25 457 грн , 14 271 - 20 999 грн , 8 555 - 13 640 грн , 17 220 - 23 520 грн , 6 099 - 10 417 грн , 20 160 - 29 551 грн , 9 018 - 15 750 грн , 17 349 - 23 520 грн , 17 145 - 23 520 грн , 24 630 - 33 450 грн , 6 048 - 11 250 грн , 22 200 - 31 152 грн , 14 250 - 22 015 грн , 20 050 - 27 816 грн , 8 399 - 14 750 грн , 15 531 - 21 649 грн ] Results links is [/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-black/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-rose-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-32gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-rose-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-plus-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5c-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-128gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-silver/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-silver/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-silver/ ]
/192.168.0.8 iphone Apple iPhone 5S 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-space-gray/ Диапазон цен: 7 895 - 14 500 грн Apple iPhone 6 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-space-gray/ Диапазон цен: 13 199 - 30 200 грн Apple iPhone 6s 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-space-gray/ Диапазон цен: 20 043 - 27 816 грн Apple iPhone 5 16GB (Black): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-black/ Диапазон цен: 6 099 - 8 299 грн Apple iPhone 6 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-space-gray/ Диапазон цен: 15 782 - 25 457 грн Apple iPhone 6 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-gold/ Диапазон цен: 14 271 - 20 999 грн Apple iPhone 5S 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-gold/ Диапазон цен: 8 555 - 13 640 грн Apple iPhone 6s 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-space-gray/ Диапазон цен: 17 220 - 23 520 грн Apple iPhone 5 16GB (White): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-white/ Диапазон цен: 6 099 - 10 417 грн Apple iPhone 6s 64GB (Rose Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-rose-gold/ Диапазон цен: 20 160 - 29 551 грн Apple iPhone 5S 32GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-32gb-space-gray/ Диапазон цен: 9 018 - 15 750 грн Apple iPhone 6s 16GB (Rose Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-rose-gold/ Диапазон цен: 17 349 - 23 520 грн Apple iPhone 6s 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-gold/ Диапазон цен: 17 145 - 23 520 грн Apple iPhone 6s Plus 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-plus-64gb-space-gray/ Диапазон цен: 24 630 - 33 450 грн Apple iPhone 5C 16GB (White): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5c-16gb-white/ Диапазон цен: 6 048 - 11 250 грн Apple iPhone 6s 128GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-128gb-space-gray/ Диапазон цен: 22 200 - 31 152 грн Apple iPhone 6 16GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-silver/ Диапазон цен: 14 250 - 22 015 грн Apple iPhone 6s 64GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-silver/ Диапазон цен: 20 050 - 27 816 грн Apple iPhone 5S 16GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-white/ Диапазон цен: 8 399 - 14 750 грн Apple iPhone 6 64GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-silver/ Диапазон цен: 15 531 - 21 649 грн Results count is 20 Results product is [Apple iPhone 5S 16GB (Space Gray) , Apple iPhone 6 16GB (Space Gray) , Apple iPhone 6s 64GB (Space Gray) , Apple iPhone 5 16GB (Black) , Apple iPhone 6 64GB (Space Gray) , Apple iPhone 6 16GB (Gold) , Apple iPhone 5S 16GB (Gold) , Apple iPhone 6s 16GB (Space Gray) , Apple iPhone 5 16GB (White) , Apple iPhone 6s 64GB (Rose Gold) , Apple iPhone 5S 32GB (Space Gray) , Apple iPhone 6s 16GB (Rose Gold) , Apple iPhone 6s 16GB (Gold) , Apple iPhone 6s Plus 64GB (Space Gray) , Apple iPhone 5C 16GB (White) , Apple iPhone 6s 128GB (Space Gray) , Apple iPhone 6 16GB (Silver) , Apple iPhone 6s 64GB (Silver) , Apple iPhone 5S 16GB (Silver) , Apple iPhone 6 64GB (Silver) ] Results price is [7 895 - 14 500 грн , 13 199 - 30 200 грн , 20 043 - 27 816 грн , 6 099 - 8 299 грн , 15 782 - 25 457 грн , 14 271 - 20 999 грн , 8 555 - 13 640 грн , 17 220 - 23 520 грн , 6 099 - 10 417 грн , 20 160 - 29 551 грн , 9 018 - 15 750 грн , 17 349 - 23 520 грн , 17 145 - 23 520 грн , 24 630 - 33 450 грн , 6 048 - 11 250 грн , 22 200 - 31 152 грн , 14 250 - 22 015 грн , 20 050 - 27 816 грн , 8 399 - 14 750 грн , 15 531 - 21 649 грн ] Results links is [/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-black/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-rose-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-32gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-rose-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-plus-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5c-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-128gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-silver/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-silver/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-silver/ ]
String message = br.readLine();
if(br.readLine()!=null)
Log.e("LINE IS",br.readLine()+"");
else{
Log.e("ERROR","br.readLine is null");
}
You're reading lines and throwing them away. What you should be testing and logging above is the content of message
. Not reading message
, reading another line and testing that, and logging the result of yet a third readLine()
. As the server doesn't even send one line, let alone three, you're never going to get anything but null for the second two readLine()
calls.
NB Don't do I/O in the accept()
thread. Every accepted socket should be handled in its own thread.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.