* mysock.h
*
* 创建日期: 2013年7月27日
* 作者: 车强 ([email protected]).
*/
#ifndef _MYSOCK_H
#define _MYSOCK_H
#include
struct Mysock;
struct Mysock* newsock (int domain, int type, int protocol);
int mybind (const char* ip, const int port, struct Mysock* mysock);
int myconnect (struct Mysock* mysock);
int mylisten(int backlog, struct Mysock* mysock);
int myaccept (struct Mysock* mysock);
ssize_t mysend (int s, const void* buf, size_t len, size_t flags);
ssize_t myrecv (int s, const void* buf, size_t len, size_t flags);
void freesock(struct Mysock* mysock);
#endif /*_MYSOCK_H*/
#include "mysock.h"
#include
#include
#include
#include
struct Mysock {
int (* bind) (const char* ip, const int port, struct Mysock* mysock); //sys/types.h sys/socket.h
int (* connect) (struct Mysock* mysock); //sys/types.h sys/socket.h
int (* listen) (int backlog, struct Mysock* mysock); //sys/types.h sys/socket.h
int (* accept) (struct Mysock* mysock); //sys/types.h sys/socket.h
ssize_t (* send) (int s, const void* buf, size_t len, size_t flags); //sys/types.h sys/socket.h
ssize_t (* recv) (int s, void* buf, size_t len, size_t flags); //sys/types.h sys/socket.h
void* data;
struct sockaddr_in addr; //#include
struct sockaddr_in fromaddr;
int sockfd;
int acceptfd;
};
struct Mysock* newsock (int domain, int type, int protocol) {
struct Mysock* mysock;
mysock = (struct Mysock* )malloc(sizeof(struct Mysock));
if ((mysock->sockfd = socket (domain, type, protocol) ==-1 )) {
perror ("Func socket faied");
exit (-1);
}
mysock->addr.sin_family = domain;
mysock->bind = mybind;
mysock->connect = myconnect;
mysock->listen = mylisten;
mysock->accept = myaccept;
return mysock;
}
int mybind (const char* ip, const int port, struct Mysock* mysock) { //为了访问数据成员,需要传入该对象的指针
mysock->addr.sin_port = htons(port);
mysock->addr.sin_addr.s_addr = inet_addr(ip);
if (bind (mysock->sockfd, (struct sockaddr*)&(mysock->addr), sizeof(mysock->addr)) == -1)
perror ("Bind fail"), exit (-1);
return 0;
}
int myconnect (struct Mysock* mysock) { //为了访问数据成员,需要这个指针
if (connect (mysock->sockfd, (struct sockaddr*)&(mysock->addr), sizeof(mysock->addr)) == -1)
perror ("Connect fail"), exit (-1);
return 0;
}
int mylisten(int backlog, struct Mysock* mysock) { //为了访问数据成员,需要这个指针
if (listen (mysock->sockfd, backlog) == -1)
perror ("Listen fail"), exit (-1);
return 0;
}
int myaccept (struct Mysock* mysock) { //为了访问数据成员,需要这个指针
socklen_t len = sizeof(mysock->fromaddr);
if (mysock->acceptfd = accept(mysock->sockfd, //区分不同主机acceptfd
(struct sockaddr*)&mysock->fromaddr,
&len) == -1)
perror ("Accept fail"), exit (-1);
return mysock->acceptfd;
}
ssize_t mysend (int s, const void* buf, size_t len, size_t flags) { //send 和recv 有待进一步完善
return send (s, buf, len, flags);
}
ssize_t myrecv (int s, const void* buf, size_t len, size_t flags) {
return recv (s, buf, len, flags);
}
void freesock(struct Mysock* mysock) {
if (!mysock)
free(mysock);
}
想尝试着封装一下socket ,使他们看起来更简洁,统一。注释没写,因为这些函数比较常见,定义了一个Mysock类,数据的操作尽量在该类的成员函数中完成。为了访问成员函数中的数据,我只能想出把该指向对象的指针作为参数传递给成员函数的方法了,这有点像c++的this指针,我想c++的设计者是不是在这个过程中发现,干脆约定一个叫this指针的东西吧,这样就不用给每个成员函数都定义一个指向自己的指针了。另外这个类中还定义了一个void* data,这个可是神器,它能访问的东西很多,例如它的父亲,它能和很多东西发生关联。