Boa noite galera,
estou com um trabalho da faculdade que tenho que me basear em um projeto que o professor enviou.
O projeto pelo que entendi, cria uma conexão UDP entre um client e um server.
No server ele starta o server e fica aguardando uma mensagem do tipo string.
E no client ele envia essa mensagem do tipo string.
Mas quando troco o tipo da variavel, ele da erro e já fucei tudo e não consegui entender o que está errado.
Alguém pode me dar um help?
Segue os códigos:
CLIENT
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package udpclient;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UDPClient {
/**
* @param args the command line arguments
* @throws java.net.SocketException
* @throws java.net.UnknownHostException
*/
public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
DatagramSocket aSocket = new DatagramSocket();
String mensagem = "7";
byte[] m = mensagem.getBytes();
InetAddress aHost = InetAddress.getByName("127.0.0.1");
int serverPort = 7000;
DatagramPacket request = new DatagramPacket(m, mensagem.length(), aHost, serverPort);
aSocket.send(request);
System.out.println("[Cliente] Informação enviada");
}
}
SERVER
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package udpserver;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDPServer {
/**
* @param args the command line arguments
* @throws java.net.SocketException
*/
public static void main(String[] args) throws SocketException, IOException {
DatagramSocket aSocket = new DatagramSocket(7000);
byte[] buffer = new byte[1000];
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
System.out.println("[Servidor] Pronto");
aSocket.receive(request);
System.out.println("[Servidor] Informação recebida");
String s = new String(request.getData());
System.out.println(s.substring(0, request.getLength()));
}
}
