I want to write my own little chat server in C on a MacOS machine. Now I want to connect to all clients, that are online and let the connection open, to be able to receive and send messages. The problem is that I only know, how to have one socket connection at a time open. So only one client can connect so far and chatting like that is kinda boring ;)
我想在MacOS机器上用C编写自己的小聊天服务器。现在我想连接到所有在线客户端并打开连接,以便能够接收和发送消息。问题是我只知道,如何一次打开一个套接字连接。所以到目前为止只有一个客户端可以连接,这样聊天有点无聊;)
7 个解决方案
#1
3
The simplest solution for a small chat server is probably to use select() or pselect().
对于小型聊天服务器来说,最简单的解决方案可能是使用select()或pselect()。
Have a look at the excellent Beej's Guide to Network Programming. In his select() tutorial, he builds a small chat server.
看看优秀的Beej网络编程指南。在他的select()教程中,他构建了一个小型聊天服务器。
#2
1
One option is to use multithreading with the pthreads library. Another option is to use asynchronous I/O with the select(2)
call. With select(2)
, you open a bunch of sockets, and then you can poll each one to see if it has data. If it has data, you read it, otherwise you move on to the next socket.
一种选择是对pthreads库使用多线程。另一种选择是使用select(2)调用的异步I / O.使用select(2),打开一堆套接字,然后您可以轮询每个套接字以查看它是否有数据。如果它有数据,则读取它,否则转到下一个套接字。
#3
1
Since Mac OS X is based on FreeBSD, for the best and most efficient program you should use kqueue
由于Mac OS X基于FreeBSD,为了获得最佳和最有效的程序,你应该使用kqueue
#4
0
Basically you need to have a listening socket on your chosen port. Once a connection is established to the listening socket, you need to open a new socket on a different port number and hand the client over to this new socket. It will be best to try and use a pre-written socket library as rolling your own here is going to be a complex process.
基本上你需要在你选择的端口上有一个监听套接字。一旦与侦听套接字建立连接,您需要在另一个端口号上打开一个新套接字,并将客户端交给这个新套接字。最好尝试使用预先编写的套接字库,因为在这里滚动自己将是一个复杂的过程。
Try searching http://sourceforge.net for some sample libraries.
尝试在http://sourceforge.net上搜索一些示例库。
#5
0
There is no problem to have multiple connected socket in one program, and you don't need to mess with multithreading. Just keep opening connections as you used to. If all your clients connects to the same listener, just do not close the listener after accept()
- it will keep listening for more incoming connections.
在一个程序中有多个连接的套接字是没有问题的,你不需要搞乱多线程。只需像以前一样保持打开连接即可。如果所有客户端都连接到同一个侦听器,则不要在accept()之后关闭侦听器 - 它将继续侦听更多传入连接。
Use select()
or poll()
to check for incoming data on all opened sockets. Do not forget to included listening socket into listed of descriptors for select()
- incoming connection is an event select()
detects.
使用select()或poll()检查所有打开的套接字上的传入数据。不要忘记将listen socket包含在select()的描述符列表中 - 传入连接是事件select()检测。
It is really very simple. No rocket science.
这真的很简单。没有火箭科学。
#6
0
you may also use fork-on-accept, like this:
你也可以使用fork-on-accept,如下所示:
int listen_fd, new_fd;
while ((new_fd = accept(listen_fd, NULL, NULL)) != -1) {
if (fork())
close(new_fd);
else
// handle client connection
}
#7
0
take a look at select, pselect and poll.
看看select,pselect和poll。
I've never use them my self, but I suspect they are for what you want to do.
我从来没有把它们用在我自己身上,但我怀疑它们是为了你想要做的。
#1
3
The simplest solution for a small chat server is probably to use select() or pselect().
对于小型聊天服务器来说,最简单的解决方案可能是使用select()或pselect()。
Have a look at the excellent Beej's Guide to Network Programming. In his select() tutorial, he builds a small chat server.
看看优秀的Beej网络编程指南。在他的select()教程中,他构建了一个小型聊天服务器。
#2
1
One option is to use multithreading with the pthreads library. Another option is to use asynchronous I/O with the select(2)
call. With select(2)
, you open a bunch of sockets, and then you can poll each one to see if it has data. If it has data, you read it, otherwise you move on to the next socket.
一种选择是对pthreads库使用多线程。另一种选择是使用select(2)调用的异步I / O.使用select(2),打开一堆套接字,然后您可以轮询每个套接字以查看它是否有数据。如果它有数据,则读取它,否则转到下一个套接字。
#3
1
Since Mac OS X is based on FreeBSD, for the best and most efficient program you should use kqueue
由于Mac OS X基于FreeBSD,为了获得最佳和最有效的程序,你应该使用kqueue
#4
0
Basically you need to have a listening socket on your chosen port. Once a connection is established to the listening socket, you need to open a new socket on a different port number and hand the client over to this new socket. It will be best to try and use a pre-written socket library as rolling your own here is going to be a complex process.
基本上你需要在你选择的端口上有一个监听套接字。一旦与侦听套接字建立连接,您需要在另一个端口号上打开一个新套接字,并将客户端交给这个新套接字。最好尝试使用预先编写的套接字库,因为在这里滚动自己将是一个复杂的过程。
Try searching http://sourceforge.net for some sample libraries.
尝试在http://sourceforge.net上搜索一些示例库。
#5
0
There is no problem to have multiple connected socket in one program, and you don't need to mess with multithreading. Just keep opening connections as you used to. If all your clients connects to the same listener, just do not close the listener after accept()
- it will keep listening for more incoming connections.
在一个程序中有多个连接的套接字是没有问题的,你不需要搞乱多线程。只需像以前一样保持打开连接即可。如果所有客户端都连接到同一个侦听器,则不要在accept()之后关闭侦听器 - 它将继续侦听更多传入连接。
Use select()
or poll()
to check for incoming data on all opened sockets. Do not forget to included listening socket into listed of descriptors for select()
- incoming connection is an event select()
detects.
使用select()或poll()检查所有打开的套接字上的传入数据。不要忘记将listen socket包含在select()的描述符列表中 - 传入连接是事件select()检测。
It is really very simple. No rocket science.
这真的很简单。没有火箭科学。
#6
0
you may also use fork-on-accept, like this:
你也可以使用fork-on-accept,如下所示:
int listen_fd, new_fd;
while ((new_fd = accept(listen_fd, NULL, NULL)) != -1) {
if (fork())
close(new_fd);
else
// handle client connection
}
#7
0
take a look at select, pselect and poll.
看看select,pselect和poll。
I've never use them my self, but I suspect they are for what you want to do.
我从来没有把它们用在我自己身上,但我怀疑它们是为了你想要做的。