socket服务器<tcp>不能正确收发消息!!!!!!!
问题描述:服务器接收客户端消息1次——再发消息不能接收——等待客户端接收了服务器的消息——服务器才可接受消息。————客户端也是这个问题——望各位赐教!!!!!急!!!急!!!#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>
#include <time.h>
#include <arpa/inet.h>
#include <lib.h>
#define SERV_PORT 6000
#define BACK_LOG 5
#define MAX_LINE BUFSIZ
#define SERVADDR "192.168.1.229"
typedef struct cli_node
{
int clifd;
struct cli_node *next;
} cli_node, *cli_list;
/*add a client's fd*/
cli_list add_cli(cli_list head, int fd)//建立链表
{
cli_list p = head;
cli_list new_cli = (cli_list) malloc(sizeof(cli_node));
while (p->next)
p = p->next;
new_cli->clifd = fd;
new_cli->next = NULL;
p->next = new_cli;
return (head);
}
/*delete a client's fd*/
cli_list delete_cli(cli_list head, int fd)//删除链表
{
cli_list p = head, q = head->next;
while (q->clifd != fd) {
p = q;
q = q->next;
}
p->next = q->next;
close(fd);
free(q);
return (head);
}
/*main*/
int main(int argc, char **argv)
{
/*definition */
int i, ofd, listenfd, connfd = 0,f;
cli_list head, ph, qh;
int b_reuse = 1;
socklen_t len;
struct timeval timeout={3,0};
struct sockaddr_in servaddr, cliaddr;
char buf[MAX_LINE], addr[15];
fd_set readset;
head = (cli_list) malloc(sizeof(cli_node));
head->next = NULL;
for (i = 3; i < sysconf(_SC_OPEN_MAX); i++)//?
close(i);
listenfd = socket(AF_INET, SOCK_STREAM, 0); // listen socket
if(listenfd < 0) {
fprintf(stderr,"socket() error!, listenfd = %d\n", listenfd);
exit(1);
}
head->clifd = listenfd;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof(int));//设置套接口选项层
bzero(&servaddr, sizeof(servaddr));
/*set servaddr */
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr=inet_addr(SERVADDR);
/*bind the listener to the local port */
if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) != 0) {
perror("bind");
exit(1);
}
/*ok, now listen */
listen(listenfd, BACK_LOG);
while (1) {
/*initialize */
FD_ZERO(&readset);//将readset集合清零
FD_SET(listenfd, &readset);//将服务器创建的套接字加入readset集合
FD_SET(connfd,&readset);
FD_SET(ofd,&readset);
/*add file describtions to fdset */
ph = head;
while (ph->next) {
FD_SET(ph->next->clifd, &readset);//将链表加入readset集合
ph = ph->next;
}
/*creat a select-list */
select((ph->clifd) + 1, &readset, NULL, NULL,&timeout);
/*add a new connection to the select-list */
if (FD_ISSET(listenfd, &readset)) {//测试listenfd是否在集合内
len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len);
head = add_cli(head, connfd);
continue;
}
/*proccess an eixsted connetction */
for (connfd = (listenfd + 1); connfd <= (ph->clifd); connfd++) {
if (FD_ISSET(connfd, &readset)) {
fprintf(stderr,"server: get a socket data:");
if (recv(connfd, buf, MAX_LINE, 0) <= 0) { // something is wrong! delete it!
head = delete_cli(head, connfd);
break;
} else {
fprintf(stderr,"%s\n", buf);
}
for (ofd = listenfd + 1; ofd <= ph->clifd; ofd++) {
if (ofd != connfd); // connfd is the current active socket
if (FD_ISSET(ofd,&readset)){
scanf ("%s",buf);
if (send(ofd, buf, MAX_LINE, 0)<0) // ofd is "other fd"
{
head=delete_cli(head,ofd);
break;
}
}
}
}
}
}
}