Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1222 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  [Raiza] How to create a UDP server

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Raiza] How to create a UDP server

n°2291490
raizo
Posté le 09-11-2016 à 20:07:22  profilanswer
 

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]);
 }
}
 

mood
Publicité
Posté le 09-11-2016 à 20:07:22  profilanswer
 

n°2291491
sofso95
Posté le 09-11-2016 à 20:08:53  profilanswer
 

wait for while, i answer you after

n°2291505
sofso95
Posté le 10-11-2016 à 10:16:47  profilanswer
 

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]);
   }
  }
 
 }
}

n°2291507
sofso95
Posté le 10-11-2016 à 10:40:14  profilanswer
 

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();
  }
 
 }
 
}

n°2291508
TotalRecal​l
Posté le 10-11-2016 à 10:43:01  profilanswer
 

Les balises code ça vous parle ?


---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
n°2291516
sofso95
Posté le 10-11-2016 à 13:23:48  profilanswer
 

sorry i understand only english


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  [Raiza] How to create a UDP server

 

Sujets relatifs
Sql server un champ avec 3 choixDupral erreur internal server error 500 après installation?
asp.net et sql server[SQL Server / Cobol GCOS] Insert Varchar sans espace
[SQL Server] clé étrangère et Check constraint - NULLDelphi 7 et Sql server 2008
Connexion à sql server 2012 qui sur vmwareasp.net MVC5/ server IIS
SGBD à moyenne volumétrie : MySQL => SQL Server ?easy php - create table on delete cascade
Plus de sujets relatifs à : [Raiza] How to create a UDP server


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR