Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54635
  • 博文数量: 23
  • 博客积分: 1598
  • 博客等级: 上尉
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-27 10:26
文章分类

全部博文(23)

文章存档

2011年(2)

2010年(21)

我的朋友

分类: LINUX

2010-08-31 17:10:13

                                                //server.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAXSIZE 255
#define PORTNUM 5555
#define THREAD_NUMBER 2


//***************************************************
//***********************读函数***********************
void *Thrd_Read(void *sockfd)
{
int clientfd=(int)sockfd;
char buf[MAXSIZE];
int readnum;


printf("waiting to read!\n");

while(1)
{
bzero(buf,MAXSIZE);
readnum=recv(clientfd,buf,MAXSIZE,0);
if(readnum==-1)
{
printf("the read error!\n");
break;
}
else if(0 == readnum)
{
continue;
}
else
{
printf("the server receive:%s\n",buf);
}
}
pthread_exit(NULL);
}
//**************************************************************
//**************************主函数*******************************
int main()
{
int sockfd,clientfd;
int sin_size,res,ret;
int readnum,writenum;
int fifo_read;
struct sockaddr_in server_addr,client_addr;
char buf[MAXSIZE];
pthread_t thread;

//建立sock套接字
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("the socker unist is error!\n");
exit(0);
}

bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORTNUM);

//端口绑定
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
printf("the bind error!\n");
exit(0);
}

//监听
if(listen(sockfd,5)==-1)
{
printf("the listen error!\n");
exit(0);printf("the client receive:%s\n",buf);
}

//等待客户端建立连接
bzero(&client_addr,sizeof(struct sockaddr_in));
sin_size=sizeof(struct sockaddr_in);
if((clientfd=accept(sockfd,(struct sockaddr *)(& client_addr),&sin_size))==-1)
{
printf("the connect error!\n");
exit(0);
}

//**************************************************************************************
//**************************建立读线程****************************************************
res=pthread_create(&thread,NULL,Thrd_Read,(void *)clientfd);
if(res!=0)
{
printf("cread pthread error!\n");
exit(res);
}
else
{
printf("the Thrd_Read success!\n");
}

//*******************************************************************************************
//*******************************主进程处理函数*************************************************
while(1)
{
bzero(buf,MAXSIZE);
printf("input your message!\n");
fgets(buf,MAXSIZE,stdin);
if((writenum=send(clientfd,buf,strlen(buf),0))==-1)
{
printf("write error!\n");
exit(0);
}
printf("the server have write %d\n",writenum);
}

close(clientfd);
close(sockfd);
exit(0);
return 0;
}


                                                            //client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define MAXSIZE 255
#define PORTNUM 5555
#define THREAD_NUMBER 2

//读函数
//void *Thrd_Read(void *ar)
//{
// int clientfd=(int)ar;
// char buf[MAXSIZE];
// int readnum;
// while(1)
// {
// bzero(buf,MAXSIZE);
// readnum=recv(clientfd,buf,MAXSIZE,0);
// if(readnum==-1)
// {
// printf("the read error!\n");
// break;
// // close(arg.clientfd);
// // exit(0);
// }
// else if(0 == readnum)
// {
// continue;
// }
// else
// {
// printf("the client receive:%s\n",buf);
// }
// }
// pthread_exit(NULL);
//}

//写函数
void *Thrd_Write(void *ar)
{
int clientfd=(int)ar;
char buf[MAXSIZE];
int writenum;
while(1)
{
bzero(buf,MAXSIZE);
printf("input your message!\n");
fgets(buf,MAXSIZE,stdin);
if((writenum=send(clientfd,buf,strlen(buf),0))==-1)
{
printf("write error!\n");
exit(0);
}
printf("the server have write %d\n",writenum);
}
pthread_exit(NULL);
}


int main(int argc,char *argv[])
{
int clientfd;
int writenum,readnum;
int sin_size,res;
struct in_addr;
struct sockaddr_in client_addr;
struct hostent *host;
char buf[MAXSIZE];
pthread_t thread_write;


//直接绑定服务器端口地址
//char *host = "127.0.0.1";
//inet_aton(host,&client_addr.sin_addr);

//建立socket连接
if((clientfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("the client socket error!\n");
exit(0);
}

//手动输入服务器端地址
if(inet_aton(argv[1],&client_addr.sin_addr)!=0)
{
if( (host = gethostbyaddr((char *)&client_addr.sin_addr, 4, AF_INET) ) == NULL )
{
printf("gethostbyaddr error for addr\n");
return 1; /* 如果调用gethostbyaddr发生错误,返回1 */
}
}
else
{
if((host=gethostbyname(argv[1]))==NULL)
{
printf("the get host error!\n");
exit(0);
}
//client_addr.sin_addr=*((struct in_addr *)host->h_addr));

}

bzero(&client_addr,sizeof(struct sockaddr_in));
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(PORTNUM);

sin_size=sizeof(struct sockaddr_in);

if(connect(clientfd,(struct sockaddr *)(&client_addr),sizeof(struct sockaddr))==-1)
{
printf("the connect error!\n");
close(clientfd);
exit(0);
}

printf("befing to creat pthread!\n");

//*********************************************************************************
//******************************创建读操作线程****************************************
res=pthread_create(&thread_write,NULL,Thrd_Write,(void *)clientfd);
if(res!=0)
{
printf("cread pthread error!\n");
exit(res);
}
else
{
printf("the Thrd_Write success!\n");
}

//*******************************************************************
//**********************写操作****************************************
while(1)
{
bzero(buf,MAXSIZE);
readnum=recv(clientfd,buf,MAXSIZE,0);
if(readnum==-1)
{
printf("the read error!\n");
break;
// close(arg.clientfd);
// exit(0);
}
else if(0 == readnum)
{
continue;
}
else
{
printf("the client receive:%s\n",buf);
}
}

close(clientfd);
exit(0);
return 0;
}

编译:
gcc -o server server.c -lpthread
gcc -o client client.c -lpthread
运行:
1.先运行./server
2.再运行./client 127.0.0.1
阅读(370) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~