% Remember to use the lgrind style \File{sockclnt.cc},{13:50},{Oct 24 1996} \L{\LB{\K{\#include}_\<\V{stdio}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{stdlib}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{unistd}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{string}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{sys}\1\V{types}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{netinet}\1\V{in}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{sys}\1\V{socket}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{arpa}\1\V{inet}.\V{h}\>}} \L{\LB{\K{\#include}_\<\V{iostream}.\V{h}\>}} \L{\LB{}} \L{\LB{}} \L{\LB{\K{\#define}_\V{ERROR}_\-1________________________\C{}\1\1_ Signifies an error has occured}} \CE{}\L{\LB{\K{\#define}_\V{COMM\_DOMAIN}_\V{AF\_INET}_____________\C{}\1\1_ Sets up the communication domain}} \CE{}\L{\LB{\K{\#define}_\V{PORT\_NUMBER}_11611294____________\C{}\1\1_ Sets up the port number to be used}} \CE{}\L{\LB{\K{\#define}_\V{IP\_ADDRESS}_\S{}\"130.194.166.9\"\SE{}______\C{}\1\1_ Machine to bind to (ie. ant.sd)}} \CE{}\L{\LB{\K{\#define}_\V{PROTOCOL}_0______________________\C{}\1\1_ Use the default Protocol}} \CE{}\L{\LB{\K{\#define}_\V{NUMBER\_OF\_CONNECTIONS}_25________\C{}\1\1_ Maximum number of concurrent}} \CE{}\L{\LB{________________________________________\C{}\1\1_ connections}} \CE{}\L{\LB{}} \L{\LB{\K{struct}_\V{packet}___________________________\C{}\1\1_ Data structure which is}} \CE{}\L{\LB{________________________________________\C{}\1\1_ transmitted between the server}} \CE{}\L{\LB{________________________________________\C{}\1\1_ and client}} \CE{}\L{\LB{\{}} \L{\LB{_\K{short}_\V{returnShort};_____________________\C{}\1\1_ Equivalent to CORBA::Short}} \CE{}\L{\LB{_\K{long}_\V{returnLong};_______________________\C{}\1\1_ Equivalent to CORBA::Long}} \CE{}\L{\LB{_\K{unsigned}_\K{short}_\V{returnUnsignedShort};____\C{}\1\1_ Equivalent to CORBA::UShort}} \CE{}\L{\LB{_\K{unsigned}_\K{long}_\V{returnUnsignedLong};______\C{}\1\1_ Equivalent to CORBA::ULong}} \CE{}\L{\LB{_\K{float}_\V{returnFloat};_____________________\C{}\1\1_ Equivalent to CORBA::Float}} \CE{}\L{\LB{_\K{double}_\V{returnDouble};___________________\C{}\1\1_ Equivalent to CORBA::Double}} \CE{}\L{\LB{_\K{char}_\V{returnCharacter};__________________\C{}\1\1_ Equivalent to CORBA::Char}} \CE{}\L{\LB{_\K{char}_\V{returnBoolean};____________________\C{}\1\1_ Equivalent to CORBA::Boolean}} \CE{}\L{\LB{_\K{char}_\V{returnString}[70];_________________\C{}\1\1_ Equivalent to CORBA::String}} \CE{}\L{\LB{_\K{float}_\V{bFloat};}\Tab{40}{\C{}\1\1_ Equivalent to CORBA::Float}} \CE{}\L{\LB{_\K{double}_\V{bDouble};}\Tab{40}{\C{}\1\1_ Equivalent to CORBA::Double}} \CE{}\L{\LB{_\K{short}_\V{bShort};}\Tab{40}{\C{}\1\1_ Equivalent to CORBA::Short}} \CE{}\L{\LB{_\K{long}_\V{bLong};}\Tab{40}{\C{}\1\1_ Equivalent to CORBA::bLong}} \CE{}\L{\LB{\};}} \L{\LB{}} \L{\LB{\Proc{main}\V{main}()}} \L{\LB{\{}} \L{\LB{_\K{int}_\V{serverId},\V{clientId},\V{clientLen}=0;}\Tab{40}{\C{}\1\1_ Declare the file descriptor}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ handlers for the server}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ connection and client}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ connection}} \CE{}\L{\LB{}} \L{\LB{_\K{int}_\V{nread}=0;}\Tab{40}{\C{}\1\1_ Declare a variable which is used}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ to record how many bytes were}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ sent and how many are received}} \CE{}\L{\LB{}} \L{\LB{_\K{struct}_\V{packet}_\V{aPacket};}\Tab{40}{\C{}\1\1_ Declare the data structure for}} \CE{}\L{\LB{}\Tab{32}{__}\Tab{40}{\C{}\1\1_ transmission}} \CE{}\L{\LB{}} \L{\LB{_\K{struct}_\V{sockaddr\_in}_\V{server};}\Tab{40}{\C{}\1\1_ Declare a socket structure for}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ the server}} \CE{}\L{\LB{}} \L{\LB{_\K{struct}_\V{sockaddr\_in}_\V{client};}\Tab{40}{\C{}\1\1_ Declare a socket structure for}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ the client}} \CE{}\L{\LB{}} \L{\LB{_\V{server}.\V{sin\_family}_=_\V{COMM\_DOMAIN};_______\C{}\1\1_ Inform the socket structure of}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ the communication domain}} \CE{}\L{\LB{}} \L{\LB{_\V{server}.\V{sin\_port}_=_\V{htons}(\V{PORT\_NUMBER});__\C{}\1\1_ Inform the socket structure of}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ the port number to be used }} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ expressed in network byte order}} \CE{}\L{\LB{}} \L{\LB{_\V{server}.\V{sin\_addr}.\V{s\_addr}_=_\V{inet\_addr}_(\V{IP\_ADDRESS});_\C{}\1\1_ Set the IP address}} \CE{}\L{\LB{}} \L{\LB{}\Tab{40}{\C{}\1\1_ Try to build a socket}} \CE{}\L{\LB{_\V{serverId}_=_\V{socket}(\V{COMM\_DOMAIN},\V{SOCK\_STREAM},\V{PROTOCOL});}} \L{\LB{_\K{if}_(\V{serverId}_==_\V{ERROR})}} \L{\LB{__\V{exit}(1);}\Tab{40}{\C{}\1\1_ Error on building the socket}} \CE{}\L{\LB{}} \L{\LB{}\Tab{40}{\C{}\1\1_ Try and bind the socket to the}} \CE{}\L{\LB{}\Tab{40}{\C{}\1\1_ system}} \CE{}\L{\LB{}} \L{\LB{}\Tab{40}{\C{}\1\1_ Try to connect to the server}} \CE{}\L{\LB{_\K{if}_(\V{connect}(\V{serverId},(\K{struct}_\V{sockaddr}_\*)\&\V{server},\K{sizeof}(\V{server}))_==_\V{ERROR})}} \L{\LB{__\V{exit}(1);}\Tab{40}{\C{}\1\1_ Error on building the socket}} \CE{}\L{\LB{}} \L{\LB{_\V{aPacket}.\V{returnShort}_=_0;}\Tab{40}{__\C{}\1\1_ Load Short Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnLong}_=_\-2100000000;________\C{}\1\1_ Load Long Data}} \CE{}\L{\LB{_\V{aPacket}.\V{bLong}_=_\-2100000000;________\C{}\1\1_ Load Long Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnUnsignedLong}_=_4200000000;_\C{}\1\1_ Load Unsigned Long Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnUnsignedShort}_=_65535;_____\C{}\1\1_ Load Unsigned Short Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnFloat}_=_\-65545.57;_________\C{}\1\1_ Load Float Data}} \CE{}\L{\LB{_\V{aPacket}.\V{bFloat}_=_\-65545.57;_________\C{}\1\1_ Load Float Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnDouble}_=_6000;_____________\C{}\1\1_ Load Double Data}} \CE{}\L{\LB{_\V{aPacket}.\V{bDouble}_=_6000;_____________\C{}\1\1_ Load Double Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnCharacter}_=_\S{}\'M\'\SE{};___}\Tab{40}{__\C{}\1\1_ Load Character Data}} \CE{}\L{\LB{_\V{aPacket}.\V{returnBoolean}_=_1;_}\Tab{40}{__\C{}\1\1_ Load Boolean Data}} \CE{}\L{\LB{_\V{strcpy}(\V{aPacket}.\V{returnString},_\S{}\"Data_Transfer_Test_for_Communications_Data.\,.\,.\,.\,.\,.\,.\,.\,.\"\SE{});}} \L{\LB{}} \L{\LB{\C{}\1\1_ Happy Birthday\"); \1\1 Load String Data}} \CE{}\L{\LB{_}} \L{\LB{_\K{for}(\K{int}_\V{counter}=0;\V{counter}\<10000;\V{counter}++)}} \L{\LB{}\Tab{40}{__\C{}\1\1_ Looping construct for 10,000 times}} \CE{}\L{\LB{_\{}} \L{\LB{__\V{aPacket}.\V{returnShort}_=_\V{counter}+1;_}\Tab{40}{__\C{}\1\1_ Save Iteration as part of the}} \CE{}\L{\LB{}\Tab{40}{__\C{}\1\1_ data sent to the object}} \CE{}\L{\LB{__\V{aPacket}.\V{bShort}_=_\V{counter}+1;}} \L{\LB{}} \L{\LB{__\V{nread}=\V{write}(\V{serverId},\&\V{aPacket},\K{sizeof}(\V{aPacket}));_\C{}\1\1_ Write the Data out}} \CE{}\L{\LB{__\V{nread}=\V{read}(\V{serverId},\&\V{aPacket},\K{sizeof}(\V{aPacket}));__\C{}\1\1_ Read the Data in}} \CE{}\L{\LB{__}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_short:_\"\SE{}_\<\<_\V{aPacket}.\V{returnShort}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_long:_\"\SE{}_\<\<_\V{aPacket}.\V{returnLong}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_unsigned_short:_\"\SE{}_\<\<_\V{aPacket}.\V{returnUnsignedShort}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_unsigned_long:_\"\SE{}_\<\<_\V{aPacket}.\V{returnUnsignedLong}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_float:_\"\SE{}_\<\<_\V{aPacket}.\V{returnFloat}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_double:_\"\SE{}_\<\<_\V{aPacket}.\V{returnDouble}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_character:_\"\SE{}_\<\<_\V{aPacket}.\V{returnCharacter}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_boolean:_\"\SE{}_\<\<_(\K{int})_\V{aPacket}.\V{returnBoolean}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_string:_\"\SE{}_\<\<_\V{aPacket}.\V{returnString}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_short:_\"\SE{}_\<\<_\V{aPacket}.\V{bShort}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_long:_\"\SE{}_\<\<_\V{aPacket}.\V{bLong}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_float:_\"\SE{}_\<\<_\V{aPacket}.\V{bFloat}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\S{}\"Reconstructed_double:_\"\SE{}_\<\<_\V{aPacket}.\V{bDouble}_\<\<_\V{endl};}} \L{\LB{__\V{cout}_\<\<_\V{endl};}} \L{\LB{_\}}} \L{\LB{_\V{shutdown}(\V{serverId},2);}\Tab{48}{__\C{}\1\1_ Do a full shutdown of}} \CE{}\L{\LB{}\Tab{48}{__\C{}\1\1_ the socket upon}} \CE{}\L{\LB{}\Tab{48}{__\C{}\1\1_ completion}} \CE{}\L{\LB{_\K{return}_0;}} \L{\LB{\}}}