怕忘記還是記一下吧。
// 這是使用應用程式精靈所產生之 VC++ 應用程式專案的主專案檔。
#include "stdafx.h"
#include<iostream>
using namespace std;
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Collections;
int _tmain()
{
try
{
Socket *clientSocket;
Socket* serverSocket = new Socket(AddressFamily::InterNetwork,SocketType::Stream, ProtocolType::Tcp) ;
IPAddress *serverIP = IPAddress::Parse("140.113.131.47") ;//指定local ip
String* Port = "1234" ;//聆聽的port
IPEndPoint* serverhost = new IPEndPoint(serverIP, Int32::Parse(Port)) ;
serverSocket->Bind(serverhost) ;
serverSocket->Listen(50) ;
unsigned char recvbytes __gc[]=new unsigned char __gc[1024];
unsigned char sendbytes __gc[]=new unsigned char __gc[1024];
while (true)
{
clientSocket = serverSocket->Accept() ;
int len = clientSocket->Receive(recvbytes) ;//從client接收字串
char buffer[1024];
for(int i=0;i<len;i++)
buffer[i]=recvbytes[i];//轉換成char型態後,比較好處理
cout<<"Server收到:"<<buffer<<endl;
//收到php的回應後,想做什麼都可以,接著送出回應
char* reply="科科";
for(int i=0;i<strlen(reply);i++)//要傳送資料前,必須將char轉換成unsigned char __gc的形式
sendbytes[i]=reply[i];
clientSocket->Send(sendbytes,strlen(reply),SocketFlags::None);//傳送資料就呼叫此函數,記得第二個參數是傳送的長度,一定要填對!
clientSocket->Close();//關閉socket
}
}
catch (Exception *ex)
{
Console::WriteLine(ex->ToString()) ;
}
return 0;
}
沒有留言:
張貼留言