多插槽听的Linux的Socket编程 [英] MultiSocket Listening Linux Socket Programing
问题描述
这是我写了一个简单code。服务器响应连接对端口2923,2924和2925
当我运行程序,只有从accpets PORT 2923. CONNECTION有人能帮助我走出了发球局。 Thanku
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; ARPA / inet.h>
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>
#包括LT&; SYS / time.h中>无效handle_client(INT cliend_fd)
{ INT RET =接受(cliend_fd,NULL,0);
如果(RET℃下)
{
的printf(接受错误。\\ n);
} 其他
{
的printf(客户端接受的\\ n);
关机(RET,2);
}
}诠释的main()
{
诠释计数= 3;
INT PORT = 2923; 结构sockaddr_in的地址;
INT MasterSocket [统计]
INT I = 0;
FD_SET readfds; INT maxfd;
maxfd = -1;
INT SelectSession; 结构体的timespec超时;
TimeOut.tv_sec = 2;
TimeOut.tv_nsec = 2; 对于(i = 0; I<计数;我++)
{
MasterSocket [I] =插座(AF_INET,SOCK_STREAM,0);
} 对于(i = 0; I<计数;我++)
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(127.0.0.1);
address.sin_port = PORT + I;
如果(绑定(MasterSocket [I],(结构sockaddr *)及地址的sizeof(地址))
< 0)
{
PERROR(绑定\\ n);
的getchar();
} 的printf(SockerDesriptor%d个---绑定%d个\\ N,MasterSocket [I],PORT + I);
} 对于(i = 0; I<计数;我++)
{
如果(听(MasterSocket [I],4)℃下)
{
PERROR(听);
的getchar();
//返回1;
}
其他
{
的printf(端口上侦听%d个--- \\ n,PORT + I);
}
} 而(1)
{
FD_ZERO(安培; readfds);
INT状态; 对于(i = 0; I<计数;我++)
{
FD_SET(MasterSocket [I],&放大器; readfds);
如果(MasterSocket [Ⅰ]≥maxfd)
{
maxfd = MasterSocket [I]
}
的printf(%d个添加到FD_SET描述%d个\\ n \\ n,PORT + I,
MasterSocket [I]);
} //状态= 0;
的printf(############等待连接\\ n);
状态= PSELECT(maxfd + 1,&安培; readfds,NULL,NULL,&放大器;超时,NULL);
如果(状态℃下)
{
PERROR(状态);
的getchar();
返回1;
} 否则,如果(状态== 0)
{
的printf(超时发生\\ n);
} 其他
{
//输出(状态%d个\\ N,地位);
SelectSession = -1;
对于(i = 0; I<计数;我++)
{
//的printf(检查设置%d个\\ N,I);
如果(FD_ISSET(MasterSocket [I],和放大器; readfds))
{
//输出(配套设置%d个\\ N,I);
SelectSession = MasterSocket [I]
的printf(SelectSession%d个\\ N,MasterSocket [I]);
如果(SelectSession == -1)
{
//关机(MasterSocket [I],2);
//继续;
打破;
}
其他
{
的printf(手柄\\ n);
handle_client(SelectSession);
的getchar();
} }
其他
{
的printf(没有在FD_ISSET \\ n); }
}
} / *为(i = 0; I<计数;我++)
{
关闭(MasterSocket [I],2);
}
* /
} 返回0;
}
从while循环中删除的getchar()
。
在 PSELECT
服务器等待得到一个角色,所以它不允许连接了客户端。
或者你有每个客户端连接后,服务器进入一个字符
和修改端口号,使用网络字节顺序 htons
程序:
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; ARPA / inet.h>
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>
#包括LT&; SYS / time.h中>无效handle_client(INT cliend_fd)
{
INT RET =接受(cliend_fd,NULL,0);
如果(RET℃下)
的printf(接受错误。\\ n);
其他{
的printf(客户端接受的\\ n);
关机(RET,2);
}
返回;
}诠释的main()
{
诠释计数= 3,PORT = 8000,选择= 1,i = 0;
INT MasterSocket [统计]
INT maxfd = -1,SelectSession;
FD_SET readfds;
结构sockaddr_in的地址; 结构体的timespec超时;
TimeOut.tv_sec = 2;
TimeOut.tv_nsec = 2; 对于(i = 0; I<计数;我++)
{
MasterSocket [I] =插座(AF_INET,SOCK_STREAM,0);
的printf(创建套接字:%d个\\ N,MasterSocket [I]);
} 对于(i = 0; I<计数;我++)
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(192.168.1.4);
address.sin_port = htons(PORT + I);
如果(setsockopt的(MasterSocket [I],SOL_SOCKET,SO_REUSEADDR,(字符*)及选择,sizeof的(OPT))小于0){
PERROR(setsockopt1);
返回-1;
}
如果(绑定(MasterSocket [I],(结构sockaddr *)及地址的sizeof(地址))小于0){
PERROR(绑定\\ n);
返回-1;
}
的printf(SockerDesriptor%d个---绑定%d个\\ N,MasterSocket [I],PORT + I);
} 对于(i = 0; I<计数;我++)
{
如果(听(MasterSocket [I],4)℃,){
PERROR(听\\ n);
返回-1;
}
其他
的printf(端口上侦听%d个--- \\ n,PORT + I);
} 而(1)
{
FD_ZERO(安培; readfds);
INT状态;
对于(i = 0; I<计数;我++)
{
FD_SET(MasterSocket [I],&放大器; readfds);
如果(MasterSocket [Ⅰ]≥maxfd)
maxfd = MasterSocket [I]
} 状态= PSELECT(maxfd + 1,&安培; readfds,NULL,NULL,&放大器;超时,NULL);
如果(状态℃下)
PERROR(状态);
/ *否则,如果(状态== 0)
的printf(超时发生\\ n); * /
否则如果(状态> 0){
对于(i = 0; I<计数;我++)
{
如果(FD_ISSET(MasterSocket [I],&放大器; readfds)){
SelectSession = MasterSocket [I]
的printf(SelectSession%d个\\ N,MasterSocket [I]);
handle_client(SelectSession);
//的getchar();
}
}
}
}
返回0;
}
This is a Simple Code that I have Written. Server responds to CONNECTIONS ON PORT 2923, 2924 and 2925.
When I run the programme , the serve only accpets CONNECTION from PORT 2923. Can someone HELP me out. Thanku
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
void handle_client(int cliend_fd)
{
int ret = accept(cliend_fd, NULL, 0);
if (ret < 0)
{
printf("Accept Error\n");
}
else
{
printf("Client Accepted\n");
shutdown(ret, 2);
}
}
int main()
{
int count = 3;
int PORT = 2923;
struct sockaddr_in address;
int MasterSocket[count];
int i = 0;
fd_set readfds;
int maxfd;
maxfd = -1;
int SelectSession;
struct timespec TimeOut;
TimeOut.tv_sec = 2;
TimeOut.tv_nsec = 2;
for (i = 0; i < count; i++)
{
MasterSocket[i] = socket(AF_INET, SOCK_STREAM, 0);
}
for (i = 0; i < count; i++)
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = PORT + i;
if (bind(MasterSocket[i], (struct sockaddr *) &address, sizeof(address))
< 0)
{
perror("Bind\n");
getchar();
}
printf("SockerDesriptor %d---bind %d\n", MasterSocket[i], PORT + i);
}
for (i = 0; i < count; i++)
{
if (listen(MasterSocket[i], 4) < 0)
{
perror("Listen");
getchar();
//return 1;
}
else
{
printf("Listening on Port %d---\n", PORT + i);
}
}
while (1)
{
FD_ZERO(&readfds);
int status;
for (i = 0; i < count; i++)
{
FD_SET(MasterSocket[i], &readfds);
if (MasterSocket[i] > maxfd)
{
maxfd = MasterSocket[i];
}
printf("%d Added to FD_SET Descriptor %d \n\n", PORT + i,
MasterSocket[i]);
}
//status = 0;
printf("############Waiting for Connection\n");
status = pselect(maxfd + 1, &readfds, NULL, NULL, &TimeOut, NULL );
if (status < 0)
{
perror("Status");
getchar();
return 1;
}
else if (status == 0)
{
printf("TimeOut occured\n");
}
else
{
//printf("Status %d\n", status);
SelectSession = -1;
for (i = 0; i < count; i++)
{
//printf("Checking Set %d\n", i);
if (FD_ISSET(MasterSocket[i], &readfds))
{
//printf("Matching Set %d\n", i);
SelectSession = MasterSocket[i];
printf("SelectSession %d\n", MasterSocket[i]);
if (SelectSession == -1)
{
//shutdown (MasterSocket[i], 2);
//continue;
break;
}
else
{
printf("In Handle\n");
handle_client(SelectSession);
getchar();
}
}
else
{
printf("Not in FD_ISSET\n");
}
}
}
/*for (i=0; i<count; i++)
{
shutdown (MasterSocket[i], 2);
}
*/
}
return 0;
}
Remove the getchar()
from while loop.
After pselect
server waiting to get a character, so it is not allowing connect anymore client.
Or you have to enter a character in server after every client connected
And change port number to network byte order using htons
Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
void handle_client (int cliend_fd)
{
int ret = accept(cliend_fd, NULL, 0);
if ( ret < 0 )
printf("Accept Error\n");
else {
printf ("Client Accepted\n");
shutdown (ret, 2);
}
return;
}
int main()
{
int count = 3, PORT = 8000, opt = 1, i = 0;
int MasterSocket[count];
int maxfd = -1, SelectSession;
fd_set readfds;
struct sockaddr_in address;
struct timespec TimeOut;
TimeOut.tv_sec = 2;
TimeOut.tv_nsec = 2;
for(i=0; i<count; i++)
{
MasterSocket[i] = socket(AF_INET , SOCK_STREAM , 0);
printf("socket created : %d\n", MasterSocket[i]);
}
for(i=0; i<count; i++)
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("192.168.1.4");
address.sin_port = htons(PORT+i);
if( setsockopt(MasterSocket[i], SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 ) {
perror("setsockopt1");
return -1;
}
if (bind(MasterSocket[i], (struct sockaddr *)&address, sizeof(address)) < 0) {
perror ("Bind\n");
return -1;
}
printf("SockerDesriptor %d---bind %d\n", MasterSocket[i], PORT+i);
}
for(i=0; i<count;i++)
{
if (listen(MasterSocket[i], 4) < 0) {
perror ("Listen\n");
return -1;
}
else
printf("Listening on Port %d---\n", PORT+i);
}
while(1)
{
FD_ZERO(&readfds);
int status;
for (i = 0; i < count; i++)
{
FD_SET(MasterSocket[i], &readfds);
if (MasterSocket[i] > maxfd)
maxfd = MasterSocket[i];
}
status = pselect(maxfd + 1, &readfds, NULL, NULL, &TimeOut, NULL );
if(status < 0)
perror("Status");
/* else if(status == 0)
printf("TimeOut occured\n"); */
else if(status > 0) {
for (i = 0; i < count; i++)
{
if (FD_ISSET(MasterSocket[i], &readfds)) {
SelectSession = MasterSocket[i];
printf("SelectSession %d\n", MasterSocket[i]);
handle_client(SelectSession);
//getchar();
}
}
}
}
return 0;
}
这篇关于多插槽听的Linux的Socket编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!