Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 多个客户端进程连接到1个服务器进程_C_Sockets_Networking - Fatal编程技术网

C 多个客户端进程连接到1个服务器进程

C 多个客户端进程连接到1个服务器进程,c,sockets,networking,C,Sockets,Networking,我正在制作一个C套接字程序,其中1个服务器进程依次与4个进程中的1个客户端进程通信 更具体地说,当客户端和服务器相互通信10条消息(每条5条消息)时,等待连接到服务器的另一个客户端连接并执行相同的操作。连接时,每个客户端必须具有不同的端口(8000-8003) 我认为我下面的程序只与一个与服务器(8000)的同一端口匹配的客户端通信 当只有1个服务器进程和4个客户端进程使用不同的端口时,我应该怎么做 从客户端截屏shot 服务器屏幕截图 客户端.c #定义POSIX_SOURCE #包括 #

我正在制作一个C套接字程序,其中1个服务器进程依次与4个进程中的1个客户端进程通信

更具体地说,当客户端和服务器相互通信10条消息(每条5条消息)时,等待连接到服务器的另一个客户端连接并执行相同的操作。连接时,每个客户端必须具有不同的端口(8000-8003)

我认为我下面的程序只与一个与服务器(8000)的同一端口匹配的客户端通信

当只有1个服务器进程和4个客户端进程使用不同的端口时,我应该怎么做

从客户端截屏shot

服务器屏幕截图

客户端.c

#定义POSIX_SOURCE
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,const char*argv[]{
内部c_插座(fd);
服务器地址中的结构sockaddr\u;
char fromServer[100];
int str_len;
int端口=8000;
整数计数=0;
智力状态;
不要等待;
pid_t pid=fork();
if(pid<0){
printf(“fork失败!\n”);
返回0;
}否则如果(pid==0){
//P-C1
pid=fork();
if(pid<0){
printf(“fork失败!\n”);
返回0;
}否则如果(pid==0){
//P-C1-C3
端口=8003;
}否则,如果(pid>0){
//P-C1
端口=8001;
}
}否则,如果(pid>0){
//P
pid=fork();
if(pid<0){
printf(“fork失败!\n”);
返回0;
}否则如果(pid==0){
//P-C2
端口=8002;
}否则,如果(pid>0){
//P
端口=8000;
}
}
printf(“pid:%d ppid:%d\n”,pid,getppid());
c_socket_fd=socket(PF_INET,SOCK_STREAM,0);
printf(“已创建的客户端套接字”);
memset(&server_地址,0,sizeof(server_地址));
服务器地址.sin\u family=AF\u INET;
服务器地址sin\u addr.s\u addr=inet\u addr(“127.0.0.1”);
服务器\地址.sin\端口=htons(端口);
if(connect(c_socket_fd,(struct sockaddr*)和服务器地址,sizeof(服务器地址))==0){
printf(“已连接的服务器\n”);
而(1){
char-toServer[100]=“你好,服务器,这是消息编号”;
char*ch=toServer;
而(*ch!='\0'){
ch++;
}
*ch=('0'+计数);
ch++;
字符温度[7]=“从”;
char*temp_ch=温度;
而(*temp_ch!='\0'){
*ch=*温度;
temp_ch++;
ch++;
}
int process_id=pid;
如果(进程id>=10000){
字符温度[6]={0,};
sprintf(临时,“%d”,进程id);
char*temp_ch=温度;
而(*temp_ch!='\0'){
*ch=*温度;
temp_ch++;
ch++;
}
}否则如果(进程id>=1000){
字符温度[5]={0,};
sprintf(临时,“%d”,进程id);
char*temp_ch=温度;
而(*temp_ch!='\0'){
*ch=*温度;
temp_ch++;
ch++;
}
}否则如果(进程id>=100){
字符温度[4]={0,};
sprintf(临时,“%d”,进程id);
char*temp_ch=温度;
而(*temp_ch!='\0'){
*ch=*温度;
temp_ch++;
ch++;
}
}否则如果(进程id>=10){
字符温度[3]={0,};
sprintf(临时,“%d”,进程id);
char*temp_ch=温度;
而(*temp_ch!='\0'){
*ch=*温度;
temp_ch++;
ch++;
}
}否则{
字符温度[2]={0,};
sprintf(临时,“%d”,进程id);
char*temp_ch=温度;
而(*temp_ch!='\0'){
*ch=*温度;
temp_ch++;
ch++;
}
}
ch++;
*ch='\0';
计数++;
如果(计数>5){
如果(pid>0)
wait_pid=等待(&status);
如果(等待pid!=-1)
打破
}
写入(c_socket_fd,toServer,sizeof(toServer));
读取(c_socket_fd,fromServer,sizeof(fromServer));
printf(“来自服务器:%s\n”,来自服务器);
}
关闭(c_插座\u fd);
}
返回0;
}
server.c

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义端口8000
int main(){
内部s_插座(fd),c_插座(fd);;
服务器地址、客户端地址中的结构sockaddr\u;
socklen\u t客户地址\u size=sizeof(客户地址);
char-toClient[100]=“你好,客户,这是我对您的消息编号的回复”;
char fromClient[100];
s_socket_fd=socket(PF_INET,SOCK_STREAM,0);
如果(s_插槽\u fd==-1){
printf(“套接字创建失败!\n”);
关闭(s_插座\u fd);
返回0;
}
printf(“已创建服务器套接字!\n”);
memset(&server_地址,0,sizeof(server_地址));
服务器地址.sin\u family=AF\u INET;
服务器地址sin\u addr.s\u addr=htonl(INADDR\u ANY);
服务器\地址.sin\端口=htons(端口);
if((绑定(s_socket_fd,(struct sockaddr*)和服务器地址,sizeof(服务器地址)))=-1){
printf(“绑定失败!\n”);
关闭(s_插座\u fd);
返回0;
}
如果((侦听(s_套接字_fd,10)=-1)){
printf(“侦听失败!\n”);
关闭(s_)插座