2010年5月22日

这次又要到Linux下做了,把之前的自己写的socket类改成了通用的,当然目前的结构还是太简单了,很多以前封装的不符合面向对象的函数又被我砍掉了,慢慢完善吧~~

 

Socket.h
  1 #if !defined(__DZLIB_SOCKET_H__)
  2 #define __DZLIB_SOCKET_H__
  3 
  4 #if defined(WIN32)
  5 #    include <WINSOCK2.H>
  6 #else
  7 #    include <stdio.h>
  8 #    include <netinet/in.h>
  9 #endif
 10 
 11 // -----------------------------------------------------------------------------
 12 // namespace DZLIB -------------------------------------------------------------
 13 namespace DZLIB
 14 {
 15 
 16 
 17 // -----------------------------------------------------------------------------
 18 // class SocketEnvironment -----------------------------------------------------
 19 #if defined(WIN32)
 20 class SocketEnvironment
 21 {
 22 public:
 23     // constructors and destructors
 24     SocketEnvironment();
 25     ~SocketEnvironment();
 26 
 27     // static funcs
 28     static bool Init();
 29     static bool Exit();
 30 };
 31 #endif
 32 
 33 
 34 // -----------------------------------------------------------------------------
 35 // class SocketAddr ------------------------------------------------------------
 36 class SocketAddr
 37 {
 38     friend class TcpSocket;
 39     friend class UdpSocket;
 40 
 41 public:
 42     // constructors and destructors
 43     SocketAddr();
 44     SocketAddr(u_short port, const char* cIp);
 45     SocketAddr(u_short port, u_long nIp);
 46 
 47     // public funcs
 48     void SetStrIP(const char* cIp);
 49     void SetNetIP(u_long nIp);
 50     void SetPort(u_short port);
 51     char* GetStrIP() const;
 52     u_short GetPort() const;
 53 
 54     // operations
 55     operator sockaddr*();
 56     operator sockaddr*() const;
 57 
 58 private:
 59     // properties
 60     short    _family;
 61     u_short    _port;
 62     u_long    _ip;
 63     char    _zero[8];
 64 };
 65 
 66 
 67 // -----------------------------------------------------------------------------
 68 // base class Socket -----------------------------------------------------------
 69 class Socket
 70 {
 71 public:
 72     // constructors and destructors
 73     Socket();
 74     Socket(u_int sockfd);
 75     virtual ~Socket();
 76 
 77     // pure virtual funcs
 78     virtual int Create() = 0;
 79     virtual int Create(u_short port, const char* cIp = NULL) = 0;
 80 
 81     // public funcs
 82     int Bind(u_short port, const char* cIp = NULL);
 83     int Close();
 84     int SetSockOpt(int optname, const void* optval, int optlen, int level = SOL_SOCKET);
 85     int GetSockOpt(int optname, void* optval, int& optlen, int level = SOL_SOCKET);
 86 
 87     // operations
 88     operator u_int();
 89 
 90 protected:
 91     // protected defines
 92 #if defined(WIN32)
 93     typedef int        SOCKLEN;
 94 #else
 95     typedef u_int    SOCKLEN;
 96 #endif
 97     // protected properties
 98     u_int    _sockfd;
 99 };
100 
101 
102 // -----------------------------------------------------------------------------
103 // class TcpSocket : Socket ----------------------------------------------------
104 class TcpSocket : public Socket
105 {
106 public:
107     // constructors
108     TcpSocket();
109     TcpSocket(u_int sockfd);
110 
111     // public funcs
112     int Create();
113     int Create(u_short port, const char* cIp = NULL);
114     int Listen(int backlog = 10);
115     int Accept(TcpSocket& peer);
116     int Accept(TcpSocket& peer, SocketAddr& peerAddr);
117     int Connect(u_long nIp, u_short port);
118     int Connect(const char* cIp, u_short port);
119     int Connect(const SocketAddr& addr);
120     int Send(const void* buf, u_int len);
121     int Recv(void* buf, u_int len);
122 };
123 
124 
125 // -----------------------------------------------------------------------------
126 // class UdpSocket : Socket ----------------------------------------------------
127 class UdpSocket : public Socket
128 {
129 public:
130     // constructors
131     UdpSocket();
132     UdpSocket(u_int sockfd);
133 
134     // public funcs
135     int Create();
136     int Create(u_short port, const char* cIp = NULL);
137     int SendTo(const void* buf, u_int len, u_long nIp, u_short port);
138     int SendTo(const void* buf, u_int len, const char* cIp, u_short port);
139     int SendTo(const void* buf, u_int len, const SocketAddr& to);
140     int RecvFrom(void* buf, u_int len);
141     int RecvFrom(void* buf, u_int len, SocketAddr& from);
142 };
143 
144 
145 // namespace DZLIB
146 
147 #endif // __DZLIB_SOCKET_H__
148 


posted @ 2010-05-22 21:21 dz 阅读(169) 评论(0) 编辑

2008年4月8日

繁体的,不过还是能理解的。
下载
原文:http://blog.darkthread.net/blogs/darkthreadtw/archive/2008/03/26/better-winform-ui.aspx
posted @ 2008-04-08 10:20 dz 阅读(387) 评论(0) 编辑

2008年4月2日

最新发现了一个能证明 string 类型是引用类型的小细节就是:
有时我们会写 string str = Content as string,
as 关键字要求对象是引用类型才行,这不就说明问题了吗?

太忙太久没更新,小小地冒个泡吧~

posted @ 2008-04-02 12:34 dz 阅读(49) 评论(0) 编辑

2008年3月12日

写了个小程序要添加Gmail ico进去,但是在网上找不到32*32以上大小的,都是16*16的。
于是乎动手制作了一个。

http://files.cnblogs.com/dz2345/g.rar


posted @ 2008-03-12 11:00 dz 阅读(123) 评论(2) 编辑

2008年2月16日

摘要: CHAPTER 8, 9, 10 阅读全文
posted @ 2008-02-16 22:00 dz 阅读(54) 评论(0) 编辑

2008年2月11日

摘要: Chapter 5 Manipulating Database Data
Chapter 6 Using Stored Procedures
使用存储过程
Chapter 7 Using XML阅读全文
posted @ 2008-02-11 10:50 dz 阅读(262) 评论(0) 编辑

2008年2月10日

摘要: 委托是一种定义了引用方法的类型。我们能为委托分配和它具有相同方法签名的静态或者实例方法。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。它就类似于 C++ 中的函数指针,但委托更安全可靠。委托就像是一类具有相同签名的方法的抽象,把它看作一个类就很好理解了。
一个委托实例可以搭载多个方法。不过要求方法的返回值为空,且不能有输出参数。
一个委托实例所搭载的方法并不需要属于同一个类。阅读全文
posted @ 2008-02-10 23:37 dz 阅读(194) 评论(0) 编辑
摘要: 不知道是不是因为没有总结习惯的关系,很多东西看了过后就忘记了,以至于最近让我不得不重视起来。1.string 是一种特殊的引用类型。2.is 检查对象是否与给定类型兼容,返回 true 或者 false,不会抛出异常。3.as 运算符类似于强制转换操作;但是,如果转换不可行,as 会返回 null 而不是引发异常。4.接口中,所有的方法都默认为 public;抽象方法所在的类必须为抽象类。5.re...阅读全文
posted @ 2008-02-10 22:01 dz 阅读(58) 评论(3) 编辑

2008年2月9日

摘要:
• PIVOT operator
• ROW_NUMBER() function
• PARTITION BY clause
• Pattern matching
• Aggregate functions
• DATETIME functions
• Joins
阅读全文
posted @ 2008-02-09 14:13 dz 阅读(300) 评论(1) 编辑

2008年2月8日

摘要: 复制 ArrayList 的时候,使用 ArrayList.Clone() 方法复制出来的新 ArrayList 之中元素的值会随着源 ArrayList 中元素的值发生变化而一起改变。查 MSDN:集合的浅表副本仅复制集合的元素(不论它们是引用类型还是值类型),但不复制引用所引用的对象。新集合中的引用与原始集合中的引用指向相同的对象。与之相对,集合的深层副本将复制这些元素以及由它们直接或间接引用...阅读全文
posted @ 2008-02-08 20:02 dz 阅读(1558) 评论(0) 编辑

导航

统计