服务器和客户端之间建立了连接,但无法通过J2ME中的OutputStream发送数据 我最近开始在J2ME中编程客户机/服务器应用程序,现在我正在使用C++ Builder 2010的Dyy组件(例如TeTTCPCserver)和J2ME。我的应用程序旨在从远程计算机重新启动kerio winroute防火墙服务。 我的服务器应用程序是用C++ Builder 2010编写的,我把TIDTCTserver组件放在一个绑定到127.0.0.1:4500的表单中。这是在本地机器的4500端口监听的。 然后我添加了一个列表框,我需要添加每个即将转换为UnicodeString的数据包 //void __fastcall TForm1::servExecute(TIdContext *AContext) UnicodeString s; UnicodeString txt; txt=Trim(AContext->Connection->IOHandler->ReadLn()); otvet->Items->Add(txt); otvet->ItemIndex=otvet->Items->Count-1; if (txt=="1") { AContext->Connection->IOHandler->WriteLn("Suhrob"); AContext->Connection->Disconnect(); } if (txt=="2") { AContext->Connection->IOHandler->WriteLn("Shodi"); AContext->Connection->Disconnect(); }
//----------------------------------------------------------------------------服务器和客户端之间建立了连接,但无法通过J2ME中的OutputStream发送数据 我最近开始在J2ME中编程客户机/服务器应用程序,现在我正在使用C++ Builder 2010的Dyy组件(例如TeTTCPCserver)和J2ME。我的应用程序旨在从远程计算机重新启动kerio winroute防火墙服务。 我的服务器应用程序是用C++ Builder 2010编写的,我把TIDTCTserver组件放在一个绑定到127.0.0.1:4500的表单中。这是在本地机器的4500端口监听的。 然后我添加了一个列表框,我需要添加每个即将转换为UnicodeString的数据包 //void __fastcall TForm1::servExecute(TIdContext *AContext) UnicodeString s; UnicodeString txt; txt=Trim(AContext->Connection->IOHandler->ReadLn()); otvet->Items->Add(txt); otvet->ItemIndex=otvet->Items->Count-1; if (txt=="1") { AContext->Connection->IOHandler->WriteLn("Suhrob"); AContext->Connection->Disconnect(); } if (txt=="2") { AContext->Connection->IOHandler->WriteLn("Shodi"); AContext->Connection->Disconnect(); },c++,sockets,java-me,indy,C++,Sockets,Java Me,Indy,//---------------------------------------------------------------------------- // void __fastcall TForm1::servConnect(TIdContext *AContext) ++counter; status->Panels->Items[0]->Text="Connections:" + IntToStr(counter); status->Panels-
// void __fastcall TForm1::servConnect(TIdContext *AContext)
++counter;
status->Panels->Items[0]->Text="Connections:" + IntToStr(counter);
status->Panels->Items[1]->Text="Connected to " + AContext->Connection->Socket->Binding->PeerIP + ":" + AContext->Connection->Socket->Binding->PeerPort;
我的客户端代码看起来像这样:
else if (command == send) {
// write pre-action user code here
InputStream is=null;
OutputStream os=null;
SocketConnection client=null;
ServerSocketConnection server=null;
try {
server = (ServerSocketConnection) Connector.open("socket://"+IP.getString()+":"+PORT.getString());
// wait for a connection
client = (SocketConnection) Connector.open("socket://"+IP.getString()+":"+PORT.getString());
// set application-specific options on the socket. Call setSocketOption to set other options
client.setSocketOption(SocketConnection.DELAY, 0);
client.setSocketOption(SocketConnection.KEEPALIVE, 0);
is = client.openInputStream();
os = client.openOutputStream();
// send something to server
os.write("texttosend".getBytes());
// read server response
int c = 0;
while((c = is.read()) != -1) {
// do something with the response
System.out.println((char)c);
}
// close streams and connection
}
catch( ConnectionNotFoundException error )
{
Alert alert = new Alert(
"Error", "Not responding!", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
switchDisplayable(alert, list);
}
catch (IOException e)
{
Alert alert = new Alert("ERror", e.toString(), null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
switchDisplayable(alert, list);
e.printStackTrace();
}
finally {
if (is != null) {
try {
is.close();
} catch (Exception ex) {
System.out.println("Failed to close is!");
}
try {
os.close();
} catch (Exception ex) {
System.out.println("Failed to close os!");
}
}
if (server != null) {
try {
server.close();
} catch (Exception ex) {
System.out.println("Failed to close server!");
}
}
if (client != null) {
try {
client.close();
} catch (Exception ex) {
System.out.println("Failed to close client!");
}
}
}
我的客户端应用程序与服务器连接,但当我尝试发送数据时,例如
os.write("texttosend".getBytes());
我无法使用获取服务器上的文本数据。也就是说,我没有从客户端收到服务器中发送的数据包
txt=Trim(AContext->Connection->IOHandler->ReadLn());
伙计们,我哪里错了?我现在这样做行吗
或者我需要使用StreamConnection而不是SocketConnection吗
当我使用telnet发送数据时,它工作得很酷,字符串将添加到listbox中
telnet 127.0.0.1 4500
texttosend
23
asf
感谢您的帮助!!!
提前谢谢 试试看
如果没有,请尝试写入已知的工作服务器,而不是您自己创建的服务器(类似),这将帮助您确定错误在哪一端。是的,在发送字节后需要调用flush方法,但是。。。。。最后 然后,我尝试将我的连接代码包含在一个新线程中,该线程实现了Runnable,工作得非常好。现在我发现我错在哪里了 这就是您需要在下面的代码块中包含上述代码的地方
Thread t= new Thread(this);
t.start();
public void run()
{
//here paste the code
}
主要问题是您正在服务器端使用
ReadLn()
ReadLn()
在遇到数据终止符(默认终止符为LF
换行符)或出现读取超时(默认情况下Indy使用无限超时)之前不会退出。您的J2ME代码没有发送任何数据终止符,因此不需要告诉ReadLn()
何时停止读取。它与Telnet一起工作的原因是它确实发送换行字符
代码的另一个问题是
TIdTCPServer
是一个多线程组件,但您的代码正在以线程不安全的方式更新UI组件。您必须与主线程同步,例如使用Indy的TIdSync
和/或TIdNotify
类,以便从服务器的事件处理程序内部安全地更新UI。Yes man,然后我尝试将我的连接代码包含在一个新的线程中,该线程实现了Runnable,效果非常好。