简体   繁体   English

我收到 java.net.SocketException: Connection reset by peer: socket write error ,当在类截图中执行主函数时

[英]I am getting java.net.SocketException: Connection reset by peer: socket write error ,when executing main function in class screenshot

I am getting java.net.SocketException: Connection reset by peer: socket write error ,when executing main function in class screenshot.我在类屏幕截图中执行主函数时收到 java.net.SocketException: Connection reset by peer: socket write error 。

I am getting a problem which i dont get when i run the main function with simple words like "hello".我遇到了一个问题,当我用“你好”这样的简单词运行主函数时,我没有遇到这个问题。 I am trying to make a program to send a ss of another computer wirelessly我正在尝试制作一个程序来无线发送另一台计算机的 ss

Help solutions or what went wrong are appriciated帮助解决方案或出了什么问题是appriciated

import java.io.*;
import java.util.*;
import java.awt.image.*;
import java.awt.*;
import java.awt.event.*;
public class screenshot
{
    public static void main(){
        Link l=new Link("localhost",1111);
        System.out.println(Link.getIpAddress());
        String s[]=its();
        for(int i=0;i<s.length;i++){
            l.send(s[i]);//l.send("hello22");
        }
        for(int i=0;i<s.length;i++){
            System.out.println(l.recieve());
        }

        //System.out.println(l.recieve());
    }

    public static String[] its(){
        String[] tbo=null;
        try{
            Robot r=new Robot();
            BufferedImage img=r.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            System.out.println(img.getWidth()+" "+img.getHeight());
            tbo=new String[img.getWidth()];
            for(int i=0;i<tbo.length;i++){
                tbo[i]="";
            }
            for(int i=0;i<img.getWidth();i++){
                for(int j=0;j<img.getHeight();j++){
                    tbo[i]+=img.getRGB(i,j);//out+=img.getRGB(i,j);
                }
                System.out.println(i);
                //out+="b";
            }
            //for(int i=0;i<img.getWidth();i++){
            //    out+=tbo[i]+"\n";
            //}
        }catch(Exception e){}
        //Base bo=new Base("1234567890-\n");
        //Base bn=new Base("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
        //BaseNumber bnum=new BaseNumber(out,bo);
        //out=bnum.valueAs(bn).toString();
        return tbo;
    }

    public static void main2(){
        System.out.println(its());
    }
}

Link class that creates a link object to send to localhost in this situation在这种情况下创建链接对象以发送到本地主机的链接类

class Link
{
    final public String ip;final public int port;Listener l;
    //protected ArrayList<String> queue=new ArrayList<String>();
    public Link(String ip,int port)
    {
        this.ip=ip;
        this.port=port;
        this.l=new Listener(port);
        l.start();
    }

    public static String getIpAddress(){
        try{
            return InetAddress.getLocalHost().getHostAddress();
        }catch(Exception e){
            System.out.println("An error ocurrerd in getIp\n "+e);
            System.exit(0);
            return null;
        }
    }

    public void send(String st)
    {
        try{      
            Socket s=new Socket(ip,port);  
            DataOutputStream dout=new DataOutputStream(s.getOutputStream()); 
            dout.writeUTF(st);  
            dout.flush();  
            dout.close();  
            s.close();  
        }catch(Exception e){
            System.out.println("An error ocurrerd in send\n "+e);
            e.printStackTrace();
            System.exit(0);
        }
    }

    public String recieve(){
        while(l.queue.size()==0);
        String s=l.queue.get(0);
        l.queue.remove(0);
        return s;
    }
}
class Listener extends Thread{
    ArrayList<String> queue=new ArrayList<String>();int port;
    Listener(int port){
        this.port=port;
    }
    public void run(){
        try{  
            while(true){
                ServerSocket ss=new ServerSocket(port);  
                Socket s=ss.accept();
                DataInputStream dis=new DataInputStream(s.getInputStream());  
                String str=(String)dis.readUTF();  
                queue.add(str);
                ss.close();
            }
        }catch(Exception e){
            System.out.println("An error ocurrerd in recieve\n "+e);
            System.exit(0);

        }

    }
}
while(true){
            ServerSocket ss=new ServerSocket(port);  
            Socket s=ss.accept();
            DataInputStream dis=new DataInputStream(s.getInputStream());  
            String str=(String)dis.readUTF();  
            queue.add(str);
            ss.close();
        }

Ideally you should create & close a socket outside while loop and just read within the while loop.理想情况下,您应该在 while 循环外创建并关闭套接字,并在 while 循环内读取。

Similarly for Send, you should not create a socket every time you have to send.同样对于发送,您不应该每次必须发送时都创建套接字。 If you have to do that than your Server has to have accept() as well in while loop.如果你必须这样做,那么你的服务器必须在 while 循环中也有 accept() 。

Hope this resolves your issue.希望这能解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.SocketException: Connection reset by peer: socket write error 提供文件时 - java.net.SocketException: Connection reset by peer: socket write error When serving a file java.net.SocketException:对等连接重置:传输音频时套接字写入错误 - java.net.SocketException: Connection reset by peer: socket write error When transmitting Audio (java.net.SocketException) 在处理请求时被捕获:连接被对等重置:套接字写入错误 - (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error “ java.net.SocketException:对等体重置连接:套接字写入错误”我该如何解决 - “java.net.SocketException: Connection reset by peer: socket write error” How can I solve it 我收到java.net.SocketException:连接重置错误 - I am getting a java.net.SocketException: Connection reset error ClientAbortException:java.net.SocketException:由peer重置连接:套接字写入错误 - ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error ClientAbortException:java.net.SocketException:对等重置连接:套接字写入错误 - ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error Influxdb-java:org.influxdb.InfluxDBIOException:java.net.SocketException:通过对等方重置连接:套接字写入错误 - influxdb-java: org.influxdb.InfluxDBIOException: java.net.SocketException: Connection reset by peer: socket write error java.net.socketexception connection reset by peer socket write error 通过Jenkins在Tomcat7上部署war时(使用Maven) - java.net.socketexception connection reset by peer socket write error While deploying war on Tomcat7 through Jenkins (Using Maven) 当发送沉重的json将近5MB时,它将给我java.net.SocketException:连接被同级重置:套接字写入错误 - While sending heavy json almost 5MB it will give me java.net.SocketException: Connection reset by peer: socket write error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM