随笔-341  评论-2670  文章-0  trackbacks-0
    为了建立一个remote object access system我不得不封装了socket、pipe以及synchronize objects。不过我还不知道如何实现穿透,这个就暂时算了。今晚用了两个小时封装了socket的一些必要操作,并写了人生中第一个socket程序。

    这个程序同时作为server和client。第一次启动的为server。接下来server进行监听,client则往server发信息并接受返回的信息。server会将接受到的信息进行反转发回去,除非收到了exit结束线程,last结束线程并结束监听。

    明天继续写脚本引擎的教程。
  1 #include "..\..\..\..\VL++\Library\Data\VL_Comm.h"
  2 #include "..\..\..\..\VL++\Library\Platform\VL_Console.h"
  3 #include "..\..\..\..\VL++\Library\Data\VL_System.h"
  4 
  5 using namespace vl;
  6 using namespace vl::platform;
  7 using namespace vl::system;
  8 using namespace vl::system::synchronization;
  9 using namespace vl::communication;
 10 
 11 volatile VBool LastSocketClient=false;
 12 
 13 class SocketThread : public VL_Thread
 14 {
 15 protected:
 16     VL_ServerSocket* FSocket;
 17 public:
 18     SocketThread(VL_ServerSocket* Socket):VL_Thread(false,true)
 19     {
 20         FSocket=Socket;
 21     }
 22 
 23     ~SocketThread()
 24     {
 25         delete FSocket;
 26     }
 27 
 28     void Run()
 29     {
 30         VWChar Buffer[1024];
 31         while(true)
 32         {
 33             memset(Buffer,0,sizeof(Buffer));
 34             if(FSocket->ReadData((VBuffer)Buffer,sizeof(Buffer),false)>0)
 35             {
 36                 if(wcscmp(Buffer,L"exit")==0)
 37                 {
 38                     return;
 39                 }
 40                 else if(wcscmp(Buffer,L"last")==0)
 41                 {
 42                     LastSocketClient=true;
 43                     return;
 44                 }
 45                 VInt Len=wcslen(Buffer);
 46                 for(VInt i=0;i<Len/2;i++)
 47                 {
 48                     VWChar First=Buffer[i];
 49                     Buffer[i]=Buffer[Len-1-i];
 50                     Buffer[Len-1-i]=First;
 51                 }
 52                 FSocket->WriteData((VBuffer)Buffer,Len*sizeof(VWChar));
 53             }
 54             else
 55             {
 56                 return;
 57             }
 58         }
 59     }
 60 };
 61 
 62 void vlmain(VL_Console& Con)
 63 {
 64     Con.SetTitle(L"Vczh Socket");
 65     Con.SetTestMemoryLeaks(true);
 66     Con.SetPauseOnExit(false);
 67 
 68     VBool ServerProcess=false;
 69     VL_SynEvent Event;
 70     switch(Event.Create(false,true,L"VCZH_EVENT"))
 71     {
 72     case VL_SynObject::arSucceed:
 73         ServerProcess=true;
 74         break;
 75     case VL_SynObject::arAlreadyExists:
 76         break;
 77     case VL_SynObject::arFail:
 78         Con.Write(L"Fail\r\n");
 79         return;
 80     }
 81 
 82     if(ServerProcess)
 83     {
 84         VL_SocketServer Server(vspTCP,L"8000",false);
 85         if(Server.IsSuccess())
 86         {
 87             Con.Write(L"Setup server success.\r\n");
 88             while(!LastSocketClient)
 89             {
 90                 VL_ServerSocket* Socket=Server.WaitForConnection();
 91                 if(Socket==0)
 92                 {
 93                     LastSocketClient=true;
 94                 }
 95                 else
 96                 {
 97                     Con.Write(L"Get a client.\r\n");
 98                     new SocketThread(Socket);
 99                 }
100             }
101             Con.Write(L"Server stops.\r\n");
102             Con.SetPauseOnExit(true);
103         }
104         else
105         {
106             Con.Write(L"Setup server fails.\r\n");
107             Con.SetPauseOnExit(true);
108         }
109     }
110     else
111     {
112         VL_ClientSocket Socket(vspTCP,L"127.0.0.1",L"8000");
113         if(Socket.Connect())
114         {
115             VUnicodeString Input;
116             VWChar Buffer[1024];
117             while(true)
118             {
119                 Con.Write(L"CLIENT>");
120                 Con.Read(Input);
121                 Socket.WriteData((VBuffer)Input.Buffer(),Input.Length()*sizeof(VWChar));
122                 if(Input==L"exit" || Input==L"last")
123                 {
124                     break;
125                 }
126                 memset(Buffer,0,sizeof(Buffer));
127                 if(Socket.ReadData((VBuffer)Buffer,sizeof(Buffer),false)>0)
128                 {
129                     Con.Write(Buffer);
130                     Con.Write(L"\r\n");
131                 }
132                 else
133                 {
134                     Con.Write(L"Connection fails.\r\n");
135                     Con.SetPauseOnExit(true);
136                     break;
137                 }
138             }
139         }
140         else
141         {
142             Con.Write(L"Connect to server fails.\r\n");
143             Con.SetPauseOnExit(true);
144         }
145     }
146 }

posted on 2008-08-01 06:23 陈梓瀚(vczh) 阅读(1661) 评论(3)  编辑 收藏 引用

评论:
# re: 封装了Socket 2008-08-01 20:09 | 空明流转
很好,很强大,我没看懂。  回复  更多评论
  
# re: 封装了Socket 2008-08-01 22:43 | 李宜
牛  回复  更多评论
  
# re: 封装了Socket 2008-08-01 23:55 | 陈梓瀚(vczh)
楼上跟我认识的一个人同名  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理