use*_*072 13 c unix sockets algorithm fork
我正在尝试使用fork()创建一个套接字编程服务器来同时处理多个客户端..但我无法正确实现它.我已经尝试了很长时间.我面临的问题是1)地址绑定问题2)问题如何处理父进程和子进程3)如何结束服务器程序即...返回到控制台我的程序为单个客户端服务器正常工作.这是我的多个客户端服务器的代码.
#include<signal.h>
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
struct sockaddr_in myaddr ,clientaddr;
int sockid,newsockid;
sockid=socket(AF_INET,SOCK_STREAM,0);
memset(&myaddr,'0',sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(8888);
myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(sockid==-1)
{
perror("socket");
}
int len=sizeof(myaddr);
if(bind(sockid,( struct sockaddr*)&myaddr,len)==-1)
{
perror("bind");
}
if(listen(sockid,10)==-1)
{
perror("listen");
}
int pid,new;
static int counter=0;
for(;;)
{ a:
new =accept(sockid,(struct sockaddr *)&clientaddr,&len);
if(pid=fork()==-1)
{
close(new);
continue;
}
else if(pid>0)
{
counter++;
//wait();
goto a;
printf("here2");
//close(new);
continue;
}
else if(pid==0)
{
counter++;
printf("here 1");
send(new,"hi",100,0);
send(new,(char *) &counter,1,0);
//kill(pid,SIGKILL);
//close(new);
}
}
printf("here3");
close(sockid);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是简单的客户端程序
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<sys/types.h>
int main()
{
struct sockaddr_in myaddr ,serveraddr;
int sockid;
sockid=socket(AF_INET,SOCK_STREAM,0);
memset(&myaddr,'0',sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(8888);
myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(sockid==-1)
{
perror("socket");
}
int len=sizeof(myaddr);
if(connect(sockid,(const struct sockaddr*)&myaddr,len)==-1)
{
perror("connect");
}
fprintf(stdout,"Client Online....");
char s[10000];
//gets(s);
//send(sockid,s,10000,0);
recv(sockid,&s,10000,0);
fprintf(stdout,"Server says....");
puts(s);
recv(sockid,&s,10000,0);
fprintf(stdout,"Server says....");
puts(s);
sleep(10);
close(sockid);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么,这是正确的方法..?任何帮助将不胜感激...
caf*_*caf 19
你遇到的主要问题是==优先级高于=,所以这一行:
if(pid=fork()==-1)
Run Code Online (Sandbox Code Playgroud)
正在分配fork() == -1to 的结果pid,这不是你想要的结果:它总是在孩子和父母的成功0时.你需要使用:fork()
if((pid = fork()) == -1)
Run Code Online (Sandbox Code Playgroud)
你应该close(new)在父母之后fork()- 孩子现在拥有那个套接字.如果要发送计数器的文本版本,则需要使用snprintf()它将其转换为文本.孩子也应该在完成后退出 - 在代码中执行此操作的最简单方法是打破循环.经过这些更正后,服务器中的内部循环如下所示:
for(;;)
{
new = accept(sockid, (struct sockaddr *)&clientaddr, &len);
if ((pid = fork()) == -1)
{
close(new);
continue;
}
else if(pid > 0)
{
close(new);
counter++;
printf("here2\n");
continue;
}
else if(pid == 0)
{
char buf[100];
counter++;
printf("here 1\n");
snprintf(buf, sizeof buf, "hi %d", counter);
send(new, buf, strlen(buf), 0);
close(new);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58011 次 |
| 最近记录: |