Commit ac4786e9 authored by Simo Vuorinen's avatar Simo Vuorinen
Browse files

Update UDPSender

parent 6d678ddb
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
public class UDPSender extends Thread {
public static void main(String[] args) throws Exception {
InetAddress targetAddress = InetAddress.getLoopbackAddress();
DatagramSocket socket = new DatagramSocket(); //luo socketin vapaaseen porttiin paikallisessa koneessa
byte[] data = Integer.toString(5454).getBytes();
DatagramPacket packet = new DatagramPacket (data, data.length, targetAddress, 3126); //luo datagrampacketin
socket.send(packet); // lähettää paketin
socket.close(); //sulkee soketin.
System.out.println("lahetetty");
UDPReceiver();
} // main
// UDPLahettaja
private static Socket UDPReceiver() throws IOException {
int porttiNo = 5454;
int yrityskerta = 0;
ServerSocket pleb = new ServerSocket(porttiNo);
Socket soketti = new Socket();
while (yrityskerta < 5) {
try {
//lahetaUDP(); // lähetetään UDP paketti asiakkaalle
pleb.setSoTimeout(5000); // soketti odottaa yhteydenottoa 5 sek
soketti = pleb.accept();
pleb.close();
System.out.println("TCP muodostettu");
break;
} catch (SocketException e) {
yrityskerta++;
System.out.println("Ei onnistunut");
}
}
return soketti;
}
}
import java.io.*;
import java.net.*;
public class UDPSender {
public static void main(String[] args) throws Exception {
InetAddress targetAddress = InetAddress.getLoopbackAddress();
DatagramSocket socket = new DatagramSocket(); //luo socketin vapaaseen porttiin paikallisessa koneessa
byte[] data = Integer.toString(54545).getBytes();
DatagramPacket packet = new DatagramPacket (data, data.length, targetAddress, 3126); //luo datagrampacketin
socket.send(packet); // lähettää paketin
socket.close(); //sulkee soketin.
} // main
} // UDPLahettaja
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment