| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 506 人关注过本帖
标题:socket服务器<tcp>不能正确收发消息!!!!!!!
取消只看楼主 加入收藏
s3c11
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-2-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
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;
                            }
                        }
                    }
                }
            }
        }
    }                           
搜索更多相关主题的帖子: 服务器 客户端 include 
2012-02-28 13:25
快速回复:socket服务器<tcp>不能正确收发消息!!!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015716 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved