Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, i'm making a messaging app using udp to handle clients and tcp to handle admin

struct sockaddr_in si_minhaTCP,si_minhaUDP, si_outra;
	int nready, maxfdp1;
	int fdTcp;
	int fdUdp;
	fd_set rset;
	if (argc != 4) {
    printf("server {client port(udp)} {admin port(tcp)} {registry file path}\n");
    exit(-1);
  }

    fdTcp = socket(AF_INET, SOCK_STREAM, 0);
    bzero(&si_minhaTCP, sizeof(si_minhaTCP));
    si_minhaTCP.sin_family = AF_INET;
    si_minhaTCP.sin_addr.s_addr = htonl(INADDR_ANY);
    si_minhaTCP.sin_port = htons((short) atoi(argv[2]));
    bind(fdTcp, (struct sockaddr*)&si_minhaTCP, sizeof(si_minhaTCP));
    listen(fdTcp, 10);


		
    fdUdp = socket(AF_INET, SOCK_DGRAM, 0);
	si_minhaUDP.sin_family = AF_INET;
    si_minhaUDP.sin_addr.s_addr = htonl(INADDR_ANY);
    si_minhaUDP.sin_port = htons((short) atoi(argv[1]));
    bind(fdUdp, (struct sockaddr*)&si_minhaUDP, sizeof(si_minhaUDP));


    FD_ZERO(&rset);

    maxfdp1 = max(fdTcp, fdUdp) + 1;
	for (;;)
	{
	  FD_SET(fdTcp, &rset);
      FD_SET(fdUdp, &rset);
      // select the ready descriptor

      nready = select(maxfdp1, &rset, NULL, NULL, NULL);
      if (FD_ISSET(fdTcp, &rset))
      {

          //handle tcp connection
      }
      if (FD_ISSET(fdUdp, &rset))
      {
          pid = fork();

			  if (pid == 0)
			  {

				   while(1)
                   {
                    //username

	 					if((recv_len = recvfrom(fdUdp, username, BUFLEN, 0, (struct sockaddr *) &si_outra, (socklen_t *)&slen)) == -1) {
	 					  erro("Erro recvfrom");
	 					}
	 					username[recv_len]='\0';
	 					//password
	 					if((recv_len = recvfrom(fdUdp, password, BUFLEN, 0, (struct sockaddr *) &si_outra, (socklen_t *)&slen)) == -1) 
                        {

	 					  erro("Erro no recvfrom");
	 					}
	 					password[recv_len]='\0';
	 					printf("Username received: %s\n" , username);
	 					printf("Password received: %s\n" , password);
                         
                        char msg[]="CONFIRMED";

						if(sendto(fdUdp, (const char *)msg, strlen(msg), 0, (struct sockaddr *) &si_outra, slen)==-1)
						{
							erro("Sendto");
						}

                        //here is the problem!!!!!
						char option[BUFLEN];
						if((recv_len = recvfrom(fdUdp, option, BUFLEN, 0, (struct sockaddr *) &si_outra, (socklen_t *)&slen)) == -1) 
                        {
							perror("Error: ");
						}
						option[recv_len]='\0';
						printf("option:%s\n",option);

                        //rest of code
                   }
              }
      }

	}




After the first sendto i can no longer receive anything from client, i dont get any error in client and i can see in wireshark that the client sended, but i never receive anything in the server

What I have tried:

I've tested receiving before the sendto and it receives just fine.
Posted
Updated 24-May-21 21:38pm
Comments
k5054 24-May-21 18:20pm    
If you're not seeing anything in wireshark, that suggest that the problem might be on the other end. Do you know its completed the read successfully on the opposite side, and has send back the confirmation?
José Silva 2021 24-May-21 19:10pm    
The client sends the option but the server never receives it, so the client continues running while the server remains locked trying to read
Greg Utas 25-May-21 8:32am    
It's hard to know what's going on with the information you've provided. Does the client get your sendto? What does the last recvfrom return, or doesn't it return at all? You say that the client sends, but does it send to the correct address and port? There are other, less likely possibilities. Are the declarations of username and password correct? Could you be using the same port for both TCP and UDP?

1 solution

My tip is that the receive is waiting to get the FULL buffer read - but it doenst get so much data. Read only 1 byte and use a loop.

The typical issue in such code is that one side is in some bad state, so it doesnt work like socket closed, blocked or left the read/write loop.

Take a look at this udp implementation to find your flaw.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900