473,378 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

pingclient

hi,
i have a ping server and i need to change de code to make a ping cliente to interact with the ping server, to print the time that the ping takes, to send also a ping per sec, like a normal ping, and all of this with UDP, and i need to create something like TCP to know is all the packages arrive ok. this is the code for de ping server. can i have some help? thanks

import java.io.*;
import java.net.*;
import java.util.*;
/* Servidor para processar pedidos ping sobre UDP */
public class PingServer
{
private static final double LOSS_RATE = 0.3;
private static final int AVERAGE_DELAY = 100; // milissegundos
public static void main(String[] args) throws Exception
{
// Obter o argumento da linha de commando.
if (args.length != 1) {
System.out.println("argumentos exigidos: porta");
return;
}
int port = Integer.parseInt(args[0]);
// Criar um gerador de números aleatórios para ser usado na
// simulação de perda de pacotes e do atraso na rede.
Random random = new Random();
// Criar um socket datagrama para receber e enviar pacotes UDP
// através da porta especificada na linha de comando.
DatagramSocket socket = new DatagramSocket(port);
// Ciclo do processo.
while (true) {
Redes de Computadores I – 2007/08 – Trabalho Prático Prog2 2/5
// Criar um pacote datagrama para armazenar o pacote UDP recebido.
DatagramPacket request = new DatagramPacket(new byte[1024], 1024);
// Bloquear até que a máquina receba um pacote UDP.
socket.receive(request);
// Imprimir os dados recebidos.
printData(request);
// Decidir se vai responder ou se vai simular a perda do pacote.
if (random.nextDouble() < LOSS_RATE) {
System.out.println(" Resposta nao enviada.");
continue;
}
// Simular o atraso na rede.
Thread.sleep((int) (random.nextDouble() * 2 * AVERAGE_DELAY));
// Enviar resposta.
InetAddress clientHost = request.getAddress();
int clientPort = request.getPort();
byte[] buf = request.getData();
DatagramPacket reply =
new DatagramPacket(buf, buf.length, clientHost, clientPort);
socket.send(reply);
System.out.println(" Resposta enviada.");
}
}
/* Imprimir os dados do ping na stream de saida standard. */
private static void printData(DatagramPacket request) throws Exception
{
// Obter referencias ao array de bytes do pacote.
byte[] buf = request.getData();
// Colocar os bytes num stream de entrada byte array
// por forma a ler os dados como um stream de bytes.
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
// Colocar o stream de saida byte array num leitor de stream de
// entrada para poder ler os dados como um stream de caracteres.
InputStreamReader isr = new InputStreamReader(bais);
// Colocar o leitor do stream de entrada num leitor com buffer
// para poder ler os caracteres uma linha de cada vez.
// (Uma linha é uma sequência de chars terminada por qualquer
// combinação de \r e \n.)
BufferedReader br = new BufferedReader(isr);
// Como os dados da mensagem estão contidos numa única linha,
// ler essa linha.
String line = br.readLine();
// Imprimir o endereço da máquina e os dados dela recebidos.
System.out.println(
"Recebido de " +
request.getAddress().getHostAddress() + ": " + new String(line) );
Redes de Computadores I – 2007/08 – Trabalho Prático Prog2 3/5
}
}
Mar 31 '08 #1
1 2182
pronerd
392 Expert 256MB
can i have some help?
Help with what? What specifically is your question?
Apr 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tim Shih | last post by:
Hi, I am implementing a server/client application using remoting. The server communicates with the client by having the client pass an object over to the server when it logs in. The object...
5
by: Gianmaria I. | last post by:
Hi, having a BitArray, how can i extract bits to create a System.byte as in the example... With BitArray bits and Byte myNewByte
2
by: Devin | last post by:
I need a way to ping an IP address from an ASP.NET page to verify that it is not in use. This is one example given to me earlier, but unfortunetly I do not know C#. Could someone translate this into...
2
by: jwray | last post by:
Hello All, I am migrating some scripts to .NET. This has been a learning experience to say the least. The whole script pull a list of computers through an LDAP call to AD. It then writes the...
5
by: Deepak | last post by:
I am programing a ping application which pings various centers . I used timer loop and it pings one by one. Now when i finish pinging one center it should wait for the ping_completed function to...
0
by: lilbit02 | last post by:
Hello, I need help getting this accomplished. I think I'm close but I get confused because I'm a novice at java. I'm trying to implement a ping client that accepts the hostname and the port. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.