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