package org.jnetpcap.examples;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
public class PcapSendPacketExample {
public static void main(String[] args) {
List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
StringBuilder errbuf = new StringBuilder(); // For any error msgs
/***************************************************************************
* First get a list of devices on this system
**************************************************************************/
int r = Pcap.findAllDevs(alldevs, errbuf);
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
return;
}
PcapIf device = alldevs.get(0); // We know we have atleast 1 device
/*****************************************
* Second we open a network interface
*****************************************/
int snaplen = 64 * 1024; // Capture all packets, no trucation
int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
int timeout = 10 * 1000; // 10 seconds in millis
Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
/*******************************************************
* Third we create our crude packet we will transmit out
* This creates a broadcast packet
*******************************************************/
byte[] a = new byte[14];
Arrays.fill(a, (byte) 0xff);
ByteBuffer b = ByteBuffer.wrap(a);
/*******************************************************
* Fourth We send our packet off using open device
*******************************************************/
if (pcap.sendPacket(b) != Pcap.OK) {
System.err.println(pcap.getErr());
}
/********************************************************
* Lastly we close
********************************************************/
pcap.close();
}
}
class B {
private String name;
private final List<A> list;
public B() {
list = new ArrayList<A>();
}
public void addA(A a) {
list.add(a);
}
public List<A> getListOfA {
return this.list;
}
}
// использование
B b = new B();
b.addA(new A(1, "First"));
b.addA(new A(2, "Second"));
for (A a : b.getListOfA()) {
System.out.println(a.getName());
}
// Telescoping constructor pattern - does not scale well!
public class NutritionFacts {
private final int servingSize; // (mL) required
private final int servings; // (per container) required
private final int calories; // optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g) optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}
}
// Builder Pattern
public class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int val)
{ calories = val; return this; }
public Builder fat(int val)
{ fat = val; return this; }
public Builder carbohydrate(int val)
{ carbohydrate = val; return this; }
public Builder sodium(int val)
{ sodium = val; return this; }
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();
public class OOP {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout());
JTextField textf = new JTextField();
JTextField textf2 = new JTextField();
JButton Mybutton = new JButton("Start");
frame.add(textf, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.9,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0));
frame.add(textf2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.9,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0));
frame.add(Mybutton, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.9,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(1, 2, 2, 2), 0, 0));
Mybutton.addActionListener(new ButtonActionListener()); // Ошибка
frame.setVisible(true);
frame.pack();
}
}
class ButtonActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Messege box");
}
}
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Party extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public static void deleteServer(int index) throws IOException {
BufferedReader reader = null;
PrintWriter writer = null;
try {
File file = new File(getFilePath());
String fileToWrite = "fileToWrite.txt";
reader = new BufferedReader(new FileReader(file));
writer = new PrintWriter(new FileWriter(fileToWrite));
int current = 0;
String line;
while ((line = reader.readLine()) != null) {
if (current != index) {
writer.println(line);
}
current++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
}
}