using namespace System; using namespace System::IO; using namespace System::Net; using namespace System::Net::Sockets; int main(array<String^>^ argv) { if (argv->Length != 1) { Console::WriteLine("Usage: Server port"); Environment::Exit(1); } int port = 0; try { port = Int32::Parse(argv[0]); } catch (FormatException^ e) { Console::WriteLine("Port number {0} is ill-formed", argv[0]); Environment::Exit(2); } /*1*/ if (port < IPEndPoint::MinPort || port > IPEndPoint::MaxPort) { Console::WriteLine("Port number must be in the range {0}-{1}", IPEndPoint::MinPort, IPEndPoint::MaxPort); Environment::Exit(3); } /*2*/ IPAddress^ ipAddress = Dns::GetHostEntry(Dns::GetHostName())->AddressList[0]; /*3*/ IPEndPoint^ ipEndpoint = gcnew IPEndPoint(ipAddress, port); /*4*/ Socket^ listenerSocket = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp); /*5*/ listenerSocket->Bind(ipEndpoint); /*6*/ listenerSocket->Listen(1); /*7*/ Console::WriteLine("Server listener blocking status is {0}", listenerSocket->Blocking); /*8*/ Socket^ serverSocket = listenerSocket->Accept(); Console::WriteLine("New connection accepted"); /*9*/ listenerSocket->Close(); /*10*/ NetworkStream^.netStream = gcnew NetworkStream(serverSocket); /*11*/ BinaryReader^ br = gcnew BinaryReader(netStream); /*12*/ BinaryWriter^ bw = gcnew BinaryWriter(netStream); try { int value1, value2; int result; while (true) { /*13*/ value1 = br->ReadInt32(); /*14*/ value2 = br->ReadInt32(); Console::Write("Received values {0,3} and {1,3}", value1, value2); result = value1 + value2; /*15*/ bw->Write(result); Console::WriteLine(", sent result {0,3}", result); } } /*16*/ catch (EndOfStreamException^ e) { } /*17*/ catch (IOException^ e) { Console::WriteLine("IOException {0}", e); } /*18*/ serverSocket->Shutdown(SocketShutdown::Both); /*19*/ serverSocket->Close(); /*20*/ netStream->Close(); Console::WriteLine("Shutting down server"); } |
using namespace System; using namespace System::IO; using namespace System::Net; using namespace System::Net::Sockets; using namespace System::Threading; int main(array<String^>^ argv) { if (argv->Length != 2) { Console::WriteLine("Usage: Client port message-count"); Environment::Exit(1); } int port = 0; try { port = Int32::Parse(argv[0]); } catch (FormatException^ e) { Console::WriteLine("Port number {0} is ill-formed", argv[0]); Environment::Exit(2); } if (port < IPEndPoint::MinPort || port > IPEndPoint::MaxPort) { Console::WriteLine("Port number must be in the range {0}-{1}",IPEndPoint::MinPort, IPEndPoint::MaxPort); Environment::Exit(3); } int messageCount = 0; try { messageCount = Int32::Parse(argv[1]); } catch (FormatException^ e) { Console::WriteLine("Message count {0} is ill-formed", argv[1]); Environment::Exit(4); } IPAddress^ ipAddress = nullptr; try { /*1*/ ipAddress = Dns::GetHostEntry(Dns::GetHostName())->AddressList[0]; /*2*/ IPEndPoint^ ipEndpoint = gcnew IPEndPoint(ipAddress, port); /*3*/ Socket^ clientSocket = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp); /*4*/ clientSocket->Connect(ipEndpoint); NetworkStream^ netStream = gcnew NetworkStream(clientSocket); BinaryReader^ br = gcnew BinaryReader(netStream); BinaryWriter^ bw = gcnew BinaryWriter(netStream); int value1, value2; int result; Random^ random = gcnew Random; (int i = 1; i <= messageCount; ++i) { /*5*/ value1 = static_cast<int>(random->NextDouble() * 100); /*6*/ value2 = static_cast<int>(random->NextDouble() * 100); /*7*/ bw->Write(value1); /*8*/ bw->Write(value2); Console::Write("Sent values {0,3} and {1,3}",value1, value2); /*9*/ result = br->ReadInt32(); Console::WriteLine(", received result {0,3}", result); /*10*/ Thread::Sleep(3000); } /*11*/ clientSocket->Shutdown(SocketShutdown::Both); Console::WriteLine("Notified server we're shutting down"); /*12*/ clientSocket->Close(); /*13*/ netStream->Close(); Console::WriteLine("Shutting down client"); } /*14*/ catch (SocketException^ e) { Console::WriteLine("Request to connect to {0} on port {1} failed"+ "\nbecause of {2}", ipAddress, port, e); Environment::Exit(5); } } |
Server listener blocking status is True New connection accepted Received values 42 and 69, sent result 111 Received values 66 and 71, sent result 137 Received values 7 and 93, sent result 100 Received values 43 and 65, sent result 108 Received values 45 and 3, sent result 48 Shutting down server |
Sent values 42 and 69, received result 111 Sent values 66 and 71, received result 137 Sent values 7 and 93, received result 100 Sent values 43 and 65, received result 108 Sent values 45 and 3, received result 48 Notified server we're shutting down Shutting down client |
Client 2600 4 Sent values 56 and 35, received result 91 Sent values 48 and 20, received result 68 Sent values 6 and 97, received result 103 Sent values 76 and 9, received result 85 Notified server we're shutting down Shutting down client Client 2600 2 Sent values 69 and 66, received result 135 Sent values 84 and 45, received result 129 Notified server we're shutting down Shutting down client Server 2600 Waiting for new connection request New connection accepted Started thread Thread-1 Waiting for new connection request Executing thread Thread-1 Received values 56 and 35, sent result 91 New connection accepted Started thread Thread-2 Waiting for new connection request Executing thread Thread-2 Received values 69 and 66, sent result 135 Received values 48 and 20, sent result 68 Received values 84 and 45, sent result 129 Received values 6 and 97, sent result 103 Shutting down server thread Thread-2 Received values 76 and 9, sent result 85 Shutting down server thread Thread-1 |