| | |
Regarding client-server ???
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 297
Reputation:
Solved Threads: 11
I am creating client -server architecture ...
1 server and 20 clients..are attached ...
when computer starts client sends abc.txt file information to server ...through socket port no 8000...server take this file and rename with that comp name...
problem is ->
when number of clients machine on at same time ..they sends information to server and server gets mixed output....socket is same.
how to solve this problem so server gets output individually for client??
Do i need to create a separate Server for every Client,or is any other way to archive this ??
1 server and 20 clients..are attached ...
when computer starts client sends abc.txt file information to server ...through socket port no 8000...server take this file and rename with that comp name...
problem is ->
when number of clients machine on at same time ..they sends information to server and server gets mixed output....socket is same.
how to solve this problem so server gets output individually for client??
Do i need to create a separate Server for every Client,or is any other way to archive this ??
•
•
Join Date: Apr 2008
Posts: 297
Reputation:
Solved Threads: 11
C++ Syntax (Toggle Plain Text)
#include<conio.h> #include <stdio.h> #include "winsock2.h" #include<iostream> #include<fstream> #include <stdlib.h> #include<string> using namespace std; int i=1; int main() { WSADATA wsaData; SOCKET RecvSocket; sockaddr_in RecvAddr; int Port = 8000; char RecvBuf[26000]; int BufLen = 26000; ifstream in_file; ofstream out_file; in_file.close(); in_file.clear(); out_file.close(); out_file.clear(); size_t counter(0); // how many found string if_name="xyz.txt"; string of_name="abc.txt"; char my_char; char firstipadd[20] ; int result,p; char* ipadd; string s; sockaddr_in SenderAddr; int SenderAddrSize = sizeof(SenderAddr); // Initialize Winsock WSAStartup(MAKEWORD(2,2), &wsaData); while(1) { in_file.close(); in_file.clear(); out_file.close(); out_file.clear(); char fname[30] = "C:\\" ; //----------------------------------------------- // Create a receiver socket to receive datagrams RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //----------------------------------------------- // Bind the socket to any address and the specified port. RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr)); recvfrom(RecvSocket,RecvBuf,BufLen, 0,(SOCKADDR *)&SenderAddr, &SenderAddrSize); FILE * pFile; string aa; pFile = fopen ("c:\\xyz.txt" , "w"); fwrite (RecvBuf , 1 ,sizeof(RecvBuf) , pFile); fclose (pFile); //------------------- printf("Socket close"); closesocket(RecvSocket); } // Clean up and exit. printf("Exiting.\n"); WSACleanup(); getch(); }
Last edited by Ancient Dragon; Jun 9th, 2008 at 8:15 am. Reason: add code tags
•
•
Join Date: Nov 2007
Posts: 982
Reputation:
Solved Threads: 210
Shouldn't you by now be familiar with code tags??
http://www.daniweb.com/forums/misc-explaincode.html
http://www.daniweb.com/forums/misc-explaincode.html
•
•
Join Date: Apr 2008
Posts: 297
Reputation:
Solved Threads: 11
C++ Syntax (Toggle Plain Text)
#include<conio.h> #include <stdio.h> #include "winsock2.h" #include<iostream> #include<fstream> #include <stdlib.h> #include<string> using namespace std; int i=1; int main() { WSADATA wsaData; SOCKET RecvSocket; sockaddr_in RecvAddr; int Port = 8000; char RecvBuf[26000]; int BufLen = 26000; ifstream in_file; ofstream out_file; in_file.close(); in_file.clear(); out_file.close(); out_file.clear(); size_t counter(0); // how many found string if_name="xyz.txt"; string of_name="abc.txt"; char my_char; char firstipadd[20] ; int result,p; char* ipadd; string s; sockaddr_in SenderAddr; int SenderAddrSize = sizeof(SenderAddr); // Initialize Winsock WSAStartup(MAKEWORD(2,2), &wsaData); while(1) { in_file.close(); in_file.clear(); out_file.close(); out_file.clear(); char fname[30] = "C:\\" ; //----------------------------------------------- // Create a receiver socket to receive datagrams RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //----------------------------------------------- // Bind the socket to any address and the specified port. RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr)); recvfrom(RecvSocket,RecvBuf,BufLen, 0,(SOCKADDR *)&SenderAddr, &SenderAddrSize); FILE * pFile; string aa; pFile = fopen ("c:\\xyz.txt" , "w"); fwrite (RecvBuf , 1 ,sizeof(RecvBuf) , pFile); fclose (pFile); //------------------- printf("Socket close"); closesocket(RecvSocket); } // Clean up and exit. printf("Exiting.\n"); WSACleanup(); getch(); }
is Multi-Threaded architecture works for it??
how??
>>how to solve this problem so server gets output individually for client??
Each client needs to use different ports. You can reserve a range of ports in the hosts file on the server computer so that nothing else on the server will take them. Assign port no 8000 to client #1, port 8001 to client #2, etc. Then in server side when recvfrom() gets something from client, server kicks off a thread to process the info so that the server can go back to recvfrom() without blocking.
Each client needs to use different ports. You can reserve a range of ports in the hosts file on the server computer so that nothing else on the server will take them. Assign port no 8000 to client #1, port 8001 to client #2, etc. Then in server side when recvfrom() gets something from client, server kicks off a thread to process the info so that the server can go back to recvfrom() without blocking.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
![]() |
Similar Threads
- Simple Client Server Instant Messaging Java code using UDP datagrams (Java)
- Porbs in Client-Server (Java)
- Client-server messenger (Java)
- URGENT: FTP Client / Server using RMI (Java)
- client server communication problem in CSocket program (C)
- Problem with Client-Server Socket Connection (Java)
- Client Server Applications (Pascal and Delphi)
- java client server quiz. interesting!!! (Java)
- Client-server vs RMI using Java (Java)
Other Threads in the C++ Forum
- Previous Thread: Algorithms to work logical operations using STLs
- Next Thread: Using ASIO Audio drivers
Views: 845 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






