LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-28-2009, 09:42 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
a new problem in multi thread server -client problem


Hi to all,


I wrote a server program to communicate with multi clients. each client has to handled by each thread with different port numbers.
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

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
static int port =6000;

void createsocket()
{
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);
}
pthread_mutex_lock( &mutex1 );
port++;
pthread_mutex_unlock( &mutex1 );
memset(&server_addr, 0, sizeof(server_addr));
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 tid = (int)arg;

//int portid = 6500;

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


createsocket();

}




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

pthread_t tid[MAX_THREAD];
int i =0;

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 *)i);
}

return 0;
}
</code>

the output is :

0 thread created
0 thread created
1 thread created
PORT nu is 6000

IPM agent Waiting for client on port 6002
2 thread created
PORT nu is 6002

IPM agent Waiting for client on port 6004
3 thread created
PORT nu is 6004

IPM agent Waiting for client on port 6006
PORT nu is 6006

IPM agent Waiting for client on port 6008
..


my intension is that thread has to wait for connection after accept(). but it is not happening like that.. can u people give idea to accept the connection.

THANKX IN ADVANCE..
 
  


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
openvpn ipaddress problem: "MULTI: bad source address from client" arobinson74 Linux - Networking 6 10-02-2008 07:26 PM
Multi-thread execution efficiency problem cdcshu Linux - Software 3 08-02-2007 01:46 AM
problem with client-server himanshu Programming 2 04-21-2005 09:34 AM
Multi-user web server problem frgtn Linux - Security 15 04-13-2005 10:58 AM
multi-thread VS multi-process ltcstyle Programming 3 12-04-2004 07:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:35 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