Visual C++ .NET/Network/TcpServer

Материал из .Net Framework эксперт
Версия от 12:06, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

TcpServer Demo

 
#include "stdafx.h"
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Threading;
using namespace System::Text;
ref class TcpServer
{
public:
    void ProcessThread(Object ^clientObj);
};
void TcpServer::ProcessThread(Object ^clientObj)
{
    Socket^ client = (Socket^)clientObj;
    IPEndPoint^ clientEP = (IPEndPoint^)client->RemoteEndPoint;
    Console::WriteLine("Connected on IP: {0} Port: {1}", clientEP->Address, clientEP->Port);
    array<unsigned char>^ msg = Encoding::ASCII->GetBytes("connected");
    client->Send(msg);
    int rcv;
    while (true)
    {
        msg = gcnew array<unsigned char>(1024);
        if ((rcv = client->Receive(msg)) == 0)
            break;
        Console::WriteLine("Port[{0}] {1}", clientEP->Port, Encoding::ASCII->GetString(msg, 0, rcv));
        client->Send(msg, rcv, SocketFlags::None);
    }
    client->Close();
    Console::WriteLine("Connection to IP: {0} Port {1} closed.", clientEP->Address, clientEP->Port);
}
void main()
{
    TcpServer^ server = gcnew TcpServer();
    Socket^ tcpListener = gcnew Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
    IPEndPoint^ iped = gcnew IPEndPoint(IPAddress::Any, 12345);
    tcpListener->Bind(iped);
    tcpListener->Listen((int)SocketOptionName::MaxConnections);
    while(true) 
    {
        Console::WriteLine("Waiting for client connection.");
        Socket^ client = tcpListener->Accept();
        Thread ^thr = gcnew Thread(
            gcnew ParameterizedThreadStart(server, &TcpServer::ProcessThread));
        thr->Start(client);
    }
}


TcpServer Listener

 
#include "stdafx.h"
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Threading;
using namespace System::Text;
ref class TcpServer
{
public:
    void ProcessThread(Object ^clientObj);
};
void TcpServer::ProcessThread(Object ^clientObj)
{
    Socket^ client = (Socket^)clientObj;
    IPEndPoint^ clientEP = (IPEndPoint^)client->RemoteEndPoint;
    Console::WriteLine(clientEP->Address);
    Console::WriteLine(clientEP->Port);
    array<unsigned char>^ msg = Encoding::ASCII->GetBytes("Success ");
    client->Send(msg);
    int rcv;
    while (true)
    {
        msg = gcnew array<unsigned char>(1024);
        if ((rcv = client->Receive(msg)) == 0)
            break;
        Console::WriteLine(clientEP->Port);
        Console::WriteLine(Encoding::ASCII->GetString(msg, 0, rcv));
        client->Send(msg, rcv, SocketFlags::None);
    }
    client->Close();
    Console::WriteLine(clientEP->Address);
    Console::WriteLine(clientEP->Port);
}
void main()
{
    TcpServer^ server = gcnew TcpServer();
    TcpListener^ socket = gcnew TcpListener(IPAddress::Any, 12345);
    socket->Start();
    
    while(true) 
    {
        Console::WriteLine("Waiting for client connection.");
        Socket^ client = socket->AcceptSocket();
        Thread ^thr = gcnew Thread(
            gcnew ParameterizedThreadStart(server, &TcpServer::ProcessThread));
        thr->Start(client);
    }
}


Tcp Server Stream

 
#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Threading;
ref class TcpServer
{
public:
    void ProcessThread(Object ^clientObj);
};
void TcpServer::ProcessThread(Object ^clientObj)
{
    TcpClient^ client = (TcpClient^)clientObj;
    IPEndPoint^ clientEP = (IPEndPoint^)client->Client->RemoteEndPoint;
    Console::WriteLine("Connected on IP: {0} Port: {1}", clientEP->Address, clientEP->Port);
    StreamWriter^ writer = gcnew StreamWriter(client->GetStream()); 
    StreamReader^ reader = gcnew StreamReader(client->GetStream()); 

    writer->WriteLine(clientEP->Port);
    writer->Flush();
    String^   msg = "asdf";
    Console::WriteLine("Port[{0}] {1}", clientEP->Port, msg);
    writer->WriteLine(msg);
    writer->Flush();
    client->Close();
    Console::WriteLine("Connection to IP: {0} Port {1} closed.", clientEP->Address, clientEP->Port);
}
void main()
{
    TcpServer^ server = gcnew TcpServer();
    TcpListener^ socket = gcnew TcpListener(IPAddress::Any, 12345);
    socket->Start();
    
    while(true) 
    {
        Console::WriteLine("Waiting for client connection.");
        TcpClient^ client = socket->AcceptTcpClient();
        Thread ^thr = gcnew Thread(gcnew ParameterizedThreadStart(server, &TcpServer::ProcessThread));
        thr->Start(client);
    }
}