首先是我的一个出错提示的头文件<myerr.h>,自从用了根本停不下来啊!!!

#ifndef			_MYERR_H_
#define _MYERR_H_ #include <stdio.h>
#include <string.h>
#include <errno.h>
extern int errno; #define MYDBG #ifdef MYDBG
#define PRN_ERRMSG_EXIT(errmsg) \
({ \
fprintf(stderr, "Say->%s : File->%s : Line->%d : Fun->%s : Cause->%s\n\r", \
errmsg,__FILE__,__LINE__,__func__,strerror(errno)); \
exit(-1); \
})
#else
#define PRN_ERRMSG_EXIT(errmsg) ({exit(-1);})
#endif #ifdef MYDBG
#define PRN_ERRMSG_RETURN(errmsg) \
({ \
fprintf(stderr, "Say->%s : File->%s : Line->%d : Fun->%s : Cause->%s\n\r", \
errmsg,__FILE__,__LINE__,__func__,strerror(errno)); \
return(-1); \
})
#else
#define PRN_ERRMSG_RETURN(errmsg) ({return(-1);})
#endif #ifdef MYDBG
#define PRN_ERRMSG(errmsg) \
({ \
fprintf(stderr, "Say->%s : File->%s : Line->%d : Fun->%s : Cause->%s\n\r", \
errmsg,__FILE__,__LINE__,__func__,strerror(errno)); \
})
#else
#define PRN_ERRMSG(errmsg)
#endif #ifdef MYDBG
#define ASSERT(EXP) \
({\
if(!(EXP)){\
fprintf(stderr, "Say->%s : File->%s : Line->%d : Fun->%s : Cause->%s\n\r", \
__FILE__,__LINE__,__func__,strerror(errno)); \
exit(-1); \
}\
})
#else
#define ASSERT(EXP)
#endif #endif //_ERROR_H_

然后就是我编写的简便代码,方便其他地方调用额。<socket.c>

#include "mysocket.h"

int ser_socket(const char *ipstr, const unsigned short port)
{
/*创建一个流式套接字*/
int s = socket(PF_INET, SOCK_STREAM, 0);
if (0 > s)
PRN_ERRMSG_RETURN("socket"); int on = 1;
if (0 > setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
PRN_ERRMSG_RETURN("setsockopt"); struct sockaddr_in addr = {
.sin_family = PF_INET,
.sin_port = htons(port),
.sin_addr.s_addr = (NULL==ipstr)?INADDR_ANY:inet_addr(ipstr),
}; memset(addr.sin_zero, 0, sizeof(addr.sin_zero));
if (0 > bind(s, (struct sockaddr*)&addr, sizeof(struct sockaddr)))
PRN_ERRMSG_RETURN("bind"); if (0 > listen(s, BACKLOG))
PRN_ERRMSG_RETURN("listen"); return s;
} int cli_socket(const char *ipstr, const unsigned short port)
{
/*创建一个流式套接字*/
int s = socket(PF_INET, SOCK_STREAM, 0);
if (0 > s)
PRN_ERRMSG_RETURN("socket"); struct sockaddr_in addr = {
.sin_family = PF_INET,
.sin_port = htons(port),
.sin_addr.s_addr = inet_addr(ipstr),
}; memset(addr.sin_zero, 0, sizeof(addr.sin_zero));
if (0 > connect(s, (struct sockaddr*)&addr, sizeof(struct sockaddr)))
PRN_ERRMSG_RETURN("connect"); return s;
} int my_accept(int sockFd, struct sockaddr_in *addr)
{
socklen_t len = sizeof(struct sockaddr_in);
memset(addr, 0, len); // 将结构体清零
int rws = accept(sockFd, (struct sockaddr*)addr, &len);
if (0 > rws)
PRN_ERRMSG_RETURN("accept"); return rws;
}

当然也要为这个文件编写头文件了。。。<mysocket.h>

#ifndef		_SOCKET_H_
#define _SOCKET_H_ #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> #include <myerr.h> #define BACKLOG 5 /* length of listening queue on socket */ /*
* 服务器开启服务
* ipstr : 服务器ip
* port : 绑定的端口
* 返回 : 一个网络套接字
*/
int ser_socket(const char *ipstr, const unsigned short port);
/*
* 客户端连接服务器
* ipstr : 服务器ip
* port : 服务器的端口
* 返回 : 一个网络套接字
*/
int cli_socket(const char *ipstr, const unsigned short port);
/*
* 等待客户端连接
* sockFd : 套接字
* addr : 接收客户端信息
* 返回 : 与客户端通信的文件描述副
*/
int my_accept(int sockFd, struct sockaddr_in *addr); #endif //_SOCKET_H_

然后就是服务器的代码了。<main_ser.c>

#include "mysocket.h"

int main(int argc,char *argv[])
{
int serFd = ser_socket(NULL, 8888);
struct sockaddr_in addr;
char buffer[1024];
while (1) {
int accFd = my_accept(serFd, &addr);
while (1) {
memset(buffer, 0, 1024);
if (0 >= recv(accFd, buffer, 1024, 0)) {
PRN_ERRMSG("recv");
close(accFd);
break;
}
printf("client : %s\n", buffer);
if (0 > send(accFd, buffer, strlen(buffer), 0)) {
PRN_ERRMSG("recv");
close(accFd);
break;
}
}
}
return 0;
}

然后就是客户端的代码了<main_cli.c>

#include "mysocket.h"

int main(int argc,char *argv[])
{
int sockFd = cli_socket("127.0.0.1", 8888);
char buffer[1024];
while (1) {
printf("input :");
fgets(buffer, 1024, stdin);
*strchr(buffer, '\n') = '\0';// 采用这种方法可以避免输入超出缓冲区
if (0 > send(sockFd, buffer, strlen(buffer), 0)) {
PRN_ERRMSG("recv");
close(sockFd);
break;
}
memset(buffer, 0, 1024);
if (0 >= recv(sockFd, buffer, 1024, 0)) {
PRN_ERRMSG("recv");
close(sockFd);
break;
}
printf("server : %s\n", buffer);
}
return 0;
}

好啦。。以后需要用的时候再来看看额。。。

05-11 20:18