LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-17-2007, 12:47 AM   #1
rajprabhu2k
LQ Newbie
 
Registered: Jul 2007
Posts: 9

Rep: Reputation: 0
How to create concurrent Server


Hi,

I am developing a client/server Quiz game using UDP , when the game starts the server should send questions to the client

which is connected to the server.
my logic is when the client A request server for the first time,the server should create a new thread and server for the

client A and sends the port and inet address to the clientA ,Now new server port and inet address will be set in the clientA.

Now clientA will start communicate with the new server. Main server will listen to another client , similarly same process

will be done.

My problem is when i create new server for the client , the socket not get binded i dont know why ? the code is given below.

Please help me

//*** Server Code **********************
struct ThreadArg{
struct sockaddr_in udpClient;
string FileName;
int udpSocket;
int NoClient;
int port;
};


int TransferFile(int udpSocket,int nport,struct sockaddr_in udpClient) {

char buf[4096]; /* I/O Buffer */
int returnStatus(0);
/* a message was received so send a confirmation */
strcpy(buf, "Message from Server OK");
int Send = 1; int NoSend(0);
sprintf(buf,"%d",nport);

//** Sending new port to the client **
returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0,
(struct sockaddr*)&udpClient, sizeof(udpClient));

//*** Creating New Server ***
close(udpSocket);
int NewSocket;
struct sockaddr_in udpClient2;
struct sockaddr_in udpServer2;
udpServer2.sin_family = AF_INET;
udpServer2.sin_addr.s_addr = inet_addr("127.0.0.2");
//* use the port passed as argument
udpServer2.sin_port = htons(nport);

returnStatus = bind(NewSocket,(struct sockaddr*)&udpServer2, sizeof(udpServer2));
if (returnStatus == 0) {
fprintf(stderr, "Bind completed!\n");
}
else {
fprintf(stderr, "Could not bind to address!\n");
close(NewSocket);
//exit(1);
}


while(1) {

if(Send == 1) {
returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0,
(struct sockaddr*)&udpClient, sizeof(udpClient));
strcpy(buf,"");
NoSend++;
Send = 0;
}

returnStatus = recvfrom(udpSocket, buf, MAXBUF, 0,
(struct sockaddr*)&udpClient, (socklen_t*) sizeof(udpClient));

int Len = strlen(buf);
if(Len > 1) {
printf("%s..\n",buf);
Send = 1;
sprintf(buf,"Message from Server - %d",NoSend);

}

}

}




void *CreateNewServer(void *arg) {
printf("Thread is created\n");
ThreadArg *TAObj;
TAObj = (ThreadArg*) arg;
TransferFile(TAObj->udpSocket,TAObj->port,TAObj->udpClient);
}



int main() {

int udpSocket;
int nport = 4001;
int returnStatus = 0;
socklen_t addrlen = 0;
struct sockaddr_in udpServer, udpClient;

char buf[4096]; /* I/O Buffer */


/* create a socket */
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket == -1) {
fprintf(stderr, "Could not create a socket!\n");
exit(1);
}
else {
printf("Socket created.\n");
}

/* set up the server address and port */
/* use INADDR_ANY to bind to all local addresses */
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr("127.0.0.1");//htonl(INADDR_ANY);
/* use the port passed as argument */
udpServer.sin_port = htons(nport);


/* bind to the socket */
returnStatus = bind(udpSocket, (struct sockaddr*)&udpServer, sizeof(udpServer));
if (returnStatus == 0) {
fprintf(stderr, "Bind completed!\n");
}
else {
fprintf(stderr, "Could not bind to address!\n");
close(udpSocket);
exit(1);
}



//********************************************
int ClientRequested(0);
pthread_t thread[10];
int iret1(0);

int Client(0);


while (1) {

addrlen = sizeof(udpClient);
ClientRequested = recvfrom(udpSocket, buf, MAXBUF, 0,
(struct sockaddr*)&udpClient, &addrlen);

if( ClientRequested ) {
printf("Inside Condition\n");
Client++;

ThreadArg TAObj;// = new ThreadArg ();;
TAObj.udpClient = udpClient;
TAObj.udpSocket = udpSocket;
TAObj.NoClient = Client;
TAObj.FileName = buf;
TAObj.port = ++nport;

iret1 = pthread_create(&thread[Client - 1], NULL, CreateNewServer, &TAObj);
pthread_join(thread[Client - 1],NULL);

ClientRequested = 0;
close(udpSocket);
printf("Waiting.....\n");

}
}

/*cleanup */
close(udpSocket);
return 0;

}


//**** Client Code ***


int main() {

int port = 4001;
int udpSocket;
int returnStatus = 0;
socklen_t addrlen = 0;
struct sockaddr_in udpServer, udpClient;
char buf[MAXBUF];

/* create a socket */
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket == -1)
{
fprintf(stderr, "Could not create a socket!\n");
exit(1);
}
else {
printf("Socket created.\n");
}
/* client address */
/* use INADDR_ANY to use all local addresses */
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = htonl(INADDR_ANY);
udpClient.sin_port = htons(port);

returnStatus = bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));

if (returnStatus == 0) {
fprintf(stderr, "Bind completed!\n");
}
else {
fprintf(stderr, "Could not bind to address!\n");
close(udpSocket);
exit(1);
}

/* set up the message to be sent to the server */
strcpy(buf, "For Professionals, By Professionals.\n");

/* server address */
/* use the command-line arguments */
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr("127.0.0.1"); //htonl(INADDR_ANY);
udpServer.sin_port = htons(port);



printf("Previous Server Port -> %d\n",port);
returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0,
(struct sockaddr*)&udpServer, sizeof(udpServer));

if (returnStatus == -1) {
fprintf(stderr, "Could not send message!\n");
}
else {
printf("Message sent.\n");
/* message sent: look for confirmation */
addrlen = sizeof(udpServer);

returnStatus = recvfrom(udpSocket, buf, MAXBUF, 0,
(struct sockaddr*)&udpServer, &addrlen);

printf("Message Received from Server -> %s\n",buf);

int nPort = atoi(buf);
close(udpSocket);
udpServer.sin_port = htons(nPort);
returnStatus = bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));

int Recv(1);

while(1) {
int choice(0);
printf("Enter Choice 1 or 2 ");
cin>>choice;

if(choice == 1) {
sprintf(buf,"Message from Client -> Hello World %d",Recv);
returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0,
(struct sockaddr*)&udpServer, sizeof(udpServer));
Recv++;
}

returnStatus = recvfrom(udpSocket, buf, MAXBUF, 0,
(struct sockaddr*)&udpServer, &addrlen);

if (returnStatus == -1) {
fprintf(stderr, "Did not receive confirmation!\n");
}

else {
buf[returnStatus] = 0;
printf("Message from Server : %s\n", buf);
}
}
}

/*cleanup */
close(udpSocket);
return 0;
}

Last edited by rajprabhu2k; 08-17-2007 at 12:49 AM.
 
Old 08-17-2007, 02:03 AM   #2
f2003800
LQ Newbie
 
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
Why binding fails?

The reason for binding failure is.
1)The port which you have chosen (i.e. nPort value) is already in use. someone may be using that port.
In this case you can't bind to that port.
What you can do is, let the system chose arbitrary ephemeral port for you and then you get it through some calls and send it to the client..


is this helpful???
 
Old 08-17-2007, 02:06 AM   #3
rajprabhu2k
LQ Newbie
 
Registered: Jul 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks , I think it will be helpful

Last edited by rajprabhu2k; 08-17-2007 at 03:15 AM.
 
Old 08-17-2007, 02:27 AM   #4
rajprabhu2k
LQ Newbie
 
Registered: Jul 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Hi , I assigned arbitrary port still its not get binded. for setting arbitrary port i used htons(0).
 
Old 08-17-2007, 04:33 PM   #5
frankjp
LQ Newbie
 
Registered: Jun 2005
Location: Around the world
Distribution: Fedora Core 2 3 4 and Freebsd 5
Posts: 10

Rep: Reputation: 0
Hi, I think you should use setsocket to allow you to bind at the same port under concurrent server.

#include <sys/socket.h>

setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen);

Option
SO_REUSEADDR
Specifies that the rules used in validating addresses supplied to bind() should allow reuse of local addresses, if this is supported by the protocol. This option takes an int value in the optval argument. This is a BOOL option.

Hoping that it will help you.

Last edited by frankjp; 08-17-2007 at 04:40 PM.
 
Old 08-21-2007, 08:01 AM   #6
rajprabhu2k
LQ Newbie
 
Registered: Jul 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you very much, it was helpful
 
Old 08-21-2007, 08:12 AM   #7
frankjp
LQ Newbie
 
Registered: Jun 2005
Location: Around the world
Distribution: Fedora Core 2 3 4 and Freebsd 5
Posts: 10

Rep: Reputation: 0
Hi, Happy to know. Enjoy.
Frank
 
  


Reply



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
Concurrent Users Sizing gerrard8 Linux - General 1 01-13-2006 08:51 AM
limiting concurrent webmin duncanbball Linux - Software 0 10-13-2004 03:16 PM
How to create a Mail server on Suse Linux Standard Server 8 DavidPartay Linux - Networking 1 06-29-2004 06:49 PM
Using sound device concurrent Sammy2ooo Linux - General 3 03-07-2004 02:41 PM
3 concurrent logins 4 same user? tyccea Red Hat 4 10-24-2003 01:07 AM

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

All times are GMT -5. The time now is 06:15 PM.

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