网络编程中如何响应多个用户的请求

darcymei 2004-08-31 11:37:03
我打算开子线程来accept用户的请求 不知用这样的方法是不是只要简单在accept函数调用开一个子线程。
如果不是 应该怎么做 请各位多指教,谢谢。
...全文
156 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pacman2000 2004-08-31
  • 打赏
  • 举报
回复
看书吧,<unix网络编程>里,好几种c/s模型都写得很清楚了。
tibet 2004-08-31
  • 打赏
  • 举报
回复
这样也是可以的
lijiangshui 2004-08-31
  • 打赏
  • 举报
回复
还是看unix网络编程吧,里面说得再清楚不过了
cenlu99 2004-08-31
  • 打赏
  • 举报
回复
共享?所有子进程都copy了一份父进程的数据,不是共享。
darcymei 2004-08-31
  • 打赏
  • 举报
回复
to yanghuajia(newperson)
你这样是所有子进程共享一个缓冲区 怎么来区分与各个不同的client交互呢。
smaxll 2004-08-31
  • 打赏
  • 举报
回复
why do u like electronic book ?
for types of classical text ,i recommend u to buy paper editon
u will get enjoy with it
yanghuajia 2004-08-31
  • 打赏
  • 举报
回复
//最好有本网络编程的书。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define MYPORT 3490 /* the port users will be connecting to */
#define BACKLOG 10 /* how many pending connections queue will hold */

main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
peror("socket");
e x i t ( 1 ) ;
}
m y _ a d d r.sin_family = AF_INET; /* host byte order */
m y _ a d d r.sin_port = htons(MYPORT); /* short, network byte order */
m y _ a d d r. s i n _ a d d r.s_addr = INADDR_ANY; /* auto-fill with my IP */
b z e r o ( & ( m y _ a d d r.sin_zero), 8); /* zero the rest of the struct */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
== -1) {
perror ( " b i n d " ) ;
exit( 1 ) ;
}
if (listen(sockfd, BACKLOG) == -1) {
p e r r o r ( " l i s t e n " ) ;
e x i t ( 1 ) ;
}
while(1) { /* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \
&sin_size)) == -1) {
p e r r o r ( " a c c e p t " ) ;
c o n t i n u e ;
}
printf("server: got connection from %s\n", \
i n e t _ n t o a ( t h e i r _ a d d r. s i n _ a d d r ) ) ;
if (!fork()) { /* this is the child process */
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
p e r r o r ( " s e n d " ) ;
c l o s e ( n e w _ f d ) ;
e x i t ( 0 ) ;
}
close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
}
}
pacman2000 2004-08-31
  • 打赏
  • 举报
回复
网上有扫描版的,不是很清楚。 建议买纸版书好了。
在china-pub上找找看好了。
darcymei 2004-08-31
  • 打赏
  • 举报
回复
那还有什么别的做法吗
darcymei 2004-08-31
  • 打赏
  • 举报
回复
有这本书吗 google里没找到啊
能否给个链接

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧