Here's the assignment:
You are to create a single server (in either Java or C/C++) which can service both C/C++ and Java clients.

The protocol of the server is as follows:

1.  The client contacts the server.
2.  The client performs a single send of a data packet containing the following items (in the order specified):
a.  a 32-bit signed integer (int)
b.  a 64-bit floating point value (double)
c.  another 64-bit floating point value (double)
d.  another 32-bit signed integer (int)
e.  an array of 80 bytes representing a C char array 
(i.e. a C “string” – NOT a Java String or a C++ string, just
a character array with a null character (character 0) value to mark the end of the string)
3.  The server responds with a single data packet (a single send) containing:
a.  a 64-bit floating point value (double) 
the value of which should be a * cos(b) + sin(c) * d, where
a,b,c, and d are the values sent in the client packet above
b.  an array of 80 bytes representing a C char array
           the value of which should be the bytewise reversal of e in the
           client message.
           For example, if the client sends:
            “Hello World!”  (where ) represents the null character,
           the server should reply with:
            “!dlroW olleH”
           Note that the remainder of the byte array (after the first zero 
            byte) is to be ignored in both the client and the server messages.  (However, both must send a complete 80 characters.)
4.  The client closes the connection.

It is EXTREMELY important to stress that BOTH the client AND the server perform ONLY ONE send each. (On the other hand, you may do as many receives as you need.) You must pack your data structure such that all the data is sent in a single packet. If you are trying to send C Structs, note that the C compiler may add padding to your data to space the data appropriately (for example, so that the b value starts at an even 64-bit boundary it may insert 32-bits of blank space after a. Beware of the differences this causes between C/C++ and Java.

Your clients should be written to run on CS1, and your server should run on Playground. NOTE that CS1 is a big-endian system, so no modification is necessary to send items in network byte order as the standard internet byte order IS big endian. However, Playground is a little endian system, so you must use the standard htons( ), htonl( ), ntohs( ), ntohl( ), etc. functions. However, there is NO htond( ) (i.e. no pre-existing function to reverse the byte order of a 64-bit entity. Therefore, you will need to write your own htond( ) function. (Once you accomplish this, the string reversal function should be simple – that is, they are the same idea, only the htond( ) function always reverses only 8 bytes.)

The .c, .h, and .java files for the server and the two clients should be placed in a single zip archive and submitted via the Sakai dropbox. Please do not include any compiled code (.class, .o, or .exe) as these just take up space (i.e. I will be recompiling your code anyway).

This assignment is due by Midnight on Wed, May 12.

I have written a java server and client but it doesn't seem to be working.
Any help appreciated.

Server.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project;

/**
 *
 * Course: CS 330(Networking)
 * Language: Java
 * Date: May 2010
 *
 */

import java.net.*;
import java.io.*;
import java.lang.*;

public class Server {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        //Declaration
        ServerSocket myServerSocket= new ServerSocket(50697);
        Socket myConnSocket = myServerSocket.accept();
        OutputStream writer = myConnSocket.getOutputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(myConnSocket.getInputStream()));

        String in_payload = reader.readLine();
        byte data[] = in_payload.getBytes();

        int myImportedInt1 = (int)data[0];
        double myImportedDouble1 = (double)data[1];
        double myImportedDouble2 = (double)data[2];
        int myImportedInt2 = (int)data[3];
        byte myImportedCString[] = (new String(data,4,data.length-4))).getBytes();

        double myDoubleExport = myImportedInt1*Math.acos(myImportedDouble1)+Math.asin(myImportedDouble2)*myImportedInt2;
        byte myCStringExport[] = new byte[80];
        int i=0, j=79;
        while(j>=0)
        {
            myCStringExport[i]=myImportedCString[j];
            i++;
            j--;

        }

        byte out_payload[] = new byte[81];
        out_payload[0] = (new Double(myDoubleExport)).byteValue();

        for(int pos=1; pos<out_payload.length; pos++)
        {
             out_payload[pos]=myCStringExport[pos-1];
        }
        reader.close();
        myServerSocket.close();
    }

}

Client.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project;

/**
 *
 * Course: CS330(Networking)
 * Language: Java
 * Date: May 2010
 */

import java.io.*;
import java.net.*;
import java.lang.String;


public class Client
{
    public static void main(String args[]) throws Exception
    {
        String addr= "127.0.0.1";
        int port = 50697;
        Socket mySocket = new Socket(addr,port);
        OutputStream writer = mySocket.getOutputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));

        byte myInt1 = new Integer(1).byteValue();
        byte myInt2 = new Integer(10).byteValue();
        byte myDouble1 = new Double(1.5).byteValue();
        byte myDouble2 = new Double(5.4321).byteValue();
        String message = new String("makeitonebiggertodayismondayijustmadeitupwearescrewedironmanisgoingtobeamazing!");

        byte myArray[] = message.getBytes();

        byte payload[] = new byte[1+1+1+1+myArray.length];
        payload[0]=myInt1;
        payload[1]=myDouble1;
        payload[2]=myDouble2;
        payload[3]=myInt2;

        for(int i=4; i<payload.length; i++)
        {
            payload[i]=myArray[i-4];
        }
        writer.write(payload);
        writer.close();
        reader.close();
        mySocket.close();
        }
    }
}

Thanks Again

Recommended Answers

All 5 Replies

what errors did you get i would check but havent got eclipse on my pc

Well there is two types of errors:
At compile time the Client class has a surplus accolode at the end of the class
At runtime the server class throw an index out of bound exception due to this statement:

int myImportedInt2 = (int) data[3];

Hope it helps.

Sorry:
Another compile erro in the server class in this statement:

byte myImportedCString[] = (new String(data, 4, data.length - 4))[B]/*)*/[/B].getBytes();

Here I commented the suerplus parhentése

Some notes about the Client class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * Course: CS330(Networking)
 * Language: Java
 * Date: May 2010
 */
/**
 *
 * Course: CS330(Networking)
 * Language: Java
 * Date: May 2010
 */
import java.io.*;
import java.net.*;
import java.lang.String;

public class Client {

    public static void main(String args[]) throws Exception {
        String addr = "localhost";
        int port = 50697;
        Socket mySocket = new Socket(addr, port);
        OutputStream writer = mySocket.getOutputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
        [COLOR="Green"][B]//Ok as long as 1 is between Byte.MAXVALUE and Byte.MINVALUE
        //Loose  information otherwise[/B][/COLOR]
        byte myInt1 = new Integer(1).byteValue();
      [COLOR="Green"][B]  //Ok as long as 10 is between Byte.MAXVALUE and Byte.MINVALUE
        //Loose information otherwise[/B][/COLOR]
        byte myInt2 = new Integer(10).byteValue();
       [COLOR="Green"][B] //Here the myDouble1 will hold 1 not 1.5 due to the rounding
        //Loose information  due to the rounding and/or truncation[/B][/COLOR]
        byte myDouble1 = new Double(1.5).byteValue();
 [COLOR="Green"][B]       //Here the myDouble2 will hold 5 not 5.4321 due to the rounding[/B][/COLOR]
        byte myDouble2 = new Double(5.4321).byteValue();

        String message = new String("makeitonebiggertodayismondayijustmadeitupwearescrewedironmanisgoingtobeamazing!\0");

        byte myArray[] = message.getBytes();
        //Here payload with losted data
        byte payload[] = new byte[1 + 1 + 1 + 1 + myArray.length];
        payload[0] = myInt1;
        payload[1] = myDouble1;
        payload[2] = myDouble2;
        payload[3] = myInt2;
      [B][COLOR="Green"]  //you can use arraycopy instead if the loop is not required[/COLOR][/B]
        System.arraycopy(myArray, 0, payload, 4, myArray.length);
        /*for (int i = 4; i < payload.length; i++) {
        payload[i] = myArray[i - 4];
        }*/
        writer.write(payload);
        writer.close();
        reader.close();
        mySocket.close();
    }
}
/*}*/

To be continued...

Hi again.

a. a 32-bit signed integer (int)
b. a 64-bit floating point value (double)
c. another 64-bit floating point value (double)
d. another 32-bit signed integer (int)
e. an array of 80 bytes representing a C char array

the size in byte of the "payload" array whould be : 4 + 8 + 8 + 4 + myArray.length; instead of 1 + 1 + 1 + 1 + myArray.length;

the next step is to "convert" the int to an array of byte not only calling the getByteValue on it.
so goes the doubles.

Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.