Commit 2ac741da authored by Simo Vuorinen's avatar Simo Vuorinen
Browse files

Add new file

parent 526ca59c
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 {
Socket tcpsocket = UDPReceiver();
// Muodostetaan oliovirrat
ObjectInputStream inpStream = new ObjectInputStream(tcpsocket.getInputStream());
ObjectOutputStream outpStream = new ObjectOutputStream(tcpsocket.getOutputStream());
//Lue - asettaa timeoutin.
tcpsocket.setSoTimeout(50000);
int t= lue(inpStream, outpStream);
int[] potnumbers =new int[t];
int l = 0;
ArrayList<Integer> vastaanotetutsummat = new ArrayList<Integer>();
ArrayList<Socket> sok = new ArrayList<Socket>();
boolean yhteys=true;
int f= 0;
if (t == -1) {
return;
} else {
for (int h = 0; h < t; h++ ) {
try{
int p = 1234+h;
potnumbers[h]=p;
outpStream.writeInt(potnumbers[h]);
outpStream.flush();
} catch (Exception e) {
System.out.print("moi");
return;
}
}
}
for (int i = 0; i<t; i++) {
int p = 1234+i;
ServerSocket plib = new ServerSocket(p);
Socket so = new Socket();
so = plib.accept();
sok.add(i, so);
plib.setSoTimeout(5000);
plib.close();
}
for (int k=0; k<t; k++) {
final LaskeLuvut laskuri= new LaskeLuvut(vastaanotetutsummat, k, sok);
laskuri.run();
}
System.out.println(vastaanotetutsummat);
System.out.println("456");
while(yhteys){
f=inpStream.readInt();
if (f==1){
int sum=0;
for (int j=0; j<vastaanotetutsummat.size(); j++) {
sum=sum+vastaanotetutsummat.get(j);}
outpStream.writeInt(sum);
}else if (f==2){
Collections.sort(vastaanotetutsummat);
outpStream.writeInt(vastaanotetutsummat.get(vastaanotetutsummat.size()-1));
}else if (f==3) {
outpStream.writeInt(l);
}else if (f==0) {
yhteys=false;
}
}
}
public static class LaskeLuvut{
private int k;
int l=0;
private ArrayList<Integer> vastaanotetutsummat;
private int z=0;
ArrayList<Socket> sok ;
LaskeLuvut(ArrayList<Integer> vastaanotetutsummat, int k, ArrayList<Socket> sok){
this.sok=sok;
this.k=k;
this.vastaanotetutsummat=vastaanotetutsummat;
}
public void run() {
try {
boolean c = true;
int q = 0;
InputStream uS = sok.get(k).getInputStream();
ObjectInputStream inStream = new ObjectInputStream(uS);
while (c) {
if(inStream.readInt()==0) {
inStream.close();
} else {
q=q+inStream.readInt();
vastaanotetutsummat.add(z, k);
l++;
}
}
inStream.close();
}catch (Exception e) {
}
}
}
private static Socket UDPReceiver() throws IOException {
int porttiNo = 1234;
int yrityskerta = 0;
ServerSocket pleb = new ServerSocket(porttiNo);
Socket soketti = new Socket();
while (yrityskerta < 5) {
try {
sendUDP(); // lähetetään UDP paketti asiakkaalle
pleb.setSoTimeout(50000); // soketti odottaa yhteydenottoa 5 sek
soketti = pleb.accept();
pleb.close();
System.out.println("TCP muodostettu");
System.out.println(soketti);
return soketti;
} catch (SocketException e) {
yrityskerta++;
System.out.println("Ei onnistunut");
}
}
return null;
}
private static void sendUDP() {
try {
InetAddress targetAddress = InetAddress.getLoopbackAddress();
DatagramSocket socket = new DatagramSocket(); //luo socketin vapaaseen porttiin paikallisessa koneessa
byte[] data = Integer.toString(1234).getBytes();
DatagramPacket packet = new DatagramPacket (data, data.length, targetAddress, 3126); //luo datagrampacketin
socket.send(packet); // lähettää paketin
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static int lue(ObjectInputStream i, ObjectOutputStream o) {
try {
return i.readInt();
} catch (Exception e){
try {
o.writeInt(-1);
} catch (IOException e1) {
e1.printStackTrace();
}
return -1;
}
}}
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