Sponsored Content
Full Discussion: Clients - Server ( UDP )
Top Forums Programming Clients - Server ( UDP ) Post 302794127 by MaHmur on Monday 15th of April 2013 08:08:45 AM
Old 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

Solaris 8 server and Jumpstarting 2.6 clients

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

concurrent udp server

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

HOW to create UDP Concurrent Server

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

socket programming (UDP with multiple clients)

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

Print server via UDP

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

How can I check UDP connection to other server?

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

UDP server socket inaddr_any - How to get the real IP

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

UDP server on Solaris 9

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

UDP linux client and Windows server

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

Some clients cannot ping UNIX server

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
Server::Client(3pm)					User Contributed Perl Documentation				       Server::Client(3pm)

NAME
Net::SMTP::Server::Client - Client session handling for Net::SMTP::Server. SYNOPSIS
use Carp; use Net::SMTP::Server; use Net::SMTP::Server::Client; use Net::SMTP::Server::Relay; $server = new Net::SMTP::Server('localhost', 25) || croak("Unable to handle client connection: $! "); while($conn = $server->accept()) { # We can perform all sorts of checks here for spammers, ACLs, # and other useful stuff to check on a connection. # Handle the client's connection and spawn off a new parser. # This can/should be a fork() or a new thread, # but for simplicity... my $client = new Net::SMTP::Server::Client($conn) || croak("Unable to handle client connection: $! "); # Process the client. This command will block until # the connecting client completes the SMTP transaction. $client->process || next; # In this simple server, we're just relaying everything # to a server. If a real server were implemented, you # could save email to a file, or perform various other # actions on it here. my $relay = new Net::SMTP::Server::Relay($client->{FROM}, $client->{TO}, $client->{MSG}); } DESCRIPTION
The Net::SMTP::Server::Client module implements all the session handling required for a Net::SMTP::Server::Client connection. The above example demonstrates how to use Net::SMTP::Server::Client with Net::SMTP::Server to handle SMTP connections. $client = new Net::SMTP::Server::Client($conn) Net::SMTP::Server::Client accepts one argument that must be a handle to a connection that will be used for communication. Once you have a new client session, simply call: $client->process This processes an SMTP transaction. THIS MAY APPEAR TO HANG -- ESPECIALLY IF THERE IS A LARGE AMOUNT OF DATA BEING SENT. Once this method returns, the server will have processed an entire SMTP transaction, and is ready to continue. Once $client->process returns, various fields have been filled in. Those are: $client->{TO} -- This is an array containing the intended recipients for this message. There may be multiple recipients for any given message. $client->{FROM} -- This is the sender of the given message. $client->{MSG} -- The actual message data. :) AUTHOR AND COPYRIGHT Net::SMTP::Server / SMTP::Server is Copyright(C) 1999, MacGyver (aka Habeeb J. Dihu) <macgyver@tos.net>. ALL RIGHTS RESERVED. You may distribute this package under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. SEE ALSO
Net::SMTP::Server::Server, Net::SMTP::Server::Relay perl v5.10.1 1999-12-28 Server::Client(3pm)
All times are GMT -4. The time now is 10:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy