473,830 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

server client question

QQ
hello I need to listen to two ports and process the received message.

Which way to do is better? I have a short program like this
but I am not sure whether it is good or not. Or I'd better open two
threads and process the message seperately.

Thanks a lot!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT1 3000 // the port users will be connecting to
#define MYPORT2 4000
#define MAXLEN 100

int main(void)
{
int udp_fd1, udp_fd2, maxfd; // listen on tcp_fd, new connection
on new_tcp_fd, udp_fd is for register
struct sockaddr_in udp_addr1, udp_addr2; // my address
information
struct sockaddr_in register_addr; //register client's address

fd_set rset;

char buf[MAXLEN];
int buf_size, numbytes, addr_len;

buf_size = sizeof(buf);
addr_len = sizeof(struct sockaddr);

struct timeval tv;
int nready;

//*************** *************** *************** ******
//open the UDP socket 1
if((udp_fd1 = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr, "\nsocket create error.\n");
exit(1);
}

udp_addr1.sin_f amily = AF_INET;
udp_addr1.sin_p ort = htons(MYPORT1);
udp_addr1.sin_a ddr.s_addr = htonl(INADDR_AN Y);
memset(&(udp_ad dr1.sin_zero), '\0', 8);

/* Binding the created socket to the port specified */
if(bind(udp_fd1 ,(struct sockaddr *)&udp_addr1,si zeof(struct
sockaddr)) == -1)
{
fprintf(stderr, "\n Error in bind \n");
exit(1);
}

//*************** *************** *************** ******
//open the UDP socket 2
if((udp_fd2 = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr, "\nsocket create error.\n");
exit(1);
}

udp_addr2.sin_f amily = AF_INET;
udp_addr2.sin_p ort = htons(MYPORT2);
udp_addr2.sin_a ddr.s_addr = htonl(INADDR_AN Y);
memset(&(udp_ad dr2.sin_zero), '\0', 8);

/* Binding the created socket to the port specified */
if(bind(udp_fd2 ,(struct sockaddr *)&udp_addr2,si zeof(struct
sockaddr)) == -1)
{
fprintf(stderr, "\n Error in bind \n");
exit(1);
}

FD_ZERO(&rset);

while(1){

/**********recei ve packet********* *************** ***************/
FD_SET(udp_fd1, &rset);
FD_SET(udp_fd2, &rset);
if(udp_fd1 > udp_fd2)
maxfd = udp_fd1 + 1;
else
maxfd = udp_fd2 + 1;

tv.tv_usec = 0;
tv.tv_sec = 2;

nready = select(maxfd,&r set,NULL,NULL,& tv);

switch(nready){

case -1:
printf("\n Error in select!!! \n");
break;

case 0:
printf("******* *******Did not receive anything\n");
break;

default:
printf("\n\n*** ************* Received packets ");
if (FD_ISSET(udp_f d1,&rset))
{
/* Receive packet from MYPORT1 */
printf("\n--------------------------------\n");

if((numbytes = recvfrom(udp_fd 1, buf, MAXLEN-1, 0,(struct
sockaddr*)&regi ster_addr, &addr_len))= =-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}

printf("Got packet from IP address %s, port %d \n",
inet_ntoa(regis ter_addr.sin_ad dr),ntohs(regis ter_addr.sin_po rt));
printf("on port: %d\n", ntohs(udp_addr1 .sin_port));
printf("Packet size: %u bytes \n", numbytes);
printf("Message : %s\n", buf);

}

if (FD_ISSET(udp_f d2,&rset)) /* new client connection */
{
/* Receive packet from MYPORT2 */
printf("\n--------------------------------\n");

if((numbytes = recvfrom(udp_fd 2, buf, MAXLEN-1, 0,(struct
sockaddr*)&regi ster_addr, &addr_len))= =-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}

printf("Got packet from IP address %s, port %d \n",
inet_ntoa(regis ter_addr.sin_ad dr),ntohs(regis ter_addr.sin_po rt));
printf("on port: %d\n", ntohs(udp_addr2 .sin_port));
printf("Packet size: %u bytes \n", numbytes);
printf("Message : %s\n", buf);
}
}

}
return 0;
}

Nov 14 '05 #1
1 1319
QQ wrote:
hello I need to listen to two ports and process the received message.

[too much code snipped]

And you need to post this somewhere where it would be topical, like,
say, news:comp.unix. programmer.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2578
by: Matt | last post by:
I think this is the basic concept in ASP server-side development. My boss told me web application is NOT client-server application. I argued with him because browser is the client, and the server code put in server. Then web application should be a client-server application. My understanding is that a web application is an application that runs on a browser. But client-server application is not necessary a web application. Please...
12
12441
by: HarveyB | last post by:
I would like to generate non-modal popup windows from ASP.Net code-behind. I have tried using Client Side scripting like "function Test(){ window.open('test.htm',_blank, 'height=200,width=400,status=no,toolbar=no, menubar=no,location=no resizable=no scrollable=no'); but I can't seem to invoke the client side script from within a Server Side Form. I know I can use the context with to Response.redirect or Server.transfer to return a
7
3374
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly communicate directly with the database. Do I need a separate db2 connect on each such machine. Please advice.
2
8408
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
9
2420
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that when I give a path like @"C:\holdfiles\myfile.txt" it looks on the server C drive. How do I pull from the client? Do I need a different class and/or method? Filestream? -- Thanks,
1
1345
by: Frank Millman | last post by:
Hi all I am developing a multi-user business/accounting application. It is coming along nicely :-), though rather slowly :-( I have hit an issue which will require a lot of changes to the code I have written so far, together with an increase in complexity and all the bad things that follow from that. Before I go ahead and make the changes, I thought I would bounce it off the group and see if there is a simpler approach.
4
3609
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
1
2491
by: =?Utf-8?B?cmJiZW5zb24=?= | last post by:
To begin,, the network infactructure- Servers - Server00 - Windows Server 2003/Installed Server01 - Windows Server 2003/plan to install Server10 - Linux RedHat Workstation/Installed Client/Workstations - Client 00 - Windows XP Pro/Installed Client 01 - Windows XP Home/Installed
2
4730
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes which Inherits MarshalByRefObject (ok, at this stage it only contains one class... but its gonna grow) - Class Library containing common classes and interfaces that will be shared between all projects. This include interfaces for the server...
11
4896
by: Jeff | last post by:
Hello everyone. I've searched through the archives here, and it seems that questions similar to this one have come up in the past, but I was hoping that I could pick your Pythonic brains a bit. Here's a broad overview of what I need to do: cross-platform, client- side GUI apps that interact with a server backed by a database. I'd also like the possibility of having a web interface for small portions of the app. It will be a fairly...
0
9791
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10771
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10487
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9313
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7745
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.