-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp3ser.java
More file actions
156 lines (123 loc) · 7.69 KB
/
Copy pathtcp3ser.java
File metadata and controls
156 lines (123 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
import java.util.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class tcp3ser {
public tcp3ser (){
}
public static void main (String [] args) throws IOException { //java tcp3ser port_number
int accumulatorUdp = 0;
DatagramChannel channelUdp =null;
SelectionKey keyUDP =null;
if(args.length!=1){
System.out.println("You have to enter the parameters with this format: tcp3ser port_number");
} else {
String port = args[0]; //args[0] is the port_number
int portServer = Integer.parseInt(port);
Selector selector= Selector.open();
Map<SocketChannel, Integer> map = new HashMap<>(); //es un mapa en el que guardamos cada cliente con su acumulador
try{
//UDP
channelUdp = DatagramChannel.open();
SocketAddress serverAddressUdp = new InetSocketAddress(portServer);
channelUdp.socket().bind(serverAddressUdp);
channelUdp.configureBlocking(false);
keyUDP = channelUdp.register(selector, SelectionKey.OP_READ| SelectionKey.OP_WRITE);
//TCP
ServerSocketChannel socketServerTcpChannel = ServerSocketChannel.open();
ServerSocket serverChann=socketServerTcpChannel.socket();
SocketAddress serverAddress = new InetSocketAddress(portServer);
serverChann.bind(serverAddress);
socketServerTcpChannel.configureBlocking(false);
socketServerTcpChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (SocketException exception1) {
System.out.println("[ERROR] Socket: " + exception1.getMessage());
} catch (IOException exception2) {
System.out.println("[ERROR] IO: " + exception2.getMessage());
}
while(true){
try {
int channels = selector.select();
if (channels == 0) {
continue;
}
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while(iterator.hasNext()){
SelectionKey key = iterator.next();
if (!key.isValid()) {
continue;
}
if(key.isAcceptable()){ //aqui creo el canal y el socket de cada cliente
ServerSocketChannel server=(ServerSocketChannel) key.channel();
SocketChannel socketChann=server.accept(); //socket del cliente que se conecta
socketChann.configureBlocking(false);
//System.out.println("There is a new TCP client");
socketChann.register(selector, SelectionKey.OP_READ); //lo registras en el selector con la opción de leer
map.put(socketChann, 0); //lo metes en el mapa con el acumulador a 0
} else if(key.isReadable()){
ByteBuffer bufferTCP = ByteBuffer.allocate(4);
ByteBuffer bufferToSendTCP = ByteBuffer.allocate(4);
ByteBuffer bufferUDP = ByteBuffer.allocate(4);
ByteBuffer buffersendUDP = ByteBuffer.allocate(4);
SocketChannel channelConnection=null;
try{
accumulatorUdp=udpFunction(channelUdp,accumulatorUdp,bufferUDP,buffersendUDP,keyUDP);
}catch(Exception exception4){
tcpFunction(channelConnection,bufferTCP,bufferToSendTCP,map,key);
}
} //end if is readable
iterator.remove();
}
} catch (SocketException exception1) {
System.out.println("[ERROR] Socket: " + exception1.getMessage());
} catch (IOException exception2) {
System.out.println("[ERROR] IO: " + exception2.getMessage());
} catch(Exception exception3){
System.out.println("[ERROR] Exception: " + exception3.getMessage());
}
}//end while
} //end else
} //end main
public static int udpFunction(DatagramChannel channelUdp,int accumulatorUdp,ByteBuffer bufferUDP,ByteBuffer buffersendUDP,SelectionKey keyUDP) throws IOException{
DatagramChannel channelForUdp = (DatagramChannel) keyUDP.channel();
SocketAddress clientForUdp = channelForUdp.receive(bufferUDP);
bufferUDP.flip();
int numberUdp = bufferUDP.getInt();
accumulatorUdp = accumulatorUdp+numberUdp;
System.out.println("Value of the accumulator UDP: "+accumulatorUdp);
buffersendUDP.putInt(accumulatorUdp);
buffersendUDP.flip();
channelUdp.send(buffersendUDP, clientForUdp);
return accumulatorUdp;
}
public static void tcpFunction(SocketChannel channelConnection, ByteBuffer bufferTCP,ByteBuffer bufferToSendTCP,Map<SocketChannel, Integer> map,SelectionKey key) throws IOException{
try{
channelConnection = (SocketChannel) key.channel();
int numRead = channelConnection.read(bufferTCP);
bufferTCP.flip();
int numberReceived = bufferTCP.getInt();
int accumulatorTcp=map.get(channelConnection) + numberReceived;
map.put(channelConnection,accumulatorTcp); //actualizar en el mapa el valor del acumulador
System.out.println("Value of the accumulator TCP: "+ accumulatorTcp);
//to send the accumulator
bufferToSendTCP.clear();
bufferToSendTCP.putInt(accumulatorTcp);
bufferToSendTCP.flip();
channelConnection.write(bufferToSendTCP);
}catch(Exception exception3){
map.remove(channelConnection);
channelConnection.close();
}
}
}//end class