连接代码有问题

发布于 2024-11-27 22:39:48 字数 1528 浏览 1 评论 0原文

这是我的类,用于连接并向服务器发送命令我尝试在方法中使用单独的连接代码 但是当我测试 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你又不是我 2024-12-04 22:39:48
  • 删除静态修饰符!!

    删除所有出现的单词static


删除代码中

    socket = new Socket(SrvrIP, SrvrPrt);   
} catch (IOException e) { e.printStackTrace();}
    finally{
        if (socket != null){
            try {
            socket.close();

:您在 finally 中关闭套接字(为什么?)这也是错误的!

  • Remove the static modifier!!

    Remove all the occurances of the word static


In your code:

    socket = new Socket(SrvrIP, SrvrPrt);   
} catch (IOException e) { e.printStackTrace();}
    finally{
        if (socket != null){
            try {
            socket.close();

you are closing the socket in finally (why?) this is wrong also!

凉世弥音 2024-12-04 22:39:48

您确定您的清单文件具有正确的权限吗?

<uses-permission android:name="android.permission.INTERNET" />

Have you made sure your manifest file has the correct permissions?

<uses-permission android:name="android.permission.INTERNET" />
仄言 2024-12-04 22:39:48

看起来您在有机会使用套接字之前就关闭了它,或者您之前没有调用 SocketConnect 。你知道如何制作套接字然后关闭它吗?

    socket = new Socket(SrvrIP, SrvrPrt);   
} catch (IOException e) { e.printStackTrace();}
    finally{
        if (socket != null){
            try {
            socket.close();

您需要创建套接字,在 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?

    socket = new Socket(SrvrIP, SrvrPrt);   
} catch (IOException e) { e.printStackTrace();}
    finally{
        if (socket != null){
            try {
            socket.close();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文