1. 程式人生 > >socket 聊天室實現

socket 聊天室實現

socket sets urn rom accep host amp ret nbsp

server

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

void
handler(int sig) { printf("recv a sig=%d\n",sig); exit(1); } int main(void) { int listenfd; listenfd = socket(PF_INET,SOCK_STREAM,0); if(listenfd < 0) { printf("creat socket failed\n"); exit(1); } struct
sockaddr_in servaddr; memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(5188); servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//host any address int n=1; if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof
(n))<0) { printf("set resueaddr failed\n"); exit(1); } if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) { printf("bind fialed\n"); exit(1); } if(listen(listenfd,SOMAXCONN)<0) { printf("listen failed\n"); exit(1); } struct sockaddr_in peeraddr; socklen_t peerlen = sizeof(peeraddr); int conn; if((conn=accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen))<0) { printf("accept failed\n"); exit(1); } printf("ip=%s, port=%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port)); pid_t pid; pid = fork(); if (pid == -1) { printf("server fork failed\n"); exit(1); } if(pid == 0) //send to client data { char recvbuf[1024]; while (1) { memset(recvbuf,0 ,sizeof(recvbuf)); int ret = read(conn,recvbuf,sizeof(recvbuf)); if( ret == -1) { printf("server read failed\n"); exit(1); } else if (ret == 0) { printf("client close\n"); break; } fputs(recvbuf,stdout); } printf("parent close \n"); kill(pid,SIGUSR1); close(conn); exit(1); } else //recv from client data { signal(SIGUSR1,handler); char sendbuf[1024]={0}; while(fgets(sendbuf,sizeof(sendbuf),stdin) != NULL) { write(conn,sendbuf,strlen(sendbuf)); memset(sendbuf, 0 ,sizeof(sendbuf)); } printf("server childpid close\n"); close(conn); exit(1); } close(listenfd); return 0; }

client

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

void handler(int sig)
{
        printf("recv a sig=%d\n",sig);
        exit(1);
}

int main(void)
{
        int sock;
        sock = socket(PF_INET,SOCK_STREAM,0);
        if(sock < 0)
        {
                printf("creat socket failed\n");
                exit(1);
        }
        struct sockaddr_in servaddr;
        memset(&servaddr,0,sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(5188);
        //servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//host any address
        servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

        if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
        {
                printf("connect failed \n");
                exit(1);
        }
        pid_t pid;
        pid=fork();
        if(pid == -1)
        {
                printf("client fork failed\n");
                exit(1);
        }
        if(pid == 0) // recv from server data
        {
                char recvbuf[1024] = {0};
                while(1)
                {
                        memset(recvbuf,0,sizeof(recvbuf));
                        int ret = read(sock,recvbuf,sizeof(recvbuf));
                        if(ret == -1)
                        {
                                printf("read faileed\n");
                                exit(1);
                        }
                        else if(ret ==0)
                        {
                                printf("server close\n");
                                break;
                        }
                        fputs(recvbuf,stdout);
                }
                close(sock);
                kill(getppid(),SIGUSR1);
        }
        else //send to server data
        {
                signal(SIGUSR1,handler);
                char sendbuf[1024] = {0};
                while(fgets(sendbuf,sizeof(sendbuf),stdin) != NULL)
                {
                        write(sock, sendbuf,strlen(sendbuf));
                        memset(sendbuf,0,sizeof(sendbuf));
                }
                close(sock);
        }
        return 0;
}

socket 聊天室實現