📄 server.java
字号:
endTransfer();
}//end if (sendEndPacket == true)
count++;
firstTime = 1;
ackReceived = false;
sendEndPacket = false;
}//end if(ackReceived == true)
}//end while ((c = in.read(sendData)) != -1)
// Reset the count
count=1;
// Close input stream
in.close();
/*DEBUG
displayMessage("\ncount " + count);
displayMessage("\nfirstTime " + firstTime);
displayMessage("\nackReceived " + ackReceived);
*/
}
catch (IOException e)
{
//e.printStackTrace();
displayMessage("\nERROR: The file cannot be found.");
displayMessage("\n Please check the filename, and try again.");
}
}
public void actionPerformed(ActionEvent a)
{
Object source = a.getActionCommand();
//Can use either of these methods...
//if ("Reset".equals(a.getActionCommand()))
if (source == "Reset")
{
// Reset values
txtPort.setText("69");
txtTimeout.setText("6000");
txtRetries.setText("3");
chkVerbose.setText("Verbose Output Enabled");
chkVerbose.setSelected(true);
verboseOutput=true;
// Set change flags
chngPort = true;
chngTimeout = true;
chngRetries = true;
chngVerbose = true;
displayMessage("\nSettings reset to default.");
// Enable the main tab
setPaneStatus(false);
// Enable the Apply button
btnApply.setEnabled(true);
}
// Apply any changes
if (source == "Apply")
{
// Change port
if(chngPort==true)
{
// Set the port
s = new Integer(txtPort.getText());
// First close the current socket
try
{
socket.close();
}
catch (Exception e)
{
//e.printStackTrace();
displayMessage("\nERROR: Trying to close socket!");
}
openSocket();
chngPort=false;
}
// Set the timout
if(chngTimeout==true)
{
maxTimeout = new Integer(txtTimeout.getText());
displayMessage("\nTimeout set to " + maxTimeout + " seconds");
chngTimeout=false;
}
// Set Retries
if(chngRetries==true)
{
maxRetries = new Integer(txtRetries.getText());
displayMessage("\nNumber of Retries set to " + maxRetries);
chngRetries=false;
}
// Set verboseOutput
if(chngVerbose==true)
{
// Conditions created to set the verbose status
if(chkVerbose.getText().equals("Verbose Output Enabled"))
{
verboseOutput=true;
displayMessage("\nVerbose Output Enabled");
}
else if(chkVerbose.getText().equals("Verbose Output Disabled"))
{
verboseOutput=false;
displayMessage("\nVerbose Output Disabled");
}
else if(verboseOutput==true)
{
verboseOutput=true;
displayMessage("\nVerbose Output Enabled");
}
chngVerbose=false;
}
// Enable the main tab
setPaneStatus(true);
// Disable the Apply button
btnApply.setEnabled(false);
}// end if (source == "Apply")
if (source == "Verbose Output Enabled")
{
chkVerbose.setText("Verbose Output Disabled");
// Disable the main tab
setPaneStatus(false);
// Enable the Apply button
btnApply.setEnabled(true);
// Flag change
chngVerbose=true;
}
if (source == "Verbose Output Disabled")
{
chkVerbose.setText("Verbose Output Enabled");
// Disable the main tab
setPaneStatus(false);
// Enable the Apply button
btnApply.setEnabled(true);
// Flag change
chngVerbose=true;
}
if (source == "Clear Output")
{
txtArea.setText("");
}
}//end public void actionPerformed(ActionEvent a)
public class pressKey implements KeyListener, ActionListener
{
protected JTextField text;
public pressKey(JTextField t)
{
text = t;
}
public void keyTyped(KeyEvent e)
{
String fieldName = text.getName();
//System.out.println("fieldName " + fieldName);
// Based upon the name is sets the flag to allow
// option change.
if(fieldName.equals("Port Field"))
{
//System.out.println("Port Field");
chngPort=true;
}
if(fieldName.equals("Timeout Field"))
{
//System.out.println("Timeout Field");
chngTimeout=true;
}
if(fieldName.equals("Retries Field"))
{
//System.out.println("Retries Field");
chngRetries=true;
}
//Allow only 5 integers
String valueCount = text.getText();
//System.out.println("valueCount: " + valueCount);
if (valueCount.length() > 4)
{
e.consume();
}
//Allow only integers
char c = e.getKeyChar();
if (!(Character.isDigit(c) ||
(c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE)))
{
e.consume();
}
// Disable the main tab
setPaneStatus(false);
// Enable the apply button
btnApply.setEnabled(true);
}
public void keyPressed(KeyEvent e)
{
// System.out.println("keyPressed: " + e);
}
public void keyReleased(KeyEvent e)
{
// System.out.println("keyReleased: " + e);
}
public void actionPerformed(ActionEvent e)
{
// System.out.println("actionPerformed: " + e);
}
}//end public class pressKey implements KeyListener, ActionListener
// Register a change listener to the JTabbedPane
public class paneChangeListner implements ChangeListener
{
protected JTabbedPane panel;
public paneChangeListner(JTabbedPane p)
{
panel = p;
}
// This method is called whenever the selected tab changes
public void stateChanged(ChangeEvent evt)
{
JTabbedPane pane = (JTabbedPane)evt.getSource();
// Get current tab
int selectedPane = pane.getSelectedIndex();
if(selectedPane==1)
{
//System.out.println("Cannot exit pane #" + selectedPane);
//pane.setEnabledAt(0, false);
}
}
}
// Allows enabling.disabling of tabs on the JTabbedPane
public void setPaneStatus(boolean status)
{
if(status==true)
{
//System.out.println("Enabled");
tabbedPane.setEnabledAt(0, true);
}
if(status==false)
{
//System.out.println("Disabled");
tabbedPane.setEnabledAt(0, false);
}
}
class popupListener extends MouseAdapter
{
JPopupMenu popup;
popupListener(JPopupMenu popupMenu)
{
popup = popupMenu;
}
public void mousePressed(MouseEvent e)
{
showPopup(e);
}
public void mouseReleased(MouseEvent e)
{
showPopup(e);
}
public void mouseExited(MouseEvent e)
{
//hidePopup(e);
}
private void showPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
popup.show(e.getComponent(),e.getX(), e.getY());
popup.setVisible(true);
}
}
private void hidePopup(MouseEvent e)
{
//popup.hide();
popup.setVisible(false);
}
}
public void endTransfer()
{
displayMessage("\n FINISHED TRANSFER of " + fileToRead);
try
{
socket.close();
firstTime=0;
ackReceived=false;
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void errorDuringTransfer()
{
// Stop transfer this way if retries have maxed out
if(retries==maxRetries)
{
socketInitialised=false;
displayMessage("\n\nMaximum retries reached...");
displayMessage("\nFile transfer was unsuccessful.");
retries=0;
}//end if(retries==maxRetries)
}
public void sendRetry()
{
// If there's no ACK and we haven't used up our retries
// then attempt to resend the packet
//while (!ackReceived & (retries<maxRetries))
//{
tryingResend=true;
retries++;
displayMessage("\nRetrying attempt # " + retries);
//Attempt to resend the packet
if(retries!=maxRetries)
{
try // Resending packet
{
// Read in the backed up packet and then send that.
FileInputStream inBak = new FileInputStream("bakPckt");
inBak.read(backupData);
backupPacket.setData(backupData);
// DEBUG
// Check the backup packet is correct by writing to a file.
try
{
FileOutputStream outBAK = new FileOutputStream("File-"+retries);
outBAK.write(backupPacket.getData());
outBAK.close();
}
catch (IOException e)
{
displayMessage("\nERROR: Unable to create file.");
displayMessage("\n During verification of backup packet.");
}
inBak.close();
socket.send(backupPacket);
waitForAck();
}
catch (IOException e)
{
displayMessage("IOException e " + e);
}
}
//}//end while (!ackReceived & (retries!=maxRetries))
if(retries==maxRetries)
{
tryingResend=false;
errorDuringTransfer();
}//end if(retries==maxRetries)
}
public void displayMessage(final String messageToDisplay)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
txtArea.append(messageToDisplay);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -