nieh@cs.utexas.edu (07/18/90)
I am having problem with the TCP/UDP facilities. What I want to do is to have a server process permanently listen to two ports one created as a UPD port and the other as a TCP port. The server will receive UPD and/or TCP packets asynchronously. The following program is written in Sun/C++ on SunOs-4.0.3. The symptom are: 1. If several processes send UPD packets to the server, it works 2. If several processes send TCP packets to the server, it works 3. If several processes send UPD packets first then other processes send TCP packets, the sever will not receive the TCP packets. 4. If several processes send TCP packets first then other processes send UDP packets, the sever will not receive the UDP packets. I need your help. If you can figure out the problem from the following code please e-mail me your solution. If you need to see the rest of the code please let me know I can send it to you. Ko-Haw Nieh // This is a server // class TcpChannel and UdpChannel are C++ classes build on top of socket #include "tcp_udp_channel.h" #include <osfcn.h> main(int ii,char **argv){ TcpChannel tconnection,*tx; UdpChannel uconnection; int tfd,ufd; fd_set fdset,xxfdset; char buf[1000]; int size; int width; int xx; // argv[1] contains the host name uconnection.CreateService(argv[1],3503); // listen to this udp channel tconnection.CreateService(argv[1],3502); // listen to this tcp channel ufd = uconnection.Descriptor(); // get the udp descriptor tfd = tconnection.Descriptor(); // get the tcp descriptor FD_ZERO(&fdset); FD_SET(ufd,&fdset); FD_SET(tfd,&fdset); width = getdtablesize(); for(int j=0;;j++){ // main loop buf[0] = '\0'; if(select(width,&fdset,0,0,NULL) < 0){ // multiplex on udp and tcp channel perror("select error\n"); exit(1); } if(FD_ISSET(ufd,&fdset)){ // a udp packet is ready size = read(ufd,buf,500); buf[size] = '\0'; printf("UDP: %d:read %d characters, content = %s\n",j,size,buf); printf("------------------------\n"); } else{ // a tcp packet is ready if(FD_ISSET(tfd,&fdset)){ tx = tconnection.AcceptClient(); // returns a TcpChannel xx = tx->Descriptor(); // get the descriptor of the Channel FD_ZERO(&xxfdset); FD_SET(xx,&xxfdset); if(select(width,&xxfdset,&xxfdset,0,NULL) < 0){ perror("select error\n"); exit(1); } size = read(xx,buf,500); buf[size] = '\0'; printf("TCP: %d:read %d characters, content = %s\n",j,size,buf); printf("------------------------\n"); delete tx; // delete (close) the TcpChannel as well as its descriptor } else printf("a wrong descriptor is ready\n"); } } } -----------tcp_udp_channel.h------------------------ #ifndef tcp_udp_Channel_h #define tcp_udp_Channel_h #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <sys/un.h> #include <sys/ioctl.h> #include <errno.h> #include <fcntl.h> #include <string.h> #include <libc.h> #include <osfcn.h> typedef unsigned boolean; //static const boolean false = 0; //static const boolean true = 1; class TcpChannel { public: TcpChannel(); ~TcpChannel(); void CreateService(char* host, int port); // called by server boolean OpenService(char* host, int port); // called by client TcpChannel* AcceptClient(); int Read(void*, int); int Write(void*, int); int Descriptor () { return fd; } void Close(); protected: int fd; // file descriptor for read/write to the TcpChannel int domain; // UNIX domain or Internet domain char* name; // service name -- server site // host name -- client site int port; // Internet prot number, not used for UNIX domain }; class UdpChannel { public: UdpChannel(); ~UdpChannel(); void CreateService(char* host, int port); // called by server int OpenServiceAndSend(char* host, int port,char *buf,int z); int Read(void*, int); int Write(void*, int); int Descriptor () { return fd; } void Close(); protected: int fd; // file descriptor for read/write to the UdpChannel int domain; // UNIX domain or Internet domina char* name; // service name -- server site // host name -- client site int port; // Internet prot number, not used for UNIX domain }; #endif Ko-Haw Nieh, Litton/Industrial Automation 1301 Harbor Bay Parkway, P.O. Box 4004, Alameda, CA 94501-0404 uunet!fernwood!lia!nieh nieh@lia.com FAX : 415-521-5499 Tele : 415-769-5640