Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.nio.channels.DatagramChannel;
- public class Server3 extends Thread{
- byte[] sendData = new byte[50];
- boolean isRunning;
- InetAddress ipAddr;
- int portNb;
- DatagramSocket sendSock;
- public Server3(InetAddress ipAddr, int portNb) throws SocketException {
- super();
- this.ipAddr = ipAddr;
- this.portNb = portNb;
- sendSock = new DatagramSocket();
- DatagramChannel channel = null;
- try {
- channel = DatagramChannel.open();
- } catch (IOException e) {
- e.printStackTrace();
- }
- sendSock = channel.socket();
- sendSock.setReuseAddress(true);
- }
- @Override
- public void run() {
- while(isRunning){
- sendData = "server msg here".getBytes();
- DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipAddr, portNb);
- try {
- sendSock.send(sendPacket);
- Thread.sleep(1000);
- System.out.println("sent msg");
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public void startSending(){
- isRunning=true;
- start();
- }
- public void stopReceiving(){
- isRunning=false;
- }
- public static void main(String[] args) throws IOException {
- Server3 s = new Server3(InetAddress.getByName("10.0.2.2"),57111);
- s.startSending();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement