日期:2014-05-16  浏览次数:20758 次

模拟聊天程序//求高人修改!
C/C++ code

//服务器程序
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <semaphore.h>
int fds[100];//保存所有的客户端的socket描述符,最大100
int size=0;
sem_t lock;
void sendtoall(const char* msg,int rcvfd)//向所有人发送信息函数
{
        int i;
        sem_wait(&lock);
        for(i=0; i<size; i++)
        {
          if(fds[i]!=rcvfd)
             send(fds[i], msg, strlen(msg), MSG_NOSIGNAL);
        }
     sem_wait(&lock);
}
void* task(void* p)//子进程函数
{
        int sd = *(int*)p;
        char buf[100] = {};
        while(1)
                  {

                memset(buf, 0, sizeof(buf));
                recv(sd, buf, sizeof(buf), 0);
                sem_wait(&lock);
                sendtoall(buf,sd);//发送给所有在fds数组内的客户端
                sem_post(&lock);
                  }
}

int main()
{
        sem_init(&lock,0,1);//初始化锁变量
        int sockfd = socket(PF_INET, SOCK_STREAM, 0);//获取sockfd,
        if(sockfd==-1)perror("创建socket失败"),exit(-1);

   struct sockaddr_in addr;//建立网络信息
        addr.sin_family = PF_INET;
        addr.sin_port = htons(8888);
        addr.sin_addr.s_addr = inet_addr("172.18.7.72");

   int res = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));//绑定
        if(res==-1)perror("绑定失败"),exit(-1);
        printf("绑定成功\n");

   listen(sockfd, 10);//监听

        struct sockaddr_in fromaddr={};
        socklen_t len;
        int sd,sd_temp;//每次while(1)循环,sd的值,会不断改变,故保存在sd_temp变量中
        while(1)
               {
                sd = accept(sockfd, (struct sockaddr*)&fromaddr,&len);//与客户端建立联系
                if(sd==-1)
               {
                        perror("等待客户端连接失败");
                        continue;
                }
                fds[size++] = sd;//客户端socket ++

            pthread_t tid;//建立子进程
                sd_temp = sd;
                pthread_create(&tid, 0, task, &sd_temp);
               }
     sem_destroy(&lock);
}




C/C++ code


//客户端
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>

void* task(void* p)//子进程函数
{
        int sd = *(int*)p;
        char buf[100] = {};
        while(1)
       {
                memset(buf, 0, sizeof(buf));
                recv(sd, buf, sizeof(buf), 0);
                printf("%s\n", buf);
        }
}

int main()
{

        int sockfd = socket(PF_INET, SOCK_STREAM, 0);//创建socket
        if(sockfd==-1)perror("创建socket失败"),exit(-1);

        struct sockaddr_in addr;//建立通信地址
        addr.sin_family = PF_INET;
        addr.sin_port = htons(8888);
        addr.sin_addr.s_addr = inet_addr("172.18.7.72");

        int res = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));//连接服务器
        if(res==-1)perror("连接失败"),exit(-1);
        printf("连接成功\n");

        pthread_t tid;//建立子进程
        pthread_create(&tid, 0, task, &sockfd);
        printf("请输入你的姓名:");//本机参与聊天代码
        char name[20] = {};
        scanf("%s", name);
        while(1)
        {
                scanf("%*c");
           char buf[100] = {};
                printf("输入内容:");
                scanf("%s", buf);
                char msg[120] = {};
                strcpy(msg, name);
                strcat(msg, ":");
                strcat(msg, buf);
                send(sockfd, msg, strlen(msg)