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 SUNOS
in.daytimed
in.daytimed(1M) System Administration Commands in.daytimed(1M)
NAME
in.daytimed - UDP or TCP daytime protocol service daemon
SYNOPSIS
in.daytimed
FMRI
svc:/internet/daytime:default
DESCRIPTION
FMRI stands for Fault Management Resource Identifier. It is used to identify resources managed by the Fault Manager. See fmd(1M) and
smf(5).
The in.daytimed service provides the server-side of the daytime protocol. This protocol is used for debugging and bandwidth measurement and
is available on both TCP and UDP transports, through port 13.
The in.daytimed service is an inetd(1M) smf(5) delegated service. The in.daytimed detects which transport is requested by examining the
socket it is passed by the inetd daemon.
TCP-based service
Once a connection is established, the in.daytimed generates the current date and time in ctime(3C) format as 7-bit ASCII and sends it
through the connection. The server then closes the connection. Any data received from the client side of the connection is discarded.
UDP-based service
The in.daytimed listens for UDP datagrams. When a datagram is received, the server generates the current date and time in ctime(3C)
format as 7-bit ASCII and inserts it in a UDP datagram sent in response to the client's request. Any received data is ignored.
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|Availability |SUNWcnsu |
+-----------------------------+-----------------------------+
|Interface Stability |Evolving |
+-----------------------------+-----------------------------+
SEE ALSO
inetd(1M), attributes(5), smf(5)
RFC 867
SunOS 5.10 23 Aug 2004 in.daytimed(1M)