#include #include #include #include #include #include #include #include #include #define ERROR -1 // Signifies an error has occured #define COMM_DOMAIN AF_INET // Sets up the communication domain #define PORT_NUMBER 11611294 // Sets up the port number to be used #define IP_ADDRESS "130.194.166.9" // Machine to bind to (ie. ant.sd) #define PROTOCOL 0 // Use the default Protocol #define NUMBER_OF_CONNECTIONS 25 // Maximum number of concurrent // connections struct packet // Data structure which is // transmitted between the server // and client { short returnShort; // Equivalent to CORBA::Short long returnLong; // Equivalent to CORBA::Long unsigned short returnUnsignedShort; // Equivalent to CORBA::UShort unsigned long returnUnsignedLong; // Equivalent to CORBA::ULong float returnFloat; // Equivalent to CORBA::Float double returnDouble; // Equivalent to CORBA::Double char returnCharacter; // Equivalent to CORBA::Char char returnBoolean; // Equivalent to CORBA::Boolean char returnString[70]; // Equivalent to CORBA::String float bFloat; // Equivalent to CORBA::Float double bDouble; // Equivalent to CORBA::Double short bShort; // Equivalent to CORBA::Short long bLong; // Equivalent to CORBA::bLong }; main() { int serverId,clientId,clientLen=0; // Declare the file descriptor // handlers for the server // connection and client // connection int nread=0; // Declare a variable which is used // to record how many bytes were // sent and how many are received struct packet aPacket; // Declare the data structure for // transmission struct sockaddr_in server; // Declare a socket structure for // the server struct sockaddr_in client; // Declare a socket structure for // the client server.sin_family = COMM_DOMAIN; // Inform the socket structure of // the communication domain server.sin_port = htons(PORT_NUMBER); // Inform the socket structure of // the port number to be used // expressed in network byte order server.sin_addr.s_addr = inet_addr (IP_ADDRESS); // Set the IP address // Try to build a socket serverId = socket(COMM_DOMAIN,SOCK_STREAM,PROTOCOL); if (serverId == ERROR) exit(1); // Error on building the socket // Try and bind the socket to the // system // Try to connect to the server if (connect(serverId,(struct sockaddr *)&server,sizeof(server)) == ERROR) exit(1); // Error on building the socket aPacket.returnShort = 0; // Load Short Data aPacket.returnLong = -2100000000; // Load Long Data aPacket.bLong = -2100000000; // Load Long Data aPacket.returnUnsignedLong = 4200000000; // Load Unsigned Long Data aPacket.returnUnsignedShort = 65535; // Load Unsigned Short Data aPacket.returnFloat = -65545.57; // Load Float Data aPacket.bFloat = -65545.57; // Load Float Data aPacket.returnDouble = 6000; // Load Double Data aPacket.bDouble = 6000; // Load Double Data aPacket.returnCharacter = 'M'; // Load Character Data aPacket.returnBoolean = 1; // Load Boolean Data strcpy(aPacket.returnString, "Data Transfer Test for Communications Data........."); // Happy Birthday"); // Load String Data for(int counter=0;counter<10000;counter++) // Looping construct for 10,000 times { aPacket.returnShort = counter+1; // Save Iteration as part of the // data sent to the object aPacket.bShort = counter+1; nread=write(serverId,&aPacket,sizeof(aPacket)); // Write the Data out nread=read(serverId,&aPacket,sizeof(aPacket)); // Read the Data in cout << "Reconstructed short: " << aPacket.returnShort << endl; cout << "Reconstructed long: " << aPacket.returnLong << endl; cout << "Reconstructed unsigned short: " << aPacket.returnUnsignedShort << endl; cout << "Reconstructed unsigned long: " << aPacket.returnUnsignedLong << endl; cout << "Reconstructed float: " << aPacket.returnFloat << endl; cout << "Reconstructed double: " << aPacket.returnDouble << endl; cout << "Reconstructed character: " << aPacket.returnCharacter << endl; cout << "Reconstructed boolean: " << (int) aPacket.returnBoolean << endl; cout << "Reconstructed string: " << aPacket.returnString << endl; cout << "Reconstructed short: " << aPacket.bShort << endl; cout << "Reconstructed long: " << aPacket.bLong << endl; cout << "Reconstructed float: " << aPacket.bFloat << endl; cout << "Reconstructed double: " << aPacket.bDouble << endl; cout << endl; } shutdown(serverId,2); // Do a full shutdown of // the socket upon // completion return 0; }