扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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"); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者