2013年12月15日星期日

socket requesting party does not answer or how to turn off automatically socket error ???

  
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;


import javax.swing.JOptionPane;

public class Receive extends Thread
{
String friendip;
String receivepath;
DatagramSocket rds;
DatagramPacket rdp;
String msg;
public Receive (String friendip, String receivepath)
{
this.friendip = friendip;
this.receivepath = receivepath;
}
public void run ()
{
try
{
try
{rds = new DatagramSocket (8880);}
catch (SocketException e)
{
JOptionPane.showMessageDialog (null, " the other party does not answer !", null, JOptionPane.OK_OPTION);
System.out.println (" no other answer ! " ) ;/ / this place want to show the other side does not receive a response after thought error ? ? how to do ? request part of the code I do not Tieshanglai , generally is make a request to another computer , and then I think he is not online, my computer could prompt other non-responders .
}
try
{
byte [] rebuf = new byte [256];
rdp = new DatagramPacket (rebuf, rebuf.length);
rds.receive (rdp); ;
msg = new String (rebuf, 0, rdp.getLength ()); ;
rds.close ();
}
catch (IOException e)
{
JOptionPane.showMessageDialog (null, " network communication error occurs , the question " + e.toString (), null, JOptionPane.OK_OPTION);
}

System.out.println ("Receive partially completed 11111" ) ;
System.out.println ("Receive msg (YES / NO):" + msg); ;
if (msg.equals ("YES"))
{
/ * int option = JOptionPane.showConfirmDialog (null, " Click OK to download or cancel ", "P2P", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION)
{
download ();
} * /
System.out.println ("Receive partially completed 22222 " ) ;
JOptionPane.showMessageDialog (null, " Click OK to download ", null, JOptionPane.OK_OPTION);
download ();

}
if (msg.equals ("NO"))
{
JOptionPane.showMessageDialog (null, " they do not agree to share this file !", null, JOptionPane.OK_OPTION) ;
}
}
catch (Exception e)
{
e.printStackTrace ();
}


}
}
------ Solution ----------------------------------- ---------
to set the receive delay in the delay did not receive a reply will throw an exception
------ For reference only ------- --------------------------------
Thank you , has been resolved.
 rds = new DatagramSocket (8880);  
byte [] rebuf = new byte [256];
rdp = new DatagramPacket (rebuf, rebuf.length);
try {
/ / set the timeout , 3 seconds
rds.setSoTimeout (3000);
rds.receive (rdp);
msg = new String (rebuf, 0, rdp.getLength ( ) ) ;
rds.close ();
System.out.println ("Receive partially completed 11111" ) ;
System.out.println ("Receive msg (YES / NO):" + msg) ;
if (msg.equals ("YES"))
{
/ * int option = JOptionPane.showConfirmDialog (null, " Click OK to download or cancel "," P2P ", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION)
{
download ();
} * /
System.out.println ("Receive partially completed 22222 " ) ;
JOptionPane.showMessageDialog (null, " Click OK to download ", null, JOptionPane.OK_OPTION);
download ();

}
if (msg.equals ("NO"))
{
JOptionPane.showMessageDialog (null, " they do not agree to share this file ! " null, JOptionPane.OK_OPTION);
}
} catch (Exception e) {
rds.close ();
JOptionPane.showMessageDialog (null, " the other party does not answer !", null, ; JOptionPane.OK_OPTION);
System.out.println (" no other answer ! " ) ;
}
posted code to solve post to share with you .

没有评论:

发表评论