0% found this document useful (0 votes)
7 views3 pages

Client Server

The document contains Java code for a client-server application where the client (DeserClient) connects to a server (DeserServer) to send a string for hashing. The client initiates a connection, verifies protocol compatibility, and sends a string to be hashed, while the server listens for connections, responds with protocol information, and generates a hash using MD5. Both classes handle communication through sockets and object streams, ensuring proper protocol adherence and error handling.

Uploaded by

Giang Hương
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Client Server

The document contains Java code for a client-server application where the client (DeserClient) connects to a server (DeserServer) to send a string for hashing. The client initiates a connection, verifies protocol compatibility, and sends a string to be hashed, while the server listens for connections, responds with protocol information, and generates a hash using MD5. Both classes handle communication through sockets and object streams, ensuring proper protocol adherence and error handling.

Uploaded by

Giang Hương
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package nb.

deser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class DeserClient {


public static final int PROTOCOL_HELLO = -268387670;

public static final short PROTOCOL_VERSION = 257;

public void run(String serverAddress, int serverPort) throws Exception {


InetAddress serverAddr = InetAddress.getByName(serverAddress);
System.out.println("[+] DeserClient started, connecting to " +
serverAddr.getHostAddress() + ":" + serverPort);
Socket sock = new Socket(serverAddr, serverPort);
sock.setSoTimeout(250);
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("[+] Connected, reading server hello packet...");
if (ois.readInt() != -268387670) {
System.out.println("[-] Server did not send a valid hello, killing
connection...");
sock.close();
return;
}
System.out.println("[+] Hello received, sending hello to server...");
oos.writeInt(-268387670);
oos.flush();
System.out.println("[+] Hello sent, reading server protocol version...");
if (ois.readShort() > 257) {
System.out.println("[-] Server protocol version is greater than client
protocol version, killing connection...");
sock.close();
return;
}
System.out.println("[+] Sending supported protocol version to the server...");
oos.writeShort(257);
oos.flush();
System.out.println("[+] Enter a client name to send to the server: ");
oos.writeUTF(br.readLine());
oos.flush();
System.out.println("[+] Enter a string to hash: ");
HashRequest hr = new HashRequest(br.readLine());
System.out.println("[+] Generating hash of \"" + hr.getData() + "\"...");
oos.writeObject(hr);
oos.flush();
hr = (HashRequest)ois.readObject();
System.out.println("[+] Hash generated: " + hr.getHash());
}
}
sẻver
package nb.deser;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;

public class DeserServer {


public static final int PROTOCOL_HELLO = -268387670;

public static final short PROTOCOL_VERSION = 257;

public void run(String listenAddress, int listenPort) throws Exception {


ObjectOutputStream oos = null;
Socket clientSock = null;
InetAddress listenAddr = InetAddress.getByName(listenAddress);
ServerSocket serverSock = new ServerSocket(listenPort, 0, listenAddr);
serverSock.setSoTimeout(250);
System.out.println("[+] DeserServer started, listening on " +
serverSock.getInetAddress().getHostAddress() + ":" + serverSock.getLocalPort());
while (true) {
try {
clientSock = serverSock.accept();
System.out.println("[+] Connection accepted from " +
clientSock.getInetAddress().getHostAddress() + ":" + clientSock.getPort());
oos = new ObjectOutputStream(clientSock.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(clientSock.getInputStream());
System.out.println("[+] Sending hello...");
oos.writeInt(-268387670);
oos.flush();
System.out.println("[+] Hello sent, waiting for hello from client...");
if (ois.readInt() != -268387670) {
System.out.println("[-] Client did not send a hello back, killing
connection...");
clientSock.close();
continue;
}
System.out.println("[+] Hello received from client...");
System.out.println("[+] Sending protocol version...");
oos.writeShort(257);
oos.flush();
System.out.println("[+] Version sent, waiting for version from client...");
if (ois.readShort() > 257) {
System.out.println("[-] Client protocol version is greater than server
protocol version, killing connection...");
clientSock.close();
continue;
}
System.out.println("[+] Client version is compatible, reading client
name...");
String clientName = ois.readUTF();
System.out.println("[+] Client name received: " + clientName);
HashRequest request = (HashRequest)ois.readObject();
System.out.println("[+] Hash request received, hashing: " +
request.getData());
request.setHash(generateHash(request.getData()));
System.out.println("[+] Hash generated: " + request.getHash());
oos.writeObject(request);
oos.flush();
System.out.println("[+] Done, terminating connection.");
clientSock.close();
} catch (Exception e) {
try {
if (clientSock != null && clientSock.isConnected() == true)
oos.writeObject(e);
} catch (Exception exception) {}
}
}
}

private String generateHash(String data) throws Exception {


MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(data.getBytes());
return String.format("%032x", new Object[] { new BigInteger(1,
md5.digest()) });
}
}

You might also like