- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
public void write(byte[] bytesToSend) {
// System.out.println("Write begin.");
try {
socket.setSoTimeout(10000);
packageID = 0;
// Add bytes to send
saveBytesToPackeges(bytesToSend);
// Begin new Thread to be sure it was delivered
mySendChecker = new SendChecker();
Thread myThread = new Thread(mySendChecker);
myThread.start();
byte[] buf2 = new byte[50];
// Sending number of packeges
byte[] buf = bytesFromInt(numberOfPackages);
for (int i = 0; i < 4; i++) {
buf2[i] = buf[i];
}
packageID = -2;
buf = packetFromBytes(buf2, sizeOfPackage - 4);
DatagramPacket myPacket = new DatagramPacket(buf, buf.length,
addressToSendTo, portToSendTo);
socket.send(myPacket);
mySendChecker.setPacket(-2, myPacket);
// recieve answer
socket.receive(myPacket);
buf = myPacket.getData();
mySendChecker.removePacket(-2);
// send delieverer
buf = packetFromBytes(buf2, sizeOfPackage - 4);
socket.send(myPacket);
// starting to send data
for (int i = 0; i < numberOfPackages; i++) {
myPacket = new DatagramPacket(bytesToSendSave[i],
bytesToSendSave[i].length, addressToSendTo,
portToSendTo);
socket.send(myPacket);
mySendChecker.setPacket(i, myPacket);
}
// start to recieve answers
int numberOfPackegeRecieved = 0;
while (true) {
buf = new byte[8];
if (mySendChecker.isEmpty()) {
break;
}
myPacket = new DatagramPacket(buf, buf.length);
socket.receive(myPacket);
buf = myPacket.getData();
numberOfPackegeRecieved = intFromBytes(buf, 4);
if (-3 != intFromBytes(buf, 4)) {
continue;
}
numberOfPackegeRecieved = intFromBytes(buf, 0);
mySendChecker.removePacket(numberOfPackegeRecieved);
myPacket = new DatagramPacket(buf, buf.length, addressToSendTo,
portToSendTo);
socket.send(myPacket);
}
buf = bytesFromInt(-5);
myPacket = new DatagramPacket(buf, buf.length, addressToSendTo,
portToSendTo);
socket.send(myPacket);
mySendChecker.setPacket(-5, myPacket);
// System.out.println("WRITE Sending -5");
while (true) {
buf = new byte[4];
myPacket = new DatagramPacket(buf, buf.length);
socket.receive(myPacket);
buf = myPacket.getData();
if (-5 == intFromBytes(buf, 0)) {
// System.out.println("WRITE recieved -5");
break;
}
}
mySendChecker.end();
socket.setSoTimeout(100);
while (true) {
try {
socket.receive(myPacket);
} catch (SocketTimeoutException e) {
break;
}
}
socket.setSoTimeout(0);
// System.out.println("Write end.");
... TO BE CONTINUED ...
Лучшая реализация потокового протокола поверх UDP среди студентов. Остальное в таком же стиле.
This is obvious 14.05.2010 17:28 # 0
Lure Of Chaos 14.05.2010 17:49 # 0
было бы неплохо если бы
Max ID 20.05.2010 17:48 # 0
Чего только стоят recIEve, packeges и .т.д. Очевидно, имелось ввиду пакеты, то есть, packets... А, например, -5 == intFromBytes(buf, 0). Круто, а ?