连接代码有问题
这是我的类,用于连接并向服务器发送命令我尝试在方法中使用单独的连接代码 但是当我测试 (SocketConnect) 方法时,我发现它不起作用。 我的代码
public class ConnAndSend {
static Socket socket = null;
static void SendCommand(String cmnd) {
DataOutputStream dataOutputStream = null;
try {
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF(cmnd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
};
}
// method to connect to server
static void SocketConnect(String SrvrIP,int SrvrPrt) {
// Socket socket = null;
try {
socket = new Socket(SrvrIP, SrvrPrt);
} catch (IOException e) { e.printStackTrace();}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
}
this is my class to connect and send commands to server i try separate connect code in method
but when i test the (SocketConnect) method i found it doesn't work.
my code
public class ConnAndSend {
static Socket socket = null;
static void SendCommand(String cmnd) {
DataOutputStream dataOutputStream = null;
try {
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF(cmnd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
};
}
// method to connect to server
static void SocketConnect(String SrvrIP,int SrvrPrt) {
// Socket socket = null;
try {
socket = new Socket(SrvrIP, SrvrPrt);
} catch (IOException e) { e.printStackTrace();}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除静态修饰符!!
删除所有出现的单词static
删除代码中
:您在
finally
中关闭套接字(为什么?)这也是错误的!Remove the static modifier!!
Remove all the occurances of the word static
In your code:
you are closing the socket in
finally
(why?) this is wrong also!您确定您的清单文件具有正确的权限吗?
Have you made sure your manifest file has the correct permissions?
看起来您在有机会使用套接字之前就关闭了它,或者您之前没有调用 SocketConnect 。你知道如何制作套接字然后关闭它吗?
您需要创建套接字,在 SendCommand 中使用它进行连接,然后关闭它。我不太清楚为什么你需要将两者分开,但我相信这是你的问题,你在使用套接字连接之前调用 close 或者你根本没有创建套接字和 SendCommand 使用“null”进行连接。
It looks like you are closing the socket before you even get a chance to use it or you are'nt calling SocketConnect before . Do you see how you you make the socket, then you close it?
You need to make the socket, use it to connect in your SendCommand, then close it. I'm not quite sure why you need to keep the two separate, but I believe that is your problem, you are calling close before you use the socket to connect or you simply aren't making the socket and
SendCommand
is using "null" to connect.