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 ??

Recommended Answers

All 6 Replies

Could you show some code from the server part? Only the connecting bit for example?
Also: what OS are you using?

#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();
}

i m using. XP...
server is continuously running ....
i m forget to add renaming of file code...

#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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.