Sunday, January 10, 2016

Ask android socket using TCP mode, client-side how to listen to the data sent by the server?




            


Ask android socket using TCP mode, client-side how to listen to the data sent by the server?
My client code is sent as part of the thread:
Runnable SendThread = new Runnable () {
public void run () {
try {
// Create a Socket object that specifies the server
IP address and port numbersocket = new Socket ("192.168.0.101", iPort);
// Get OutputStream
them from SocketOutputStream outputStream = socket.getOutputStream ();
byte buffer [] = new byte [1024];
outputStream.write (buffer, 0, 1);
Log.v (MainServerActivity.sTAG, "outputstream flush ...");
outputStream.flush ();

} Catch (Exception e) {
// TODO Auto-generated catch block
Log.v (sTAG, "socket is new occur exception ...");
e.printStackTrace ();
}
}
};

The server receives a request receiving portion clent code is as follows:
ServerSocket serverSocket = null;
try {
// Create a ServerSocket object and let the Socket port monitoring
in 4567serverSocket = new ServerSocket (MainServerActivity.iPort);
while (! MainServerActivity.bStartShare) {
// Call the ServerSocket's accept () method, accepts requests sent by the client
Socket socket = serverSocket.accept ();
// Get InputStream object from Socket among
InputStream inputStream = socket.getInputStream ();
// Prove the reception to request the beginning of a screen capture
ScreenShot ();
// SaveBitmap (mScreenBitmap);
OutputStream out = socket.getOutputStream ();
if (mScreenBitmap.compress (Bitmap.CompressFormat.PNG, 70, out)) {
out.flush ();
}
}
} Catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
finally {
try {
Log.v (MainServerActivity.sTAG, "serverSocket close ...");
serverSocket.close ();
} Catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}

That my client how to listen to the server data sent to me? Please help.

Reply:
client do not listen, when the connection is established client data directly to the receiving server on the list. Reference http://bbs.csdn.net/topics/390316680
Reply:
In the client open a sub-thread, run () method similar to the following:
 public void run () {
while (isRun) {
try {
if ((msg = bufferedReader.readLine ())! = null) {
......
}
} Catch (Exception e) {
e.printStackTrace ();
}
}
}

IsRun setting in the main thread to stop the thread at the appropriate time.
Reply:
 bufferedReader = new BufferedReader (new InputStreamReader (
socket.getInputStream ()));

Reply:
Thank you very much, have to get.
Reply:
lz how to get a look at the code you give me thank 403818952@qq.com
Reply:
450370187@qq.com can send me so!
Reply:
704101213@qq.com can send me so!

        


Reply:
How to solve, I also get, what you can ask. . . ?
Reply:
attention. . . . . . . . .

No comments:

Post a Comment