server.c
`#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3333
#define CLIENTFD 6
#define MAX_SIZE 1024
struct message
{
int len;
char buf[MAX_SIZE];
int clientfd[CLIENTFD];
};
struct message msg;
void *send_handle(void *arg)
{
mqd_t mqd;
FILE *fp;
struct mq_attr attr;
int len;
int i;
int fd = *((int *)arg);
char buffer[MAX_SIZE];
msg.clientfd[msg.len++] = fd;
printf("msg.len = %d\n",msg.len);
mqd = mq_open("/helloworld",O_CREAT|O_RDWR,0666,NULL);
if(mqd == -1)
{
perror("mqd error");
exit(EXIT_FAILURE);
}
fp = fopen("timg.jpeg","rb+");
if(fp == NULL)
{
perror("open error");
exit(EXIT_FAILURE);
}
while(1)
{
printf("please input somewords\n");
scanf("%s",buffer);
for(i = 1; i < msg.len; i ++)
{
while(1)
{
len = fread(msg.buf,1,sizeof(msg.buf),fp);
printf("len = %d\n",len);
mq_send(mqd,(char *)&msg,sizeof(msg),i);
if(mq_send < 0)
{
perror("mq_send error");
exit(EXIT_FAILURE);
}
if(len == 0)
{
printf("fwrite over\n");
break;
}
}
fseek(fp,0,SEEK_SET);
}
}
}
int main()
{
int sockfd;
int new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int n_read;
int ser_size;
int opt = 1;
pthread_t id;
char buffer[MAX_SIZE];
msg.len = 1;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror("socket error!\n");
exit(1);
}
printf("socket success.............!\n");
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)) < 0)
{
perror("set socket option error!\n");
exit(1);
}
bzero(&server_addr,0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("192.168.1.123");
if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof server_addr) < 0)
{
perror("bind error!\n");
exit(1);
}
printf("bind success.............!\n");
listen(sockfd,5);
printf("listen success.............!\n");
printf("accepting..................!\n");
while(1)
{
ser_size = sizeof client_addr;
if((new_fd = accept(sockfd,(struct sockaddr *)&client_addr,&ser_size)) < 0)
{
perror("accept error!\n");
exit(1);
}
if(pthread_create(&id,NULL,(void *)send_handle,&new_fd) != 0)
{
perror("pthread_create fail");
}
printf("accept success.................!\n");
}
}`
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3333
#define CLIENTFD 6
#define MAX_SIZE 1024
struct message
{
int len;
char buf[MAX_SIZE];
int clientfd[CLIENTFD];
};
struct message msg;
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Please input server ip!\n");
exit(1);
}
int sockfd;
int n_write;
unsigned int sender;
mqd_t mqd;
time_t timep;
struct tm *p;
FILE *fp;
struct mq_attr attr;
struct sockaddr_in server_addr;
int len;
char name[MAX_SIZE] = {0};
time(&timep);
p = localtime(&timep);
sprintf(name,"%02d%02d%02d",p->tm_hour,p->tm_min,p->tm_sec);
strcat(name,".jpeg");
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror("client socket error!\n");
exit(1);
}
bzero(&server_addr,0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof server_addr) < 0)
{
perror("connect server error!\n");
exit(1);
}
mqd = mq_open("/helloworld",O_CREAT|O_RDWR,0666,NULL);
if(mqd == -1)
{
perror("mqd error");
exit(EXIT_FAILURE);
}
if(mq_getattr(mqd,&attr)<0)
{
perror("mq_getattr error");
exit(EXIT_FAILURE);
}
fp = fopen(name,"wb+");
if(fp == NULL)
{
perror("open error");
exit(EXIT_FAILURE);
}
while(1)
{
len = mq_receive(mqd,(char *)&msg,attr.mq_msgsize,&sender);
if(len == -1)
{
perror("mq_receive fail");
exit(EXIT_FAILURE);
}
printf("len = %d,sender = %d\n",len,sender);
fwrite(msg.buf,1,sizeof(msg.buf),fp);
fflush(fp);
}
}