How to create a UDP server [Raiza] - Java - Programmation
Marsh Posté le 10-11-2016 à 10:16:47
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
public class UDPPollingServer {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
byte [] tab = new byte[1000];
int [] count = {0,0,0,0,0};
final int TRUMP = 0;
final int CLINTON = 1;
final int JOHNSON = 2;
final int STEIN = 3;
final int BLANK = 4;
DatagramSocket ds = new DatagramSocket(2016);
DatagramPacket dp = new DatagramPacket(tab,tab.length);
while(true){
dp.setData(tab);
dp.setLength(tab.length);
//Reception
ds.receive(dp);
String candidat = new String(tab,0,dp.getLength(),"UTF-8" );
if(candidat.compareTo("trump\n" )==0){
count[TRUMP]++;
}
else if(candidat.compareTo("clinton\n" )==0){
count[CLINTON]++;
}
else if(candidat.compareTo("johnson\n" )==0){
count[JOHNSON]++;
}
else if(candidat.compareTo("stein\n" )==0){
count[STEIN]++;
}
else{
count[BLANK]++;
}
System.out.println("Vote reçu : "+candidat);
System.out.println("Recapitulatif :" );
for(int i = 0; i<5;i++){
System.out.println("Candidat "+i+" : "+count[i]);
}
}
}
}
Marsh Posté le 10-11-2016 à 10:40:14
package udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
public class UDPPollingServer {
public static void main(String[] args) throws NumberFormatException, IOException {
byte[] buf = new byte[2048];
DatagramSocket s = new DatagramSocket(Integer.parseInt(args[0]));
s.setSoTimeout(60000);
DatagramPacket p = new DatagramPacket(buf, buf.length);
int trump = 0, clinton =0, johnson = 0, stein = 0, blank=0;
List<String> candidats = new ArrayList<String>();
while(true){
s.receive(p);
InetAddress ia = p.getAddress();
String name = new String(p.getData(), p.getLength(), p.getOffset(), "UTF-8" );
// si on lit en ASCII le programme de vote n'est pas portable et selon les machines
// des votes pourraient être mal lues et donc comptés blanc
if(name.equals("quit" )){ //arrêt du serveur
break;
}
if(name.equals("clinton" )){
clinton++;
p.setData(Integer.toBinaryString(clinton).getBytes());
}
else if(name.equals("trump" )){
trump++;
p.setData(Integer.toBinaryString(trump).getBytes());
}
else if(name.equals("johnson" )){
johnson++;
p.setData(Integer.toBinaryString(johnson).getBytes());
}
else if(name.equals("stein" )){
stein++;
p.setData(Integer.toBinaryString(stein).getBytes());
}
else{
blank++;
p.setData(Integer.toBinaryString(blank).getBytes());
}
s.send(p);
System.out.println("Trump = "+trump);
System.out.println("Clinton = "+clinton);
System.out.println("Johnson = "+johnson);
System.out.println("Stein = "+stein);
System.out.println("Blanc = "+blank);
s.close();
}
}
}
Marsh Posté le 10-11-2016 à 10:43:01
Reply
Marsh Posté le 09-11-2016 à 20:07:22
Hello, I would like to creat a UDP server call Raiza.
And I try it to do it my self but it doesn't work on Java Raiza.
Could you please help me ?
This is my Raiza Code :
public class RaizaFileCopier
{
public static final int BUFF_LEN = 1024;
public static void transfer(InputStream is, OutputStream os) throws IOException
{
byte[] data = new byte[BUFF_LEN];
for (int i = is.read(data); i >= 0; i = is.read(data))
os.write(data, 0, i);
}
public static void copyFile(String source, String destination) throws IOException
{
try (
InputStream is =
new BufferedInputStream(new FileInputStream(source));
OutputStream os =
new BufferedOutputStream(new FileOutputStream(destination)) )
{
transfer(is, os);
}
}
public static void main(String[] args) throws IOException
{
if (args.length < 2)
{
System.err.println("Usage: java FileCopier source destination" );
System.exit(-1);
}
copyFile(args[0], args[1]);
}
}