Clients - Server ( UDP ) | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Clients - Server ( UDP )

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-15-2013
MaHmur MaHmur is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 15 April 2013, 8:19 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Clients - Server ( UDP )

Hello,

I have a question: I want to create a n client to one server connection. This is the client-server algorithm.

Quote:
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<error.h>
#include<string.h>
#include<stdlib.h>
#include<signal.h>

const int PORT_SERVER=9001;
const int CLIENT_MAXIM=10;

extern int errno;
int ds;
int dc;
int nr=0;

void semna(int nr_semnal)
{ if(nr_semnal == SIGCHLD)
{ wait(NULL);
nr--;
return; }
}

int e_prim(int i)
{ int k;
for(k=2; k*k<=i;k++)
if(i%k==0)
return 0;
return 1;
}

void client()
{ char buffer[100];
char aux[100];
int i,t;
int numar, k;
sprintf(aux, "Este clientul numarul: %d\n", nr);
if(write(dc,aux,strlen(aux)) != strlen(aux))
{ shutdown(dc,2);
exit(errno); }
sprintf(aux, "Dati numarul:");
if(write(dc,aux,strlen(aux)) != strlen(aux))
{ shutdown(dc,1);
exit(errno); }
bzero(buffer,100);

if(read(dc,buffer,100) == 0)
{ shutdown(dc,2);
exit(errno); }
numar=atoi(buffer);
for(k=2;k<numarl;k++)
if(e_prim(k))
{ sprintf(aux, "Numar prim: %d\n",k);
if(write(dc,aux,strlen(aux)) != strlen(aux))
{ shutdown(dc,2);
exit(errno); }
}
shutdown(dc,2);
exit(errno);
}

int main()
{ struct sockaddr_in server;
if(signed(SIGCHLD, semnal) == SIG_ERR)
{ perror("signal()");
exit(errno); }
if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{ perror("signal()");
exit(errno); }
if((ds = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ perror("socket()");
return errno; }
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT_SERVER);
server.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind(ds,&server,sizeof(server)) == -1)
{ perror("bind()");
return errno; }
if(listen(ds,5) == -1)
{ perror("listen()");
return errno; }
printf("Asteptam clientul la portul %d ... \n", PORT_SERVER);
while(1)
{ dc=accept(ds,NULL,NULL);
if(nr == CLIENTI_MAXIM)
{ shutdown(dc,2);
continue; }
switch(fork())
{ case 0:
client();
break;
case -1:
perror("fork()");
break;
default:
break; }
nr++; }
}
Enybody help to make the changes?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UDP server on Solaris 9 allbread UNIX for Dummies Questions & Answers 1 11-04-2009 01:06 PM
Print server via UDP samciz Programming 1 01-02-2009 10:03 AM
socket programming (UDP with multiple clients) shashi Programming 3 10-23-2008 06:31 AM
concurrent udp server Frank2004 IP Networking 0 03-30-2006 01:01 AM
Solaris 8 server and Jumpstarting 2.6 clients rambo15 Solaris 4 07-15-2005 06:36 PM



All times are GMT -4. The time now is 10:42 PM.