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
SSL_CTX_set_psk_client_callback(3SSL)				      OpenSSL				     SSL_CTX_set_psk_client_callback(3SSL)

NAME
SSL_CTX_set_psk_client_callback, SSL_set_psk_client_callback - set PSK client callback SYNOPSIS
#include <openssl/ssl.h> void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, unsigned int (*callback)(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len)); void SSL_set_psk_client_callback(SSL *ssl, unsigned int (*callback)(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len)); DESCRIPTION
A client application must provide a callback function which is called when the client is sending the ClientKeyExchange message to the server. The purpose of the callback function is to select the PSK identity and the pre-shared key to use during the connection setup phase. The callback is set using functions SSL_CTX_set_psk_client_callback() or SSL_set_psk_client_callback(). The callback function is given the connection in parameter ssl, a NULL-terminated PSK identity hint sent by the server in parameter hint, a buffer identity of length max_identity_len bytes where the the resulting NULL-terminated identity is to be stored, and a buffer psk of length max_psk_len bytes where the resulting pre-shared key is to be stored. NOTES
Note that parameter hint given to the callback may be NULL. RETURN VALUES
Return values from the client callback are interpreted as follows: On success (callback found a PSK identity and a pre-shared key to use) the length (> 0) of psk in bytes is returned. Otherwise or on errors callback should return 0. In this case the connection setup fails. 1.0.1e 2013-02-11 SSL_CTX_set_psk_client_callback(3SSL)
All times are GMT -4. The time now is 04:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy