04-15-2013
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?
10 More Discussions You Might Find Interesting
1. Solaris
Ladies and Gentlemen:
I have successfully configured a Solaris 8 server with Jumpstart! I can Jumpstart Solaris 8 client systems with no problem. My configuration is as follows:
Jumpstart Server: Solaris 8 patched with Recommended Patches from June 05.
I have installed Solaris 8 in... (4 Replies)
Discussion started by: rambo15
4 Replies
2. IP Networking
Just like concurrent tcp server, anyone know how to design the concurrent udp server with the thread? it's best to give some basic examples,thanks (0 Replies)
Discussion started by: Frank2004
0 Replies
3. UNIX for Advanced & Expert Users
HI
I want to create a UDP concurrent server
can any one give the code for UDP concurrent server (8 Replies)
Discussion started by: chanikya
8 Replies
4. Programming
Hi all,
I have an application where
there are 5 udp clients/senders which keep sending data to same IP with different port number
can I design my udp server to recieve data from all 5 clients at the same time? how should I use the server address structure? should I use different... (3 Replies)
Discussion started by: shashi
3 Replies
5. Programming
Hello,
I would like to ask you for a help. I need to program in C print server which is based on connectionless protocol UDP. The problem is that I am not experienced in programming network applications, I can help myself with some other answered questions on this forum about network apps in C,... (1 Reply)
Discussion started by: samciz
1 Replies
6. UNIX for Dummies Questions & Answers
Hi,
My network dep. telles me that they have opened the FW but my application still can not get through to other server. If it was TCP I could simply test it myself with "telnet", but how can I check it when the connection is UDP?
Tnx (1 Reply)
Discussion started by: mehrdad68
1 Replies
7. IP Networking
Hello !
I seem to have the same problem as in https://www.unix.com/ip-networking/91203-inaddr_any-opposite.html#post302262417
But I can't find a solution.
I have a UDP server socket bound to 0.0.0.0. The server hosts the addresses IP1, IP2 and IP3. I get an incoming request to IP1. I use... (1 Reply)
Discussion started by: steinwej
1 Replies
8. UNIX for Dummies Questions & Answers
So I am attempting to implement a UDP server on an older Solaris 9 box; the server will be handling packets at a rate of about 12/sec and will be constructing acknowledgment messages on the fly.
I've found snippets of sample code here and there on the net... should I be writing this from scratch... (1 Reply)
Discussion started by: allbread
1 Replies
9. Programming
Hi,
I have a situation where i need to communicate a linux client with a windows server, I am using a UDP socket communication channel. I am able to send packets from my linux clients to the windows server but unable to receive any data packet from the server. Do i need to make any setting in... (0 Replies)
Discussion started by: John20
0 Replies
10. IP Networking
All of sudden in this morning, some computers could not connect to our UNIX server while other still could.
Some computers could ping the server while some could not.
Same on the server side.
It could ping some clients but not some.
All Windows clients could ping each other.
And more, the... (1 Reply)
Discussion started by: jonapa
1 Replies
LEARN ABOUT MOJAVE
net::server::proto::udp
Net::Server::Proto::UDP(3) User Contributed Perl Documentation Net::Server::Proto::UDP(3)
NAME
Net::Server::Proto::UDP - Net::Server UDP protocol.
SYNOPSIS
See Net::Server::Proto.
DESCRIPTION
Protocol module for Net::Server. This module implements the SOCK_DGRAM socket type under INET (also known as UDP). See
Net::Server::Proto.
PARAMETERS
The following paramaters may be specified in addition to normal command line parameters for a Net::Server. See Net::Server for more
information on reading arguments.
udp_recv_len
Specifies the number of bytes to read from the UDP connection handle. Data will be read into $self->{'server'}->{'udp_data'}. Default
is 4096. See IO::Socket::INET and recv.
udp_recv_flags
See recv. Default is 0.
udp_broadcast
Default is undef.
QUICK PARAMETER LIST
Key Value Default
## UDP protocol parameters
udp_recv_len d+ 4096
udp_recv_flags d+ 0
udp_broadcast bool undef
INTERNAL METHODS
"object"
Returns an object with parameters suitable for eventual creation of a IO::Socket::INET object listining on UDP.
"connect"
Called when actually binding the port. Handles default parameters before calling parent method.
LICENCE
Distributed under the same terms as Net::Server
perl v5.18.2 2013-01-09 Net::Server::Proto::UDP(3)