LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Socket programming: sending and receiving msgs back and forth (https://www.linuxquestions.org/questions/linux-networking-3/socket-programming-sending-and-receiving-msgs-back-and-forth-808837/)

ninja123 05-19-2010 08:16 AM

Socket programming: sending and receiving msgs back and forth
 
Hi all,

I have a server which is used by number of client Apps. Now when a client initiates a sendto request to a server it has to respond(after recvfrom) by sending a message using sendto back to client and this should be done back and forth.

What I dont understand is, does the client have to open a new port to receive packets from client? If yes, then how will server know to what port of the client(assuming server ports are well-known to clients)?

I had given up on sending messages back and forth using UDP and switched to TCP. I could send a read and write message back and forth. But the trouble comes when I have more than one client wanting to talk to server using SOCK_STREAM. The first one gets through but the second client seems to get blocked forever(but I got no error upon socket creation or upon connect with the server on the same port as the previous client)..

Can somebody throw some light??

Thanks for the help.

posixculprit 05-19-2010 08:30 AM

Show us the code!

If you're doing something along the lines of:

Code:

client_sock = accept(listen_sock, ...);
recvfrom(client_sock, ...);
sendto(client_sock, ...);

.. you're doing it wrong.

ninja123 05-20-2010 06:16 AM

Here is the client code:

Code:

//this is from header file
typedef struct{
        unsigned int permitFlag;
        char rmsg [32];
}ctrlMsg;

//client code

            int dataRead;
                ctrlMsg rmessage;
                int fd_client;
                struct sockaddr_in serv_addr;
                char buf[32];
                int check_ret;

                buf[0] = '\0';

                fd_client = socket(AF_INET, SOCK_STREAM, 0);
                if(fd_client < 0) {
                        printf("error creating socket. errno is %d\n", errno);
                        exit(1);
                }

                serv_addr.sin_family = AF_INET;
                serv_addr.sin_port = htons(PORT);
                serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

                bzero(&(serv_addr.sin_zero), 8);

                check_ret = connect(fd_client,
                                (struct sockaddr *) &serv_addr, ADDR_LEN);
                if(check_ret < 0) {
                        printf("Connect error. Errno is %d\n", errno);
                        exit(1);
                }

                while(1) {
                        strcpy(buf, "this is client");
                        buf[strlen(buf)] = '\0';
                        check_ret = write(fd_client, buf, 32);
                        if(check_ret < 0) {
                                printf("errno is %d\n", errno);
                        }
                        bzero(buf, 32);
                        check_ret = read(fd_client, &rmessage, sizeof(rmessage));
                        if(check_ret < 0) {
                                printf("errno is %d\n", errno);
                        }
                        if (strlen (rmessage.rmsg) > 0) {

                                dataRead = atoi(rmessage.rmsg);
                                printf ("Msg from server: \"%d\".\n", dataRead);
                                putring(dataRead);
                        }
                        delay(1000);
                }


ninja123 05-20-2010 06:27 AM

And here is the server code(UDP)

Code:

         
                char buf[32];
                ctrlMsg smessage;
                smessage.permitFlag = 1;
                uint16_t port_no = 2024;
                int v, sock, addr_len;
                struct sockaddr_in addr;
                if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
                exit(EXIT_FAILURE);
                memset(&addr, 0, sizeof(addr));
                addr.sin_family = AF_INET;
                addr.sin_addr.s_addr = INADDR_ANY;
                addr.sin_port = htons(port_no);

                if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
                exit(EXIT_FAILURE);
                printf("server : bind over\n");

                for (;;) {
                        /* do recvfrom
                        if ((v = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &addr_len)) > 0)
                        printf ("Message got from ODR: %s\n", buf);
                        bzero(buf,32);

                        strcpy(smessage.rmsg, “hello”);
                        if (sendto(sock, &smessage, sizeof(smessage), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
                        perror("sending datagram message");
                        memset(&smessage, '0', sizeof(smessage));
                }


ninja123 05-20-2010 06:30 AM

I was actually reading about select(). Is select to allow multiple sockets between server and client? But then, to have it working, should we know the number of clients talking to a server? That somehow sounds so wrong :( :(


All times are GMT -5. The time now is 02:37 AM.