使用线程的套接字写入错误

苏克什

我有以下课程

ClientDemo.java

ClientTread.java

ServerDemo.java

ServerThread.java

 public class ClientDemo {
   public static void main(String[] args) throws InterruptedException {
     try {
        Socket client=new Socket("localhost", 6666);
        while(true)
        {
            System.out.println("Hello");
            Thread th=new Thread(new ClientThread(client));
            th.start();
            System.out.println("Thread started........");
            th.sleep(1000*30);

        }

     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
    }

   }

ClientThread.java

 public class ClientThread implements Runnable {
   Socket c;
    public ClientThread(Socket client) {
    this.c=client;
}

@Override
public void run() {
    DataOutputStream dos=null;
    try {
        System.out.println("Client thread is going to write.......");
        dos = new DataOutputStream(c.getOutputStream());
        dos.writeUTF("Hello From Client");
        System.out.println("Data written by client............");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        System.out.println(e+"");
    }


      }

    }

SeverDemo.java

      public class ServerDemo {
        public static void main(String[] args) {
      try {

    ServerSocket serversocket=new ServerSocket(6666);
    System.out.println("server listening..........");
    Thread ts=new Thread( new ServerThread(serversocket.accept()));
    ts.start();
    System.out.println("server thread started.........");
            } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
        }
     }
    }

ServerThread.java

 public class ServerThread implements Runnable {
Socket s;

public ServerThread(Socket server) {
    this.s=server;
}

@Override
public void run() {
    DataInputStream dis;
    try {
        dis = new DataInputStream(s.getInputStream());
        String message=dis.readUTF();
        System.out.println(message);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

}

该代码执行一次完美,之后我得到以下错误

在客户端控制台中

Hello

Thread started........

Client thread is going to write.......

Data written by client............

Hello

Thread started........

Client thread is going to write.......

java.net.SocketException: Connection reset by peer: socket write error
rahulserver

在您的ServerThread中,

public void run() {
    DataInputStream dis;
    try {
        dis = new DataInputStream(s.getInputStream());
        String message=dis.readUTF();
        System.out.println(message);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

从客户端读取一行后,执行暂停。因此服务器代码退出。因此,您正在获取异常。因此,您可以做的是:

public void run() {
        DataInputStream dis;
        try {
             while(true)
             {
                dis = new DataInputStream(s.getInputStream());
                String message=dis.readUTF();
                System.out.println(message);
             }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

在这里,我们在一个while循环中将代码包含在run方法内,以使服务器永远运行。您可以提出自己的逻辑。

希望有帮助!!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用线程时的套接字问题

来自分类Dev

使用套接字和线程写文件

来自分类Dev

使用多线程的基本套接字聊天程序会引发错误

来自分类Dev

线程UDP套接字

来自分类Dev

Java中的MultipartEntityBuilder HttpPost套接字写入错误

来自分类Dev

对等方重置连接:FTP期间套接字写入错误

来自分类Dev

写入数据后向套接字发送错误响应

来自分类Dev

套接字错误:地址已在使用中

来自分类Dev

IOError:使用BeautifulSoup的[Errno套接字错误]

来自分类Dev

com.microsoft.sqlserver.jdbc.SQLServerException:对等连接重置:使用SQL Server 2008的grails应用中的套接字写入错误

来自分类Dev

com.microsoft.sqlserver.jdbc.SQLServerException:对等连接重置:使用SQL Server 2008的grails应用中的套接字写入错误

来自分类Dev

使用Qdatastream从套接字读取数据并写入文件

来自分类Dev

如何使用线程JAVA从套接字读取输入

来自分类Dev

Python线程将无法开始使用套接字

来自分类Dev

使用libev的多线程套接字服务器

来自分类Dev

使用线程来处理套接字的多个读/写操作?

来自分类Dev

如何使用多线程从套接字读取请求

来自分类Dev

写入uWSGI Unix套接字

来自分类Dev

写入uWSGI Unix套接字

来自分类Dev

Boost :: ASIO多线程将陈旧数据写入套接字?

来自分类Dev

套接字和线程python

来自分类Dev

循环创建套接字/线程

来自分类Dev

套接字用于多线程?

来自分类Dev

套接字和线程python

来自分类Dev

循环创建套接字/线程

来自分类Dev

从主线程关闭套接字

来自分类Dev

套接字错误-python

来自分类Dev

套接字错误-python

来自分类Dev

错误:在原始套接字中使用sendto()时,地址错误

Related 相关文章

热门标签

归档