|
楼主 |
发表于 2006-3-29 08:35:56
|
显示全部楼层
多谢版主和aliff兄指点!!
按照这句话“在一个已经连接的socket上面accept可以看socket里面是否有数据可以读或者写”修改程序后实现了同时监听两个端口的要求!
修改后的程序如下:
修改三处,已在修改处标注。
留做以后遗忘备查:)
[PHP]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>
//#include <fcntl.h>
int
main (void)
{
int s1, s2;
int ss1, ss2;
int n;
int rv;
char buf[256];
struct sockaddr_in recv_addr;
struct sockaddr_in recv_remote_addr;
struct sockaddr_in send_addr;
struct sockaddr_in send_remote_addr;
socklen_t sin_size;
int yes = 1;
fd_set readfds;
fd_set writefds;
struct timeval tv;
if ((s1 = socket (PF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("socket s1");
exit (1);
}
//fcntl(s1, F_SETFL, O_NONBLOCK);
if ((s2 = socket (PF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("socket s2");
exit (1);
}
//fcntl(s2, F_SETFL, O_NONBLOCK);
if (setsockopt (s1, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1)
{
perror ("setsockopt s1");
exit (1);
}
if (setsockopt (s2, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1)
{
perror ("setsockopt s2");
exit (1);
}
recv_addr.sin_family = AF_INET;
recv_addr.sin_port = htons (3490);
//recv_addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
inet_aton ("127.0.0.1", &(recv_addr.sin_addr));
memset (&(recv_addr.sin_zero), '\0', 8);
send_addr.sin_family = AF_INET;
send_addr.sin_port = htons (9034);
//send_addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
inet_aton ("127.0.0.1", &(send_addr.sin_addr));
memset (&(send_addr.sin_zero), '\0', 8);
if (bind (s1, (struct sockaddr *) &recv_addr, sizeof (struct sockaddr)) ==
-1)
{
perror ("bind s1");
exit (1);
}
if (bind (s2, (struct sockaddr *) &send_addr, sizeof (struct sockaddr)) ==
-1)
{
perror ("bind s2");
exit (1);
}
//clear the set ahead of time
FD_ZERO (&readfds);
FD_ZERO (&writefds);
//listen (s1, 10);
//listen (s2, 10);
//add our descriptors to the set
//FD_SET (s1, &readfds);
//FD_SET (s2, &writefds);
//since we got s2 second, it's the "greater",
//so we use that for the n param in select()
//n = s2 + 1;
//tv.tv_sec = 2;
//tv.tv_usec = 1000000;
//监听s1,s2看s1是否有读请求,s2是否有写请求,然后分别响应请求
for (;;)
{
listen (s1, 10);
listen (s2, 10);
FD_SET (s1, &readfds);
FD_SET (s2, &readfds); //writefds-->readfds
n = s2 + 1;
tv.tv_sec = 2;
tv.tv_usec = 1000000;
rv = select (n, &readfds, NULL, NULL, &tv); //&writefds-->>NULL
if (rv == -1)
{
perror ("select"); //error occurred in select()
}
else if (rv == 0)
{
printf ("Timeout occurred! No data after 3 seconds.\n");
}
else
{
if (FD_ISSET (s1, &readfds))
{
if ((ss1 =
accept (s1, (struct sockaddr *) &recv_remote_addr,
&sin_size)) == -1)
{
perror ("accept ss1");
}
if (recv (ss1, buf, sizeof (buf), 0) == -1)
{
perror ("recv ss1");
}
else
{
printf ("We got: %s\n", buf);
}
close (ss1);
}
if (FD_ISSET (s2, &readfds)) //writefds-->readfds
{
if ((ss2 =
accept (s2, (struct sockaddr *) &send_remote_addr,
&sin_size)) == -1)
{
perror ("accept ss2");
}
if (send (ss2, "Hello world!\n", 14, 0) == -1)
//if (send (ss2, buf, sizeof (buf), 0) == -1)
{
perror ("send ss2");
}
else
{
printf ("We send: %s\n", buf);
}
close (ss2);
}
}
}
return 0;
}
[/PHP] |
|