#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 if (bind(serverId,(sockaddr*) &server,sizeof(server)) == ERROR) exit(1); // Start to listen for connections if (listen(serverId,NUMBER_OF_CONNECTIONS) == ERROR) exit(1); // Accept connection clientId = accept(serverId,(sockaddr*) &server,&clientLen); if (clientId == ERROR) exit(1); for (int counter=0;counter<10000;counter++) // Looping construct for 10,000 times { nread=read(clientId,&aPacket,sizeof(struct packet)); // Read the data in nread=write(clientId,&aPacket,sizeof(struct packet)); // Write the data out } shutdown(serverId,2); // Do a full shutdown of the socket // upon completion }