一个服务器模型的测试(例子)

服务器端:
[ssj@main test]$ cat easyserver.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#define MAX 200 /*缓冲区大小*/
#define BB 16 /*IP地址长度 IPV4是16,IPV6是28*/
void my_turn(char *p) /*字符处理函数,小写变大写,linux下没有strupr了???*/
{
if (p == NULL)return;
while( *p != '')
{
if( *p >= (char)0x61 && *p <= (char)0x7a)
*p=(char)((int)*p-(int)0x20);
p++;
}
}

int main(void)
{
struct sockaddr_in sin,cin; /*sin本地IP地址结构,cin客户端IP地址结构*/
int lfd,cfd; /*lfd本地socket套接字描述符,cfd,由accept建立的*/
socklen_t len; /*客户端IP长度*/
char buf[MAX]; /*缓冲区*/
char addr_p[BB]; /*IP地址十进制字符串*/
int port = 65533; /*绑定的端口*/
int n; /*收到的数据长度*/
bzero(&sin,sizeof(sin)); /*初始化sin*/
sin.sin_family=AF_INET; /*定义为IPV4*/
sin.sin_addr.s_addr=INADDR_ANY; /*服务器可以接受任意地址*/
sin.sin_port=htons(port); /*把IP地址转有16位字节序,并赋值*/

lfd=socket(AF_INET,SOCK_STREAM,0); /*建立套接字*/
bind(lfd,(struct sockaddr *)&sin,sizeof(sin)); /*绑定端口与套接字*/
listen(lfd,10); /*监听,并设置最大队列为10*/
printf("running……….n");

while(1) /*循环处理客户的连接请求*/
{
cfd=accept(lfd,(struct sockaddr *)&cin,&len);
n=read(cfd,buf,MAX);
inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p));
printf("client IP is:%s:%d size:%dn",addr_p,ntohs(cin.sin_port),n);
if (strcmp(buf,"close")==0) /*是否退出*/
{
write(cfd,"server done",12);
close(cfd);
close(lfd);
printf("server done.n");
exit(0);
}
printf("type:%sn",buf);
my_turn(buf);
printf("turn type:%sn=================================n",buf);
write(cfd,buf,n);
close(cfd);
}
if(close(lfd)==-1) /*收尾,实际上,上边的while是个死循环,用ctrl+c退出,或用close退出,都执行不到这里*/
{
perror("fail close");
exit(1);
}
return 0;
}

客户端:
[ssj@main test]$ cat easyclient.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#define MAX 200

int main(int argc,char * argv[])
{
struct sockaddr_in pin;
struct sockaddr_in *apin=&pin;
char buf[MAX];
int sfd,port=65533;
char *str="ssj test";

if (argc>1)
{
str=argv[1];
}
bzero(&pin,sizeof(pin));
pin.sin_family=AF_INET;
inet_pton(AF_INET,"127.0.0.1",&pin.sin_addr);
pin.sin_port=htons(port);

sfd=socket(AF_INET,SOCK_STREAM,0);
connect(sfd,(struct sockaddr *)&pin,sizeof(pin));
write(sfd,str,strlen(str)+1);
read(sfd,buf,MAX);
printf("recive:%sn",buf);
close(sfd);
return 0;
}

======================================
更完善的一个版本(服务器)
[ssj@main test]$ cat easyserver.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include "ssjio.c"
#define MAX 200
#define BB 16
void my_turn(char *p)
{
if (p == NULL)return;
while( *p != '')
{
if( *p >= (char)0x61 && *p <= (char)0x7a)
*p=(char)((int)*p-(int)0x20);
p++;
}
}

int main(void)
{
struct sockaddr_in sin,cin;
int lfd,cfd;
socklen_t len;
char buf[MAX];
char addr_p[BB];
int port = 65533;
int n;
bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(port);

if((lfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Fail to create socket");
exit(1);
}

if(bind(lfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("Fail to bind");
exit(1);
}
if(listen(lfd,10)==-1)
{
perror("Fail to listen");
exit(1);
}
printf("running……….n");

while(1)
{
if((cfd=accept(lfd,(struct sockaddr *)&cin,&len))==-1)
{
perror("Fail to accept");
exit(1);
}

n=ssj_read(cfd,buf,MAX);
if (n==-1)
exit(1);
else if(n==0)
{
printf("the connect closedn");
close(cfd);
continue;
}

inet_ntop(AF_INET,&cin.sin_addr,addr_p,sizeof(addr_p));
printf("client IP is:%s:%d size:%dn",addr_p,ntohs(cin.sin_port),n);
if (strcmp(buf,"close")==0)
{
n=ssj_write(cfd,"server done",12);
if(n==-1)
exit(1);
close(cfd);
close(lfd);
printf("server done.n");
exit(0);
}
printf("type:%sn",buf);
my_turn(buf);
printf("turn type:%sn=================================n",buf);
n=ssj_write(cfd,buf,n);
if(n==-1)
exit(1);

if(close(cfd)==-1)
{
perror("Fail to close");
exit(1);
}
}
if(close(lfd)==-1)
{
perror("fail close");
exit(1);
}
return 0;
}

=========================
[ssj@main test]$ cat ssjio.c
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

ssize_t ssj_read(int fd,void *buf,size_t length)
{
ssize_t done=length;
done=read(fd,buf,length);
return done;
}

ssize_t ssj_write(int fd,void *buf,size_t length)
{
ssize_t done=length;
while(done>0)
{
done=write(fd,buf,length);
if(done!=length)
if (errno==EINTR)
done=length;
else{
perror("Fail to write");
return -1;
}
else
break;
}
return done;
}

客户端同理

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注