【C语言】linux下c语言 客户端服务器编程框架

客户端服务器编程模型:

客户端代码:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXLINE 100;
#define RIO_BUFSIZE 8192;


int main()
{

int clientfd = socket(AF_INET,SOCK_STREAM,0);  
//创建一个客户端套接字描述符

struct sockaddr_in serveraddr;
struct hostent *hp;
bzero((char *)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(15636);
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//创建服务器套接字

connect(clientfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
//将客户端套接字描述符与服务器套接字进行连接
printf("connected.\n");
char buf[100];
//定义发送数据缓冲区 while(1){
printf("please input string:\n");
fgets(buf,100,stdin);
//从键盘输入字符串
send(clientfd,buf,100,0);
//将位于buf缓冲区的数据发送到clientfd描述符连接的套接字的IP地址对应的线程
recv(clientfd,buf,100,0);
//从clientfd关联的服务器端进程获得数据。并保存在缓冲区中
printf("%s\n",buf);
}


close(clientfd);
return EXIT_SUCCESS;
}

服务器端代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main()
{

int port=15636 ;
int listenfd = socket(AF_INET, SOCK_STREAM,0);
//创建服务器端套接字描述符,用于监听客户端请求
struct hostent *hp;
struct sockaddr_in serveraddr;
bzero((char *)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(port);
//创建用于服务的服务器端套接字,注意与 客户端创建的套接字的区别  IP段里,这里是可以为任何IP提供服务的,客户端里的IP是请求的端点

bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
//将服务器端的套接字描述符和服务器端套接字相关联

listen(listenfd,1024);
//打开监听
char temp[100];
struct sockaddr_in clientaddr;
int clientlen, connfd;
clientlen = sizeof(clientaddr);
connfd = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen);
//等待接受客户端的请求,这是一个阻塞函数。若成功接受客户端请求则返回新的套接字描述符,这个描述符用于 send和recv函数
while(1){

recv(connfd,temp,100,0);
printf("%s\n",temp);
fgets(temp,100,stdin);
send(connfd,temp,100,0);
}
//recv接收客户端发送过来的字符串,send发送给客户端字符串
 EXIT_SUCCESS;
}

注意:以上代码只是为了突出客户端服务器编程框架,因为并未判断出错及相关处理方法。

下面为完整代码:

客户端:

//the client code
//copyright  chenglibin  http://blog.csdn.net/chenglibin1988
#include #include #include #include #include #include #include #include #include #include #define MAXLINE 100;int main(){int clientfd = socket(AF_INET,SOCK_STREAM,0);if(clientfd < 0){ perror("socket"); exit(1);}struct sockaddr_in serveraddr;struct hostent *hp;bzero((char *)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(15636);
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(clientfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
        perror("connect");
        exit(1);
}

printf("connected\n");
char buf[100];
int idata;
while(1){
idata = 0;
printf("input a string:\n");
fgets(buf,100,stdin);
send(clientfd,buf,100,0);
idata = recv(clientfd,buf,100,0);
if(idata < 0){
        perror("recv");
        exit(1);
}

printf("data received from server is:\n %s\n",buf);
}

close(clientfd);
return EXIT_SUCCESS;
}
 
  

 
  

***************************************************************************************************************************************************************************************

***************************************************************************************************************************************************************************************

服务器端代码如下:

//the server code
//copyright  chenglibin  http://blog.csdn.net/chenglibin1988
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define PORT 15636
int main()
{

int listenfd = socket(AF_INET, SOCK_STREAM,0);
if(listenfd < 0){
        perror("socket");
        exit(1);
}

struct hostent *hp;
struct sockaddr_in serveraddr;
bzero((char *)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);

if(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
        perror("connect");
        exit(1);
}

if(listen(listenfd,1024) < 0){
        perror("listen error");
        exit(1);
}

char temp[100];
struct sockaddr_in clientaddr;
int clientlen, connfd;
clientlen = sizeof(clientaddr);

connfd = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen);
if(connfd < 0){
        perror("accept error");
        exit(1);
}

printf("Accepted!\n");

int idata;
while(1){
idata = 0;
idata = recv(connfd,temp,100,0);
if(idata < 0){
perror("recv error");
exit(1);
}
printf("data received from client is :\n%s\n",temp);

printf("input a string:\n");
fgets(temp,100,stdin);
send(connfd,temp,100,0);
}
EXIT_SUCCESS;
}



你可能感兴趣的:(C语言)