Monday, January 11, 2016

android holding socket connection




            


I use a button's click event to create a socket in the interface, as follows:
 
sock = new Socket (tv_ip.getText () toString (), port.);
in = new BufferedReader (new InputStreamReader (sock.getInputStream ()));
out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ())), true);


In addition, I use another button to send the data, as follows:
 
if (sock.isConnected ()) {
if (! sock.isOutputShutdown ()) {
out.println (str_send);
tv_result.setText (str_send);
}
}

But only made once, you must click a button to create a socket, you can send it again. Logically, I did not close the socket connection. Because of the android socket handling mechanism is not very familiar with, but also please advise seniors advise, how to maintain socket created until the program ends
Reply:
The reply to be removed administrator 2013-04-27 16:23:20

Reply:
no one? Their top a
Reply:
socket I did not how used, I do not know ah
Reply:
Is not it, I feel for you seniors, it should be a very simple question ah!
Reply:
while loop wrapped
Reply:
quote 5th floor knxw0001 reply:
the while loop package


Does not it? How you control
Reply:
But only made once, you must click a button to create a socket, you can send it again. Logically, I did not close the socket connection. Because of the android socket handling mechanism is not very familiar with, but also please advise seniors advise, how to maintain socket created until the program ends


socket get the input and output streams, and the corresponding flow can not be shut down, closed the lead to socket failure, need to re-initialize, especially when ssl connection, the effect will be particularly poor. write out call outsteam.flush () after the stream on the line, do not close
Reply:
quote 7th floor QQ718225250 reply:
but only made once, you must click a button to create a socket, you can send it again. Logically, I did not close the socket connection. Because of the android socket handling mechanism is not very familiar with, but also please advise seniors advise, how to maintain socket created until the program ends


socket get the input and output streams, and the corresponding flow can not be shut down, closed the lead to socket failure, need to re-initialize, especially when ssl connection, the effect will be particularly poor. After calling outsteam.flush write out stream () on the line, do not close

Calling flush not ah. The whole process, I do not have the corresponding stream Close
Reply:
It is a service to shut down the bar
Reply:
quote 9th floor ultrapro reply:
is the service to shut down the bar

No ah. Here is the code
services
 while (1) 
{
SOCKET sockcln = accept (sockSrv, (sockaddr *) & amp; addrClient, & amp; len);
unsigned long IP = addrClient.sin_addr.S_un.S_addr;
printf ("get connect request from IP:% d% d% d% d \ n...", (IP & lt; & lt; 24) & gt; & gt; 24, (IP & lt; & lt; 16) & gt; & gt; 24, (IP & lt; & lt; 8) & gt; & gt; 24, IP & gt; & gt; 24);
char recvBuf [100];
memset (recvBuf, 0, sizeof (recvBuf));
recv (sockcln, recvBuf, sizeof (recvBuf), 0);
parseMsg (recvBuf);
}

Reply:
Great God seeking guidance ah!
Reply:
Should use the while loop package, set up a judge, after the data has been received, emptied it
Reply:
Certainly where there is the anomaly
Reply:
The reply to be removed administrator 2013-05-11 11:39:45

Reply:
quote 12th floor u010523911 reply: after
should use the while loop package, set up a judge, the data has been received, emptied it
< / fieldset>

Not quite understand what you mean, oh
Reply:
quote 13th floor ydx1991 reply:
is certainly unusual where a


No, oh, pro! I debugging, data send finished, socket and socket isOutputShutdown logo are open
Reply:
Landlord to solve the problem it? I have encountered this problem I would like to ask

No comments:

Post a Comment