在linux下如何实现一个服务器端监听到客户端发送数据包(简单的实现方式就行)

瞌睡鱼 2009-02-06 08:43:05
希望那位大虾指点一下
...全文
256 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
waizqfor 2009-02-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenqiang35 的回复:]
用select来探测

C/C++ code
1. 创建socket;

2. my_addr.sin_family = AF_INET; /*sin_family is unsigned short, host byte order*/
my_addr.sin_port = htons(SERVERPORT);/*unsigned short, network byte order*/
my_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr(INADDR_ANY); /*auto-fill with my IP*/
bzero(&(my_addr.sin_zero), 8); /*zero the rest of the struct*/

3. bind;


[/Quote]
学习 !~`
csgdseed 2009-02-06
  • 打赏
  • 举报
回复
很详细了
chenqiang35 2009-02-06
  • 打赏
  • 举报
回复
用select来探测

1. 创建socket;

2. my_addr.sin_family = AF_INET; /*sin_family is unsigned short, host byte order*/
my_addr.sin_port = htons(SERVERPORT);/*unsigned short, network byte order*/
my_addr.sin_addr.s_addr = INADDR_ANY;//inet_addr(INADDR_ANY); /*auto-fill with my IP*/
bzero(&(my_addr.sin_zero), 8); /*zero the rest of the struct*/

3. bind;

4. listen;

5. /* 把集合清空 */(这个是重点,每一次while循环都应清空fd_set集合,再把相应的句柄加入fd_set)
FD_ZERO(&rfds);

/* 把标准输入句柄0加入到集合中 */
FD_SET(0, &rfds);
maxfd = 0;

/* 把sockfd句柄加入到集合中 */
FD_SET(sockfd, &rfds);
if(sockfd > maxfd)
maxfd = sockfd;

/*把clientfd加入到rfds集合中*/
for(j = 0; j < currentid; j++)
{
FD_SET(clientfd[j], &rfds);

if(clientfd[j] > maxfd)
{
maxfd = clientfd[j];
}
}

6. /* 开始等待 */
retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);

7. for(j = 0; j < currentid; j++)//遍历每个已经客户端

8. if(FD_ISSET(sockfd, &rfds))//判断select探测的sockfd描述符是否发生变化,若是变化则,clientfd[currentid] = accept(sockfd, (struct sockaddr *)&clientaddr, &sin_size );

9. if(FD_ISSET(clientfd[j], &rfds))判断select探测的clientid描述符是否发生变化;

瞌睡鱼 2009-02-06
  • 打赏
  • 举报
回复
/*
** server.c -- a stream socket server demo
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT 3490 // the port users will be connecting to

#define BACKLOG 10 // how many pending connections queue will hold

void sigchld_handler(int s)
{
while(wait(NULL) > 0);
}

int main(void)
{
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;
struct sigaction sa;
int yes=1;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}

while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}

return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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