[英]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.