socket 聊天室实现

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;
}
时间: 2024-10-15 06:58:11

socket 聊天室实现的相关文章

Web Socket 聊天室

Web sockets test Web Socket 聊天室 按下连接按钮,会通过WebSocket发起一个到聊天浏览器的连接. 服务器地址: 用户名: 连接 发送 来自网上.............

python socket 聊天室

import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #绑定端口 s.bind(("127.0.0.1", 8888)) while True: data = s.recvfrom(1024) print(str(data[0].decode("gbk"))) send_data = input("请输入聊天内容") if "exit" in se

socket聊天室

1 #服务端 2 from socket import * 3 import json 4 def recvMsg(s): 5 while True: 6 #接收用户的信息 7 data,address = s.recvfrom(1024) 8 data = json.loads(data) 9 print(data,address) 10 11 if data['type'] == 'enter': 12 # 将用户进入聊天室的信息发给其它所有在线用户 13 sendToAll(('>>系统

【Unity3D自学记录】Unity3D网络之Socket聊天室初探

首先创建一个服务端程序,这个程序就用VS的控制台程序做即可了. 代码例如以下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace SocketServer { class Program { const int Port = 20000; //设置连接port static void Main(strin

socket聊天室(服务端)(多线程)(TCP)

#include<string.h> #include<signal.h> #include<stdio.h> #include<sys/socket.h> #include<stdlib.h> #include<netdb.h> #include<pthread.h> #include<memory.h> #include<semaphore.h> int Thread_num=0,count=0

Socket聊天室-TcpListener,TcpClient

参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端: 1 #region 属性 2 3 //请求的客户端连接 4 Socket clientsocket; 5 //当前连接集合 6 List<Client> clients; 7 //请求客户端线程 8 Thread clientservice; 9 //服务器监听线程 10 Thread threadListen; 11 //服务器监听连接 12 TcpListener

socket 聊天室

服务端: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Thre

PHP+Socket聊天室(telnet命令调试)

class socket { private $_socket; private $_domain = AF_INET; private $_type = SOCK_STREAM; private $_protocol = SOL_TCP; private $_clients = []; private $_max_clients = 10; public function __construct() { if (!extension_loaded('sockets')) { die('the

Python Socket 编程——聊天室演示样例程序

上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket 编程的理解. 聊天室程序需求 我们要实现的是简单的聊天室的样例,就是同意多个人同一时候一起聊天.每一个人发送的消息全部人都能接收到,类似于 QQ 群的功能,而不是点对点的 QQ 好友之间的聊天.例如以下图: 图来自:http://www.ibm.com/developerworks/linux/tu