我现在写一个服务端程序,要求可以链接多个客户端
有报警信息时要能发送报警信息,如果客户端有数据发来时,接收客户端的信息
现在运行起来有问题,当我打开第一个客户端时运行正常,当我断开这个链接,再重新打开一个客户端时,发送时就出现了问题,情况如下:
第一个链接
There is a new socket link coming 4!
alarmtype = 50
data from socket 4, bytes = 1212
data from socket 4, bytes = 1212
Socket link 4 disconnect!
第二个链接
There is a new socket link coming 5!
alarmtype = 50
send error
: Bad file descriptor
data from socket 5, bytes = 1212
data from socket 5, bytes = 1212
代码如下:
void *sockalarm(void *arg)
{
int sdbytes = 0;
int cnfd = (int)arg;
int connfd;
rtcp_hdr_t rtcp_head;
warn_hdr_t pkt;
struct sockaddr_in cliaddr;
socklen_t cliaddr_len;
pthread_t rcvthread[pthread_count];
pthread_t sendthread[pthread_count];
struct sockaddr_in servaddr, cliaddr;
int sockfd;
pthread_t sockthread;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(sockfd, MAX_NUM);
printf("Accepting connections ...\n");
cliaddr_len = sizeof(cliaddr);
while(1)
{
if(pthread_count < MAX_PTHR) {
if( (connfd = accept(cnfd, (struct sockaddr *)&cliaddr, &cliaddr_len)) < 0 ) {
perror("socket accept error\n");
} else {
printf("There is a new socket link coming %d!\n", connfd);
if( pthread_create(&rcvthread[pthread_count], NULL, rcvptz, (void *)connfd) ) {
perror("rcv pthread_create\n");
}
if( pthread_create(&sendthread[pthread_count], NULL, sendalarm, (void *)connfd) ) {
perror("send pthread_create\n");
}
pthread_count += 1;
}
} else {
printf("thread count has big enough!\n");
break;
}
}
pthread_exit(NULL);
}
void *sendalarm(void *arg)
{
int sdbytes = 0;
int cnfd = (int)arg;
rtcp_hdr_t rtcp_head;
warn_hdr_t pkt;
while (1)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
memset(&pkt, 0, sizeof(warn_hdr_t));
pkt.rtcp_head.ver = 2;
pkt.rtcp_head.pt = 204;
pkt.rtcp_head.len = 1200;
pkt.rtcp_head.ssrc = 11;
pkt.rtcp_head.command = 2;
pkt.head = 0xDDCC;
pkt.len = 1188;
printf("alarmtype = %d\n", alarmtype);
pkt.type = alarmtype;
sdbytes = send(cnfd, (char *)&pkt, RTP_LEN, MSG_DONTWAIT);
if(sdbytes < 0) {
perror("send error\n");
break;
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
close(cnfd);
pthread_exit(NULL);
}
void *rcvptz(void *arg)
{
int i;
int rcvbytes = 0;
rtcp_hdr_t rtcp_head;
warn_hdr_t pkt;
int cnfd = (int)arg;
char sockbuf[RTP_LEN] = {0};
char urtbuff[MAX_DATA] = {0};
char netbuff[MAX_DATA] = {0};
while (1)
{
rcvbytes = recv(cnfd, sockbuf, RTP_LEN, 0);
if(rcvbytes > 0) {
printf("data from socket %d, bytes = %d\n", cnfd, rcvbytes);
if( rcvbytes > 12 ) {
rtcp_head = *(rtcp_hdr_t *)sockbuf;
if((rtcp_head.ver == 2) && (rtcp_head.pt == 204) && (rtcp_head.command == 1)) {
for(i = 0; i < (rcvbytes - 12); i++) {
urtbuff[i] = sockbuf[12+i];
// printf("%02x ", urtbuff[i]);
}
} else {
printf("the header of rtcp pcket is wrong\n");
}
}
} else if(rcvbytes == 0) {
printf("Socket link %d disconnect!\n", cnfd);
break;
} else {
printf("Socket %d \n", cnfd);
perror("rcv error!\n");
break;
}
sleep(1);
}
close(cnfd);
pthread_exit(NULL);
}