Dear All,
Could someone help and tell me how to handle multiple send and receive operations with udp sockets? In fact here is my problem:
server.c is composing of serveral sub programs (the same for client.c)
-----server.c------- -
................
-
int main(){
-
............
-
//create socket
-
if (sd = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto)) {
-
fprintf(stderr, "socket creation failed\n");
-
exit(1);
-
}
-
-
// sockaddr structure
-
memset((char *)&sad, 0, sizeof(sad));
-
sad.sin_port = htons(port);
-
sad.sin_family = AF_INET;
-
sad.sin_addr.s_addr = htons(INADDR_ANY);
-
-
//bind between the socket and the address
-
if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
-
fprintf(stderr, "bind failed\n");
-
exit(1);
-
}
-
...............
-
sub-prog1(int sd){
-
...............
-
if (recvfrom(sd,buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
-
perror("read data failled");
-
exit(1);
-
}
-
.........
-
if (sendto(sd,buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
-
perror("write data failled");
-
exit(1);
-
}
-
............
-
}
-
sub-prog2(int sd){
-
...............
-
if (recvfrom(sd,buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
-
perror("read data failled");
-
exit(1);
-
}
-
.........
-
if (sendto(sd,buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
-
perror("write data failled");
-
exit(1);
-
}
-
............
-
}
-
sub-prog3(int sd){
-
...............
-
if (recvfrom(sd,buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
-
perror("read data failled");
-
exit(1);
-
}
-
.........
-
if (sendto(sd,buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
-
perror("write data failled");
-
exit(1);
-
}
-
............
-
}
-
} // end of main
-
-
------client.c------
-
............
-
#define MAXSOCK 3
-
................
-
int main(){
-
................
-
// sockaddr structure
-
memset((char *)&sad, 0, sizeof(sad));
-
sad.sin_port = htons(port);
-
sad.sin_family = AF_INET;
-
sad.sin_addr.s_addr = htons(INADDR_ANY);
-
-
for (i=0;i<MAXSOCK; i++){
-
//create socket
-
if (sd[MAXSOCK] = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto)) {
-
fprintf(stderr, "socket creation failed\n");
-
exit(1);
-
}
-
} // end of for
-
...............
-
sub-prog1(int sd[0]){
-
.........
-
if (sendto(sd[0],buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
-
perror("write data failled");
-
exit(1);
-
}
-
...............
-
if (recvfrom(sd[0],buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
-
perror("read data failled");
-
exit(1);
-
}
-
............
-
}
-
sub-prog2(int sd[1]){
-
.........
-
if (sendto(sd[1],buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
-
perror("write data failled");
-
exit(1);
-
}
-
...............
-
if (recvfrom(sd[1],buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
-
perror("read data failled");
-
exit(1);
-
}
-
............
-
}
-
sub-prog3(int sd[2]){
-
.........
-
if (sendto(sd[2],buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
-
perror("write data failled");
-
exit(1);
-
}
-
...............
-
if (recvfrom(sd[2],buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
-
perror("read data failled");
-
exit(1);
-
}
-
............
-
}
-
} // end of main
-
--------------------------
I tried serveral ways to make it working but unfortunatly I haven't succeded yet. Could some please tell me the right way on how to make such programs working ..... thanking you in advance and any help would be appreciated.
Faithfully yours,
Azwaw
2 4104
Given that you've cut a lot of code, we need more information. Is it not working because of errors, or is there a logical error in there so that it compiles without warnings, but does not act the way you intended?
Hi,
thanks for your reply. the problem is not about the compiling ...
Actually I don't get any warning or error when the programs are compiled, but the problem is when the sub-prog2 is exucuted and then I get the errors like: segmentation ...
My question is how to handle multipe sending and receiving data using udp socket ???? Any one has an idea, it would be appreciated. Thanks
Regards and thanks for your reply,
Azwaw
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alphamacaroon |
last post by:
All,
I'm wondering if anyone can help me with a strange problem I'm having.
First off, here's what I'm trying to do:
I'm developing a UDP network application that should allow the end user
to...
|
by: Bruce Vander Werf |
last post by:
I am developing a network client application (using the Socket class)
that will need to make simultaneous TCP connections to many (100 or
more) servers. In this case, which would be a better...
|
by: Manfred Braun |
last post by:
Hi All,
I am trying to understand the blocking method socket.Send().
The call blocks as expected, but does this mean, it returnes after the
underlying TCP layer got a confirmation, that the send...
|
by: Sharon |
last post by:
Hi all,
I've implemented a TCP server using the Socket async methods.
When connecting to the server from 3 instances of hyper terminal,
i've noticed that each of the newly created server sockets,...
|
by: yvan |
last post by:
Hi,
Here is my client/server scenario:
Step1: Client connects to server and sends data sucessfully (using
Socket.Send()).
Step2: Server gracefully exists (calls Socket.Shutdown() and...
|
by: BuddyWork |
last post by:
Hello,
Could someone please explain why the Socket.Send is slow to send to the same
process it sending from. Eg. Process1 calls Socket.Send which sends to the
same IP address and port, the...
|
by: Buddy Home |
last post by:
There is two examples of code.
Example 1. Send and Receive within the same process. Put this code in a
console app called SendAndReceive and run the code.
using System;
using...
|
by: =?Utf-8?B?YmJn?= |
last post by:
Hi all,
I am testing how well my server application is connecting to multiple
clients and give and take some information. All actions(connect, accept,
send, receive) are performed by async...
|
by: =?Utf-8?B?Y2hyaXNiZW4=?= |
last post by:
Hi,
My understanding is that for the same socket, assuming send and receive run
at two different threads, I have to use BeginReceive to avoid the blocking on
send part. However, I can jsut use...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |