JavaのCRC32ライブラリで計算されたチェックサムに問題があります。チェックサムが正しく計算されてから、JavaでCRC32を使用していない
だから、クライアントはそう....
/**
* Increments current packet message number, selects a
* random destination, and sends the packet.
* @param connection
* @throws InterruptedException
* @throws IOException
*/
private void generateAndSendPackets(Socket connection) throws InterruptedException, IOException
{
byte packet[] = new byte[5];
for (byte messageNumber = 0; messageNumber <= 20; messageNumber++)
{
packet[0] = CLIENT_ID; //SOURCE
packet[1] = randomDestination(); //DESTINATION
packet[3] = messageNumber; //DATA
packet[4] = messageNumber;
//Need more data?
packet[2] = computeCheckSum(packet); //COMPUTE CHECKSUM
send(packet); //SEND PACKET
Thread.sleep(4000); //WAIT TO SEND NEXT PACKET
}
closeConnection(connection); //Closes the connection
}
/**
* Given a packet, it computes the checksum for
* the packet with internal Checksum library
* @param packet
* @return
*/
private byte computeCheckSum(byte[] packet)
{
Checksum checkSum = new CRC32();
checkSum.update(packet, 0, packet.length);
return (byte)checkSum.getValue();
}
のようなチェックサムが次にパケットを送信計算します。チェックサムを含む。
ザ・ルーターは、それが計算して出力する場合ので、それが最初に時刻を修正んので...
/**
* Given a packet, it computes the checksum for
* the packet with internal Checksum library
* @param p
* @return
*/
private boolean checkCheckSum(byte[] p)
{
byte[] packet = p;
byte[] tempPacket = new byte[5];
tempPacket[0] = packet[0];
tempPacket[1] = packet[1];
tempPacket[3] = packet[3];
tempPacket[4] = packet[4];
Checksum checkSum = new CRC32();
checkSum.update(tempPacket, 0, tempPacket.length);
byte cc = (byte)checkSum.getValue();
System.out.println(packet[0] + " " + packet[1] + " " + packet[2] + " " + packet[3] + " " + packet[4]);
tempPacket[2] = (byte)checkSum.getValue();
System.out.println(tempPacket[0] + " " + tempPacket[1] + " " + tempPacket[2] + " " + tempPacket[3] + " " + tempPacket[4]);
if((byte)checkSum.getValue() == packet[2]){
System.out.println(packet[2] + "," + cc);
return true;
}else{
System.out.println(packet[2] + "," + cc);
return false;
}
}
のようなチェックサムを計算します...が、その後、その後、それはちょうどそうのように失敗しました。..
Waiting for connection....
Connection recieved from /127.0.0.1 on port 9000
Waiting for packet...
Packet recieved: 11 44 -118 00
11 44 -118 0 0
11 44 -118 0 0
-118,-118
<Error here because we weren't done another part yet>
Waiting for packet...
Packet recieved: 11 33 -42 11
11 33 -42 1 1
11 33 -128 1 1
-42,-128
Checksum invalid!!!!
Waiting for packet...
次に、各それが無効になった後....
SOそれは、再度チェックサムを取得するために、その後台無し、正しく一度チェックサムを得ています。私はそれが私が間違ってJavaのCRC32ライブラリで何かをしていると思う私の推測です。これらはすべて自分のスレッドでもありますが、それは問題ではないと思います。途中でこれを行っていくつかの部分があるのでここで
は、私たちのコードは以下の通りです、私は、上記の問題の部分を強調してみました....
Client.java
package runner;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
/**
* Client side for our client
* server project. Sends a request
* with a string to reverse.
* @author xxxxx
*
*/
public class Client
{
private static final int PORT = 9000;
private static final String HOST = "localhost";
private static final byte CLIENT_ID = (byte)11;
private Random rand;
//For reciving packets
private ServerSocket clientRecieverSocket = null;
//Output to server
private PrintWriter out;
//Input from server
private Scanner conIn;
//Input from user
private Scanner in;
//Data Output Stream
private DataOutputStream dos;
/**
* Creates a new instance of the client
* @param args
* @throws UnknownHostException
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException
{
Client c = new Client();
c.run();
}
/**
* Runs the client
*
* @throws UnknownHostException
* @throws IOException
* @throws InterruptedException
*/
private void run() throws IOException, InterruptedException
{
String msg; //Recieve messages from server
//Opens connection
Socket connection = openConnection();
//Gets I/O streams from server
System.out.println("Getting output stream...");
out = new PrintWriter(connection.getOutputStream());
System.out.println("Getting input stream...");
conIn = new Scanner(connection.getInputStream());
System.out.println();
dos = new DataOutputStream(connection.getOutputStream());
msg = conIn.nextLine();
System.out.println(msg);
in = new Scanner(System.in);
generateAndSendPackets(connection);
}
/**
* Increments current packet message number, selects a
* random destination, and sends the packet.
* @param connection
* @throws InterruptedException
* @throws IOException
*/
private void generateAndSendPackets(Socket connection) throws InterruptedException, IOException
{
byte packet[] = new byte[5];
for (byte messageNumber = 0; messageNumber <= 20; messageNumber++)
{
packet[0] = CLIENT_ID; //SOURCE
packet[1] = randomDestination(); //DESTINATION
packet[3] = messageNumber; //DATA
packet[4] = messageNumber;
//Need more data?
packet[2] = computeCheckSum(packet); //COMPUTE CHECKSUM
send(packet); //SEND PACKET
Thread.sleep(4000); //WAIT TO SEND NEXT PACKET
}
closeConnection(connection); //Closes the connection
}
/**
* Given a packet, it computes the checksum for
* the packet with internal Checksum library
* @param packet
* @return
*/
private byte computeCheckSum(byte[] packet)
{
Checksum checkSum = new CRC32();
checkSum.update(packet, 0, packet.length);
return (byte)checkSum.getValue();
}
/**
* Select random destination
* to send to.
*/
private byte randomDestination()
{
List<Byte> destinations = Arrays.asList((byte)22,(byte)33,(byte)44);
//destinations.remove(CLIENT_ID); //Do not send it to itself...
rand = new Random();
return destinations.get(rand.nextInt(destinations.size())); //converts string to byte
}
/**
* Open the connection
* @return
* @throws UnknownHostException
* @throws IOException
*/
private Socket openConnection() throws IOException
{
System.out.println("Connecting to server...");
Socket connection = new Socket(HOST, PORT); //Connects to server
System.out.println("Connection made!");
return connection;
}
/**
* Closes the connection
* @param connection
* @throws IOException
*/
private void closeConnection(Socket connection) throws IOException
{
System.out.println("Closing connection...");
connection.close();
}
/**
* Sends a message to the server
* with the string to reverse.
* @param packet
*/
private void send(byte[] packet) throws IOException
{
System.out.println("You sent " + packet + ", sending message...");
System.out.println(packet[0] + " " + packet[1] + " " + packet[2] + " " + packet[3] + " " + packet[4]);
dos.write(packet, 0, packet.length);
dos.flush();
}
}
Router.java
なぜCRC-32を使用して1バイトしか送信しないのですか?これは意味をなさない。 – EJP