LQ Newbie
Registered: Jul 2007
Posts: 9
Rep:
|
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.
|