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

Tän ku pyörittää käyntiii nii se suorittaaa to metodi (kerääSoketit) ekan...

Tän ku pyörittää käyntiii nii se suorittaaa to metodi (kerääSoketit) ekan forloopi jonka jälkee en oo mitää hajuu mitä tapahtuuu:
 Connection to 5'th adder created.
Trying to connect to 1240
Connection to 6'th adder created.
Trying to connect to 1241
Connection to 7'th adder created.
Making test 1 in set 1
Received exception while testing ... aborting.
Exception: java.net.SocketTimeoutException: Read timed out
Connections to calculators closing ...
... thread done.

Ja no periaattees ainoo asia mikä puuttuu enää ni on et jos toi toimii ni sit pitäis lisätä ne kaikki metodit mist ne sulkee noi soketit su muut mut muute pitäis olla iha ok.
parent 256d4d6e
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>();
boolean yhteys=true;
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");
KerääSoketit(t,sok,vastaanotetutsummat, l);
while(true){
int f=inpStream.readInt();
if (f==1){
int sum=0;
for (int i=0; i<=vastaanotetutsummat.size(); i++) {
sum=sum+vastaanotetutsummat.get(i);}
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 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();
s.add(soketi);
System.out.println("1");
}
System.out.println("3");
for (int j=0; j<=s.size(); j++){
try {
System.out.println("2");
int q = 0;
InputStream uusiIS = s.get(j).getInputStream();
ObjectInputStream inStream = new ObjectInputStream(uusiIS);
while(inStream.readInt()!=0) {
q=q+inStream.readInt();
System.out.print(q);
v.add(j, q);
l++;
}
}
catch (Exception e) {
System.out.println("moi");
}
}
} catch (Exception e) {
}
}
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");
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) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return -1;
}
}
}
Supports Markdown
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