使用套接字从服务器获取请求 [英] Getting request from the server using sockets
本文介绍了使用套接字从服务器获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨.我需要从服务器接收响应,该服务器在request(os.write(data))之后发送响应.
Hi. I need to receive a response from server, which sends response after request(os.write(data)).
try{
byte[] data = __selectedString.getBytes();
os.write(data);
}
catch(IOException x)
{
}
try{
InputStream in = connection.openInputStream();//<== Here I got an exception like "no more input streams available".
int ch;
String str = null;
StringBuffer sb = new StringBuffer();
while ((ch = in.read()) != -1) {
sb.append((char)ch);
}
list.append(sb.toString(), null);
in.close();
list.deleteAll();
list.append(str, null);
in.close();
}
catch(IOException e)
{
}
以这种方式成功执行连接:
Connection successfully performs in this way:
try{
SocketConnection sc = (SocketConnection)
Connector.open("socket://" + IP +":13000");
//OutputStream os = null;
try{
os = sc.openOutputStream();
byte[] data = "Hello from a socket!".getBytes();
os.write(data);
}
finally{
//sc.close();
//os.close();
}
}
我已经用Google搜索过,并且只发现了有关http-Connection的信息,但是我通过套接字直接连接到PC.
请帮忙.
I have Googled and found only about http-Connection, but I connecting directly to my PC via sockets.
Please, help.
推荐答案
您的代码看起来不完整,但我理解(或希望我这样做;)).我想您想要一个简单的tcp/ip客户端/服务器连接.
以下是一些帮助您入门的链接:
http://www.ibm.com/developerworks/java/library/wi-jio/ [ ^ ]
http://developers.sun.com/mobility/midp/articles/midp2network/ [ ^ ]
还是您可能是说用J2SE而不是J2ME:
http://java.sun.com/developer/technicalArticles/releases/nio/ [ ^ ]
http://www.tutorialspoint.com/java/java_networking.htm [
Your code looks incomplete but I understood (or hope I did ;) ) what you want to so. I guess you want a simple tcp/ip client/server connection.
Here are some links to get you started:
http://www.ibm.com/developerworks/java/library/wi-jio/[^]
http://developers.sun.com/mobility/midp/articles/midp2network/[^]
or did you maybe mean J2SE instead of J2ME:
http://java.sun.com/developer/technicalArticles/releases/nio/[^]
http://www.tutorialspoint.com/java/java_networking.htm[^]
Good luck!
是的,我用JAVA ME编写,但是我不知道为什么程序无法正常工作.
这是NetBeanse 6.9项目的存档:
http://www.sendspace.com/file/972g2a
我非常希望这个问题能够得到解决.
附加信息-存档中.
请帮助.
Yes I writing in JAVA ME, but i don''t know why program doesn''t work correctly.
this is an archive with NetBeanse 6.9 project:
http://www.sendspace.com/file/972g2a
I very hope that problem will be solved.
Additional information - in archive.
Please, help.
这篇关于使用套接字从服务器获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文