Reputation: 1254
I've written three rmi servers which send data to each other using UDP unicast. Every time a method is executed on a particular instance of a server the initiated server sends requests to the other servers which respond with the result of the method executed on that particular instance of the server. I've implemented them using threads. The method executes the first time but when i execute it again I get the following error:
java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.net.SocketException: Connection reset
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.getNonReturners(Unknown Source)
at drms.org.client.DRMSClient.main(DRMSClient.java:379)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readByte(Unknown Source)
... 6 more
getNonReturners-Method where the threads are created for udp communication
requestData: Send request message to the other servers
responseData: Send response message to the server which requests it
Server side code:
public class DRMSServer implements DRMSInterface, Serializable {
final static int _concordiaPortNumber = 1098;
final static int _mcgillPortNumber = 222;
final static int _dawsonPortNumber = 223;
/**
* Description: Sends UDP request from invoked Server to the other servers
*
* @param: Port Number
* @param: Data
*/
public String requestData(final int portNumber, final String data) {
StringBuilder sb = new StringBuilder();
try {
final DatagramSocket clientSocket = new DatagramSocket();
final byte[] receiveData = new byte[1024000];
final DatagramPacket receivePacket = new DatagramPacket(
receiveData, receiveData.length);
byte[] sendData = new byte[1024000];
sendData = data.getBytes();
System.out.print("Ready to send data ");
sb.append("Ready to send data ");
sb.append("\n");
final DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, portNumber);
clientSocket.send(sendPacket);
clientSocket.setSoTimeout(10000);
try {
clientSocket.receive(receivePacket);
final String _result = new String(receivePacket.getData());
final InetAddress returnIPAddress = receivePacket.getAddress();
final int port = receivePacket.getPort();
System.out.println("From server at: " + returnIPAddress + ":"
+ port);
sb.append("From server at: " + returnIPAddress + ":" + port);
sb.append("\n");
System.out.println("Message: " + _result);
sb.append("Message: " + _result);
}
catch (final SocketTimeoutException ste) {
System.out.println("Timeout Occurred: Packet assumed lost");
}
// clientSocket.close();
}
catch (final SocketException e) {
e.printStackTrace();
}
catch (final IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* Description: Returns the administrator with the list of defaulter who
* have a book past their loan date. Based on the UDP request from the
* invoked server it sends the UDP response.
*
* @param: Port Number
*/
public String responseData(final int portNumber) throws NotBoundException {
String _result = null;
try {
@SuppressWarnings("resource")
final DatagramSocket serverSocket = new DatagramSocket(portNumber,
IPAddress);
final DRMSInterface _concordiaServer = (DRMSInterface) Naming
.lookup("rmi://localhost:1098/concordia");
final DRMSInterface _mcgillServer = (DRMSInterface) Naming
.lookup("rmi://localhost:222/mcgill");
final DRMSInterface _dawsonServer = (DRMSInterface) Naming
.lookup("rmi://localhost:223/dawson");
byte[] receiveData = new byte[1024000];
byte[] sendData = new byte[1024000];
String regex = "(?<=[\\w&&\\D])(?=\\d)";
while (true) {
receiveData = new byte[1024000];
final DatagramPacket receivePacket = new DatagramPacket(
receiveData, receiveData.length);
System.out.println("Waiting for datagram packet");
serverSocket.receive(receivePacket);
final String _request = new String(receivePacket.getData());
System.out.println(_request);
String array[] = _request.split(regex);
String _educationalInstitution = array[0];
System.out.println(_educationalInstitution);
int numDays = Integer.parseInt(array[1].trim());
System.out.println(numDays);
final InetAddress IPAddress = receivePacket.getAddress();
final int port = receivePacket.getPort();
System.out.println("From: " + IPAddress + ":" + port);
System.out.println("Request Message from: "
+ _request.toUpperCase());
if (_educationalInstitution.toLowerCase().equals("concordia")) {
if (portNumber == _mcgillPortNumber) {
_result = _mcgillServer.getNonReturnersData(
_educationalInstitution, numDays);
}
else if (portNumber == _dawsonPortNumber) {
_result = _dawsonServer.getNonReturnersData(
_educationalInstitution, numDays);
}
}
else if (_educationalInstitution.toLowerCase().equals("mcgill")) {
if (portNumber == _concordiaPortNumber) {
_result = _concordiaServer.getNonReturnersData(
_educationalInstitution, numDays);
}
else if (portNumber == _dawsonPortNumber) {
_result = _dawsonServer.getNonReturnersData(
_educationalInstitution, numDays);
}
}
else if (_educationalInstitution.toLowerCase().equals("dawson")) {
if (portNumber == _mcgillPortNumber) {
_result = _mcgillServer.getNonReturnersData(
_educationalInstitution, numDays);
}
else if (portNumber == _dawsonPortNumber) {
_result = _dawsonServer.getNonReturnersData(
_educationalInstitution, numDays);
}
}
// final String capitalizedSentence =
if(_result !=null){
sendData = _result.getBytes();
}
else{
_result="NO DEFAULTERS";
sendData = _result.getBytes();
}
final DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
catch (final SocketException ex) {
System.out.println("UDP Port 9876 is occupied.");
System.exit(1);
}
catch (final IOException e) {
e.printStackTrace();
}
return _result;
}
/**
* Description: Returns the administrator with the list of defaulter who
* have a book past their loan date
*
* @param: Admin username
* @param: Admin password
* @param: Educational Institution
* @param: No of days
*/
public String getNonReturnersData(String educationalInstituion, int numDays) {
//returns data
}
/**
* Description: Using this method the admin invoked server communicates with
* other servers using UDP/IP messages to get their information.Once the
* information is received the admin invoked server sends the result string
* to the admin
*
* @param: Admin username
* @param: Admin password
* @param: Educational Institution
* @param: No of days
*/
@Override
public String getNonReturners(String adminUsername, String adminPassword,
String educationalInstitution, int numDays) throws RemoteException,
Exception {
String _result = null;
String _initiatedServerResult=null;
final ArrayList<String> result = new ArrayList<String>();
final DRMSInterface _concordiaServer = (DRMSInterface) Naming
.lookup("rmi://localhost:1098/concordia");
final DRMSInterface _mcgillServer = (DRMSInterface) Naming
.lookup("rmi://localhost:222/mcgill");
final DRMSInterface _dawsonServer = (DRMSInterface) Naming
.lookup("rmi://localhost:223/dawson");
if (educationalInstitution.toLowerCase().equals("concordia")) {
final Thread t1 = new Thread() {
@Override
public void run() {
try {
result.add(_mcgillServer
.responseData(_mcgillPortNumber));
}
catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
};
t1.setDaemon(true);
t1.start();
final Thread t2 = new Thread() {
@Override
public void run() {
try {
result.add(_dawsonServer
.responseData(_dawsonPortNumber));
}
catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
};
t2.setDaemon(true);
t2.start();
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + _mcgillPortNumber);
result.add("Attemping to connect to " + IPAddress
+ ") via UDP port" + _mcgillPortNumber);
result.add("/n");
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + _dawsonPortNumber);
result.add("Attemping to connect to " + IPAddress
+ ") via UDP port" + _dawsonPortNumber);
result.add("/n");
final String _concordiaRequestMessage = educationalInstitution
+ numDays;
System.out.println("Sending data "
+ _concordiaRequestMessage.length() + " bytes to server.");
result.add("Sending data " + _concordiaRequestMessage.length()
+ " bytes to server.");
result.add(_concordiaServer.requestData(_mcgillPortNumber,
_concordiaRequestMessage));
result.add(_concordiaServer.requestData(_dawsonPortNumber,
_concordiaRequestMessage));
_initiatedServerResult=_concordiaServer.getNonReturnersData(
educationalInstitution, numDays);
if(_initiatedServerResult !=null){
result.add(_initiatedServerResult);
}
else{
result.add("No Defaulters in Concordia");
}
}
else if (educationalInstitution.toLowerCase().equals("mcgill")) {
final Thread t1 = new Thread() {
@Override
public void run() {
try {
result.add(_concordiaServer
.responseData(_concordiaPortNumber));
}
catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
};
t1.setDaemon(true);
t1.start();
final Thread t2 = new Thread() {
@Override
public void run() {
try {
result.add(_dawsonServer
.responseData(_dawsonPortNumber));
}
catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
};
t2.setDaemon(true);
t2.start();
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + _concordiaPortNumber);
result.add("Attemping to connect to " + IPAddress
+ ") via UDP port" + _concordiaPortNumber);
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + _dawsonPortNumber);
result.add("Attemping to connect to " + IPAddress
+ ") via UDP port" + _dawsonPortNumber);
final String _mcgillRequestMessage = educationalInstitution
+ numDays;
System.out.println("Sending data "
+ _mcgillRequestMessage.length() + " bytes to server.");
result.add("Sending data " + _mcgillRequestMessage.length()
+ " bytes to server.");
result.add(_mcgillServer.requestData(_concordiaPortNumber,
_mcgillRequestMessage));
result.add(_mcgillServer.requestData(_dawsonPortNumber,
_mcgillRequestMessage));
_initiatedServerResult=_mcgillServer.getNonReturnersData(
educationalInstitution, numDays);
if(_initiatedServerResult !=null)
{
result.add(_initiatedServerResult);
}
else{
result.add("No Defaulters in Mcgill");
}
}
else if (educationalInstitution.toLowerCase().equals("dawson")) {
final Thread t1 = new Thread() {
@Override
public void run() {
try {
result.add(_concordiaServer
.responseData(_concordiaPortNumber));
}
catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
};
t1.setDaemon(true);
t1.start();
final Thread t2 = new Thread() {
@Override
public void run() {
try {
result.add(_mcgillServer
.responseData(_mcgillPortNumber));
}
catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
};
t2.setDaemon(true);
t2.start();
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + _concordiaPortNumber);
result.add("Attemping to connect to " + IPAddress
+ ") via UDP port" + _concordiaPortNumber);
System.out.println("Attemping to connect to " + IPAddress
+ ") via UDP port" + _mcgillPortNumber);
result.add("Attemping to connect to " + IPAddress
+ ") via UDP port" + _mcgillPortNumber);
final String _dawsonRequestMessage = educationalInstitution
+ numDays;
System.out.println("Sending data "
+ _dawsonRequestMessage.length() + " bytes to server.");
result.add("Sending data " + _dawsonRequestMessage.length()
+ " bytes to server.");
result.add(_dawsonServer.requestData(_concordiaPortNumber,
_dawsonRequestMessage));
result.add(_dawsonServer.requestData(_mcgillPortNumber,
_dawsonRequestMessage));
result.add(_concordiaServer.getNonReturnersData(
educationalInstitution, numDays));
_initiatedServerResult=_dawsonServer.getNonReturnersData(
educationalInstitution, numDays);
if(_initiatedServerResult !=null)
{
result.add(_initiatedServerResult);
}
else{
result.add("No Defaulters in Dawson");
}
}
return _result = result.toString();
}
/**
* Description: Creates a remote Concordia server object
* Did the same for the other two servers
*/
public void exportServerConcordia() throws Exception {
Remote _obj = UnicastRemoteObject.exportObject(this,
_concordiaPortNumber);
Registry _r = LocateRegistry.createRegistry(_concordiaPortNumber);
_r.bind("concordia", _obj);
}
public static void main(String args[]) {
try {
DRMSServer concordia = new DRMSServer();
concordia.exportServerConcordia();
System.out.println("Concordia Server is up and running");
DRMSServer mcgill = new DRMSServer();
mcgill.exportServerMcgill();
System.out.println("Mcgill server is up and running");
DRMSServer dawson = new DRMSServer();
dawson.exportServerDawson();
System.out.println("Dawson server is up and running");
//Tried one solution but it didn't work
try {
while (true){
try{
Thread.currentThread();
Thread.sleep (50000000L);
}catch (InterruptedException e) {}
}
} catch (Exception ex) {
System.err.println("Exiting Frequency Keeper Server");
ex.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 2
Views: 6830