开挂怎么才能最快接受传奇服务端免费下载发送的数据

服务端是一个传感器,建立连接后它不停的发送数据给我的应用程序
我在一个子线程中不断的判定是否接收到数据,接收到后发送一个消息
到主线程,在主线程中进行数据处理
现在的问题是,刚开始工作的很好,但是进过一两个小时后发现读到的数据
发生了延迟
请问这种情况是由于什么引起的呢?
回复讨论(解决方案)
首先,得排除是不是网络问题。
其次,接收到的数据,处理完成后,有没有及时得到释放。
1,2小时会有延时,从这个现象来看,有可能是某些资源没有得及时的释放导致的。
跑3,4个小时,甚至7,8个小时后,情况会如何?
首先,得排除是不是网络问题。
其次,接收到的数据,处理完成后,有没有及时得到释放。
1,2小时会有延时,从这个现象来看,有可能是某些资源没有得及时的释放导致的。
跑3,4个小时,甚至7,8个小时后,情况会如何?
首先感谢你的回答,等了好多天啊。
不是网络的问题,因为以前的一个版本软件运行的很好,一天一夜也不会延迟。
以前的版本是vb写的用的winsck控件,运行的不错,现在换成delphi用了
Indy控件,我每次是用readln读一行,是不是因为没有把缓冲区读完呢?
现在我把winsck控件弄到delphi里了,写好后运行一下看看是否延迟。
另外想问一下,delphi的子线程中用到indy控件时,是否也要使用sychonize把它
包含呢?Indy不是号称线程安全吗?
Indy我用的比较少。我一般用的是TTcpClient&和&TTCpServer,&&这2个Delphi自带的控件。接收数据&一般是&命令头&+&数据,这样的结构。先接收命令头,判断是否为有效协议,然后才是接收数据,接收数据要注意,最后接收的数据不一定正好是缓冲区的大小。
//Msg.dwLen&=&1028;&&需要接收数据长度
&&BuffLen&=&&512;
&&Buffer:&array&[0..511]&of&
&&iLeft:&I&&//剩余长度
&&iTotal:&&//接收数据总长度
&&iRcv:&&&&//接收数据长度
&&iLeft&:=&0;
&&iTotal&:=&0;
&&while&iTotal&&&Msg.dwLen&do
&&&&iLeft&:=&Msg.dwLen&-&iT
&&&//如果剩余数据长度小于缓冲区长度,则按剩余数据长度接收。否则按缓冲区长度接收
&&&&if&iLeft&&&BufferLen&then&&&
&&&&&&ilen&:=&Rcv(Buffer,&iLeft)
&&&&&&ilen&:=&Rcv(Buffer,&BufferLen);
&&&&if&ilen&=&-1&then&
&&&&begin&
&&&&&&&closesocket(s);
&&&&//如果有错误发生,则退出接收。并关闭socket
&&&&itotal&:=&itotal&+&
VCL&控件&在线程&最好使用Sychonize()&来调用它。
TTcpClient可以工作于非阻塞模式吗?我把它设置为非阻塞后,连接就不成功了?有什么好建议吗?
TTcpClient&可以用于非阻塞模式,不过,得要用异步选择去操作。示例代码如下所示&:
//socket模型使用&异步选择
//定义消息
&&WM_SOCKET&=&WM_USER&+&2034;
//消息处理
&&procedure&OnSocket(var&Msg:&TMessage);&message&WM_SOCKET;
//sokcet&事件处理
&&function&OnFDRead(const&s:&TSocket):&B
&&function&OnFDClose(const&s:&TSocket):&B
&&function&InitSocket(const&s:&TSocket):&B
&&function&OnRcv(const&s:&TSocket):&B
implementation
function&InitSocket(const&s:&TSocket):&B
&&//监测socket的事件,详见MSDN
&&Result&:=&(0&=&WSAAsyncSelect(s,&Handle,&WM_SOCKET,&FD_READ&or&FD_CLOSE))&;
function&OnFDClose(const&s:&TSocket):&B
&Result&:=&closesocket(s)&=&0;
function&OnFDRead(const&s:&TSocket):&B
&&protocol:&TP&&//协议结构体。
&&&//读取数据
&&&Zeromemory(@protocol,&sizeof(TProtocol));
&&&iRcv&:=&recv(s,&protocol,&sizeof(TProtocol),&0);
&&&if&(-1&=&iRcv)&and&(WSAGetLastError&=&WSAECONNRESET)&then
&&&&&&onFDClose(s);
&&&&&//如果发生错误,则关闭socket&
&&&case&protocol.dwCommand&of
&&&&//命令判别
&&&OnRcv(s);
function&OnRcv(const&s:&TSocket):&
&&BufLen&=&512;
&&iTotal:&I
&&Buffer:&array&[0&..&BufLen]&of&B
&&iLeft:&I
&&//FStream:&TMemoryS&是存放接收到数据。
&&ilen&:=&0;
&&iTotal&:=&0;
&&while&iTotal&&&FMsgLen&do
&&&&iLeft&:=&FMsgLen&-&iT
&&&&if&iLeft&&&BufLen&then
&&&&&&iLeft&:=&BufL
&&&&ilen&:=&recv(FSockt,&Buffer,&BufLen,&0);&&&//WSAEWOULDBLOCK
&&&&if&(SOCKET_ERROR&=&iLeft)&and&(WSAGetLastError&=&WSAECONNRESET)&then
&&&&&&closesocket(FSockt);
&&&&&&//异步时,返回-1,并不一定是socket发生错误。
&&&&FStream.WriteBuffer(Buffer,&ilen);
&&&&iTotal&:=&iTotal&+&
&&Result&:=&iTotal&=&FMsgL
procedure&OnSocket(var&Msg:&TMessage);&
&&sockt:&TS
&&sockt&:=&Msg.WP
&&if&WSAGetAsyncError(Msg.LParam)&&&&0&then
&&&&OnFDClose(sockt);
&&&&&//socket是否有错误发生。
&&case&WSAGetSelectEvent(Msg.LParam)&of
&&&&FD_READ:
&&&&&&OnFDRead(sockt);
&&&&FD_CLOSE:
&&&&&&OnFDClose(sockt);
&&&&//判断此socket的事件。
以上代码需要写在TForm对象中。
多谢你了,学习到了很多 !2015年5月 移动开发大版内专家分月排行榜第一2015年4月 移动开发大版内专家分月排行榜第一2014年9月 移动开发大版内专家分月排行榜第一
2015年3月 移动开发大版内专家分月排行榜第二2014年8月 移动开发大版内专家分月排行榜第二
2015年4月 移动开发大版内专家分月排行榜第二
2015年5月 移动开发大版内专家分月排行榜第三2015年3月 移动开发大版内专家分月排行榜第三2014年10月 移动开发大版内专家分月排行榜第三
2015年4月 移动开发大版内专家分月排行榜第二
2015年5月 移动开发大版内专家分月排行榜第三2015年3月 移动开发大版内专家分月排行榜第三2014年10月 移动开发大版内专家分月排行榜第三
2015年4月 移动开发大版内专家分月排行榜第二
2015年5月 移动开发大版内专家分月排行榜第三2015年3月 移动开发大版内专家分月排行榜第三2014年10月 移动开发大版内专家分月排行榜第三
2008年12月 移动平台大版内专家分月排行榜第一
2009年3月 移动平台大版内专家分月排行榜第三2009年1月 移动平台大版内专家分月排行榜第三2008年11月 移动平台大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。3399人阅读
Android(156)
注意:如果打开输出流了 客户端的请求方法是GET,就会请求到服务器的POST方法去
package com.example.k.
import android.app.A
import android.os.B
import android.os.H
import android.os.M
import android.util.L
import android.view.V
import android.widget.B
import android.widget.TextV
import java.io.BufferedR
import java.io.DataOutputS
import java.io.IOE
import java.io.InputS
import java.io.InputStreamR
import java.io.UnsupportedEncodingE
import java.net.HttpURLC
import java.net.MalformedURLE
import java.net.URL;
import java.net.URLD
import java.net.URLE
public class MainActivity extends Activity {
private final int GETVALUE = 0,POSTVALUE = 1;
Button sendGET, sendPOST;
TextView t, sendPOSTtextV
String str = &梁嘉亮&;
String str1;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case GETVALUE:
msg.obj = URLDecoder.decode(msg.obj + &&, &UTF-8&);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
t.setText(&& + msg.obj);
case POSTVALUE:
sendPOSTtextView.setText(&& + msg.obj);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
t = (TextView) findViewById(R.id.textView);
sendPOSTtextView = (TextView) findViewById(R.id.textView2);
sendPOST = (Button) findViewById(R.id.button1);
sendGET = (Button) findViewById(R.id.button);
sendGET.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new Thread(new Runnable() {
public void run() {
//用GET方法向服务器传送数据,在链接里面传值
str1 = URLEncoder.encode(str, &UTF-8&);
URL url = new URL(&http://120.27.103.151:8088/JWebDemoByIDE/MyServlet?name=& + str1 + &&age=23&);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod(&GET&);
connection.setRequestProperty(&charset&, &UTF-8&);
connection.setConnectTimeout(5000);
connection.setDoInput(true);
if (connection.getResponseCode() == 200) {
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder s = new StringBuilder();
while ((line = reader.readLine()) != null) {
s.append(line);
reader.close();
Message message = new Message();
message.what = GETVALUE;
message.obj = s.toString();
handler.sendMessage(message);
} catch (Exception e) {
Log.i(&ok&, &有错误!&);
}).start();
sendPOST.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new Thread(new Runnable() {
public void run() {
URL url = new URL(&http://120.27.103.151:8088/JWebDemoByIDE/MyServlet&);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(&POST&);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty(&charset&, &UTF-8&);
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//向服务器传送数据
out.writeBytes(&Im Liang Jia Liang!&);
if (connection.getResponseCode() == 200) {
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder s = new StringBuilder();
//读取服务器返回的数据
while ((line = reader.readLine()) != null) {
s.append(line);
Message message = new Message();
message.what = POSTVALUE;
message.obj = s.toString();
handler.sendMessage(message);
reader.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}).start();
记得要声明一下网络权限:
&?xml version=&1.0& encoding=&UTF-8&?&
&manifest package=&com.example.k.servletdemo& xmlns:android=&/apk/res/android&&
&uses-permission android:name=&android.permission.INTERNET&/&
&application android:theme=&@style/AppTheme& android:supportsRtl=&true& android:label=&@string/app_name& android:icon=&@mipmap/ic_launcher& android:allowBackup=&true&&
&activity android:name=&.MainActivity&&
&intent-filter&
&action android:name=&android.intent.action.MAIN&/&
&category android:name=&android.intent.category.LAUNCHER&/&
&/intent-filter&
&/activity&
&/application&
&/manifest&布局文件:
&?xml version=&1.0& encoding=&UTF-8&?&
&LinearLayout android:layout_height=&match_parent& android:layout_width=&match_parent& android:orientation=&vertical& xmlns:android=&/apk/res/android&&
&Button android:layout_height=&wrap_content& android:layout_width=&match_parent& android:text=&sendGET& android:id=&@+id/button&/&
&TextView android:layout_height=&wrap_content& android:layout_width=&wrap_content& android:id=&@+id/textView&/&
&Button android:layout_height=&wrap_content& android:layout_width=&match_parent& android:text=&sendPOST& android:id=&@+id/button1&/&
&TextView android:layout_height=&wrap_content& android:layout_width=&wrap_content& android:id=&@+id/textView2&/&
&/LinearLayout&
以下是服务器端:(服务器用的是tomcat,虚拟主机是阿里云的ECS)
package com.iotek.jee.
import java.io.BufferedR
import java.io.IOE
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = -6866661L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取http://120.27.103.151:8088/JWebDemoByIDE/MyServlet?name=liangjialiang 问号后面的值
String name = req.getParameter(&name&);
String age = req.getParameter(&age&);
resp.getWriter().write(name);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BufferedReader b =
req.getReader();
StringBuilder s = new StringBuilder();
while((line = b.readLine())!=null){
s.append(line);
resp.getWriter().write(s.toString());
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
服务器web.xml文件
&?xml version=&1.0& encoding=&UTF-8&?&
&web-app xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance& xmlns=&/xml/ns/javaee& xsi:schemaLocation=&/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd& id=&WebApp_ID& version=&3.0&&
&display-name&JWebDemoByIDE&/display-name&
&servlet-name&MyServlet&/servlet-name&
&servlet-class&com.iotek.jee.servlet.MyServlet&/servlet-class&
&/servlet&
&servlet-mapping&
&servlet-name&MyServlet&/servlet-name&
&url-pattern&/MyServlet&/url-pattern&
&/servlet-mapping&
&welcome-file-list&
&welcome-file&index.html&/welcome-file&
&welcome-file&index.htm&/welcome-file&
&welcome-file&index.jsp&/welcome-file&
&welcome-file&default.html&/welcome-file&
&welcome-file&default.htm&/welcome-file&
&welcome-file&default.jsp&/welcome-file&
&/welcome-file-list&
&/web-app&服务器就两个文件如下图:
运行效果图如下:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:41040次
积分:2308
积分:2308
排名:第14630名
原创:195篇
评论:15条
(5)(3)(11)(14)(6)(12)(6)(8)(7)(26)(38)(30)(33)(1)}

我要回帖

更多关于 360云盘服务端升级 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信