LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2009, 04:12 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
problem in multi thread server program


Hi,



I have written a multithread server program that each thread has to communicate with single client. so while accepting the connection it has to wait for connection right? but it is not waiting for connection. my program as follows:


Code:
#include<fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <wchar.h>
#include <pthread.h>

#define MAX_THREAD 12
#define MAX_CLIENT_BUFFER 409



void createsocket(int port)
{
        unsigned int len, bytes_received;
        char recv_data[256];
        struct sockaddr_in server_addr, client_addr;
        static int sock, connected, sin_size;

        printf("PORT nu is %d\n", port);
        if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("Socket");
                exit(1);
        }

        memset(&server_addr, 0, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(port)
server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(port);
        server_addr.sin_addr.s_addr = INADDR_ANY;

        if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
        {
                perror("Unable to bind");
                exit(1);
        }

        if (listen(sock, 2) == -1)
        {
                 perror("Listen");
                 exit(1);
        }
        printf("\nIPM agent Waiting for client on port %d\n", ntohs(server_addr.sin_port));

        while(1)
        {

                connected = accept(sock,(struct sockaddr *)&client_addr,&sin_size);

                if(connected == -1)

                {
                        printf("connection with bad file descriptor\n");
                        exit(1);
                }

                printf("\n I got a connection from (%s , %d)",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

                while(1)
                {

                        bytes_received = recv(connected,(char *)&len,4,0);//receive msglength

                        printf("\n RECIEVED BUFFER LENGTH = %d\n" ,bytes_received);
                                                                                   memset(recv_data, 0, sizeof(recv_data));

                        bytes_received = recv(connected,(char*)recv_data,ntohl(len),0);//receive msg

                        if((bytes_received == 0) ||(bytes_received == -1))

                        {
                        perror("recv:");

                        }

                        recv_data[bytes_received] = '\0';

                        printf("\n RECIEVED DATA = %s\n " , recv_data);

                }
        }
close(sock);
}



void *thread_func(void *arg)
{
        int portid = (int)arg;


        //printf("%d thread created\n", tid);

        //portid++;

        createsocket(portid);

}



int main(int argc , char* argv[])
{

        pthread_t tid[MAX_THREAD];
        int i =0;
        int portnu = 6500;

        if(argc != 3)
        {
                printf("Give number of agents and port number as command line arguments\n");

                exit(1);

        }

        for(i = 0; i < atoi(argv[1]); i++)
        {
                pthread_create(&tid[i], NULL, &thread_func, (void *)portnu++);
        }

return 0;
}

the output as follows:
PORT nu is 6500
PORT nu is 6500
[naveen@vinlabsmyspc13 TEST]$ ./a.out 7 6500
PORT nu is 6500

IPM agent Waiting for client on port 6500
PORT nu is 6501

IPM agent Waiting for client on port 6501
PORT nu is 6502

IPM agent Waiting for client on port 6502
PORT nu is 6503
PORT nu is 6504

IPM agent Waiting for client on port 6503

IPM agent Waiting for client on port 6504
PORT nu is 6505
PORT nu is 6506

IPM agent Waiting for client on port 6505

IPM agent Waiting for client on port 6506


pls suggest how to make accept to wait for connection.

I will give stars for suitable replies.

THANKS IN ADVANCE

Last edited by naveenisback; 07-29-2009 at 08:37 AM. Reason: found answer
 
Old 07-29-2009, 06:55 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Originally Posted by naveenisback View Post
I will give stars for suitable replies
i dont even know what this means, but i dont think you have to try to bribe people to help you.

from your output and a quick look at your code, it appears that your program is waiting for a client connection before it continues.

if you mean you want a connection to be established/client to connect before you create the next thread, then you have to do the socket setup and accept call in main, before you create the next thread.

if this isnt what you want, then please elaborate on what you mean.

edit:
your output sample
Quote:
PORT nu is 6500
PORT nu is 6500
[naveen@vinlabsmyspc13 TEST]$ ./a.out 7 6500
PORT nu is 6500
can be quite misleading. i would either kill your previous processes/threads of 'a.out' or open a new console window, run 'a.out', and copy only that output. from this output it seems that you ran the program twice, so its hard to tell what is happening.

Last edited by nadroj; 07-29-2009 at 06:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the priority of process that contain multi-thread? aixilin Programming 5 10-04-2012 02:27 PM
a new problem in multi thread server -client problem naveenisback Linux - Software 0 07-28-2009 08:42 AM
Is there an ftp program that allows for multi-thread ftp uploads ? Want faster upload brjoon1021 Linux - Software 4 02-04-2009 06:28 PM
Multi-thread execution efficiency problem cdcshu Linux - Software 3 08-02-2007 12:46 AM
multi-thread VS multi-process ltcstyle Programming 3 12-04-2004 06:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration