Commit 646c4178 authored by Simo Vuorinen's avatar Simo Vuorinen
Browse files

Delete Tässäuusin versio

parent dd31eb75
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;
import java.util.Random;
public class UdpSender extends Thread {
private ArrayList<Integer> vastaanotetutsummat = new ArrayList<Integer>();
private ArrayList<Socket> sok = new ArrayList<Socket>();
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>();
ArrayList<ServerSocket> sok2 = new ArrayList<ServerSocket>();
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;
}
}
}
System.out.print("lol");
for (int i = 0; i <=t; i++){
int p =1234+i;
ServerSocket plab = new ServerSocket(p);
Socket soketi = new Socket();
sok.add(soketi);
sok2.add(plab);
System.out.println(soketi);
System.out.println("1");
}
System.out.println("3");
LaskeLuvut(sok, vastaanotetutsummat , l,sok2);
System.out.println(inpStream.readInt());
while(true){
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 void LaskeLuvut(ArrayList<Socket> s1, ArrayList<Integer> v1 , int l1, ArrayList<ServerSocket> ss) {
try {
for (int i=0; i<=s1.size(); i++) {
int q = 0;
ServerSocket o1= ss.get(i);
Socket o = s1.get(i);
o=o1.accept();
InputStream uusiIS = o.getInputStream();
ObjectInputStream inStream = new ObjectInputStream(uusiIS);
if(inStream.readInt()==0) {
inStream.close();
} else {
q=q+inStream.readInt();
System.out.print(q);
v1.add(i, q);
l1++;
}
}
}catch (Exception e) {
}
}
public static void KerääSoketit(int t, ArrayList<Socket> s, ArrayList<Integer> v, int l) {
try {
for (int i = 0; i <=t; i++){
int p =1234+i;
ServerSocket plab = new ServerSocket(p);
Socket soketi = new Socket();
soketi=plab.accept();
System.out.println(soketi);
s.add(soketi);
System.out.println("1");
}System.out.println("2");
}
catch (Exception e) {
System.out.println("moi");
}
System.out.println("3"); }
public static void LueSoketinluvut(ArrayList<Socket> s1, ArrayList<Integer> v1 , int l1) {
for (int i=0; i<=s1.size(); i++){
try {
int q = 0;
InputStream uusiIS = s1.get(i).getInputStream();
ObjectInputStream inStream = new ObjectInputStream(uusiIS);
if(inStream.readInt()==0) {
inStream.close();
} else {
q=q+inStream.readInt();
System.out.print(q);
v1.add(i, q);
l1++;
}
} catch (Exception e) {
System.out.println("moi");
}
}
}
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;
}
}
private static int lue2(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