Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CAN someone convert this to cpp? buffer_size = 512 #constantly loop for incommin

ID: 3904057 • Letter: C

Question

CAN someone convert this to cpp?

buffer_size = 512
#constantly loop for incomming server connections
while (True):
#set up socket to listen on port 6566
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("",6566))
s.listen(1)

#accept connection from RUDICS service print "Accepting socket."
conn, addr = s.accept()
print "Socket accepted."
#while connection is made, receive 512 bytes at a time
#need to add for EOF, while loop doesn't quit when client is done
while (True):
#read the recieved 512 bytes
data = conn.recv(buffer_size)
#if command 'File' is given, expect to recieve a file
if data == "File" or "File ":
try:
#get file name
filename = conn.recv(buffer_size)
file = open(str(filename), 'w')
print "Created "gmail-pl-pds">" + str(filename)
#start recording data until 'EOF' is recieved while (True):
try:
data = conn.recv(buffer_size)
if data == "EOF" or "EOF" in data:
break
file.write(data)
print "Writing " + str(sys.getsizeof(data)) + " bytes of data..."
except:
s.close()
conn.close()
print "Error: Problem reading data and writingI8 to file."
exit()
#done writing to file
file.close()
print "File completely transfered!"
except:
s.close()
conn.close()
print "Error: Problem reading data and writing to file. 2."

exit()

8
#if command 'closecall' is given, end the call elif data == "closecall":
print "Ending call. 'closecall' command recieved."
s.close()
conn.close()
break
#if no command is given, end the call
elif data == "":
print "No data received. Ending call."
s.close()
conn.close()
break
#if an unknown command is given, print it and close the call
else:
print "Unformatted data received. Ending call."
print "Data: " + str(data)
s.close()
conn.close()
break

Explanation / Answer

buffer_size = 512;
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( 6566 );


#constantly loop for incomming server connections
while (true){
    #set up socket to listen on port 6566
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET,     socket.SO_REUSEADDR, 1)
    //s.bind(("",6566));
    if (bind(s, (struct sockaddr *)&address, sizeof(address))<0)
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    //s.listen(1)
    if (listen(s, 3) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    //accept connection from RUDICS service print "Accepting socket."
    //conn
    int conn = s.accept()
    //print "Socket accepted."
    cout << "Socket accepted ";
    #while connection is made, receive 512 bytes at a time
    #need to add for EOF, while loop doesn't quit when client is done
    int buffer_size = 512;
    char data[buffer_size];
    while (true){
          //read the recieved 512 bytes
          //data = conn.recv(buffer_size)
          int valread = read( conn , data, buffer_size);
          //if command 'File' is given, expect to recieve a file

          string filedata = "";
          if (strcmp(data,"File") == 0 || strcmp(data,"File ") == 0){
             string filedata = "";
             try{
                   #get file name
                   valread = read( conn , data, buffer_size);
                   ofstream file(data);
                   cout << "Created "pl-pds">" << data << endl;
                   #start recording data until 'EOF' is recieved while (True):
                   try {
                       while(true){
                           valread = read( conn , data, buffer_size);
                           filedata = filedata + data;
                           ch = strtok(data,"EOF");
                           if (strcmp(data,"EOF") == 0 ||ch != NULL):
                               break;
                      }
                      file << filedata << endl;

                      cout << "Writing " << filedata.length() << " bytes of data...";
                  }
                  catch(Exception e){
                       close(s);
                       close(conn);
                       cout << "Error: Problem reading data and writingI8 to file." << endl
                       exit();
                  }
                  #done writing to file
                  file.close();
                  cout << "File completely transfered!" << endl;
              }
              catch (Exception e){
                   close(s);
                   close(conn);
                   cout << "Error: Problem reading data and writing to file. 2." << endl
                   exit;
              }
          }
          if (strcmp(data,"closecall") == 0){
             //if command 'closecall' is given, end the call elif data ==                           //"closecall":coutrint "Ending cout << " closecall command recieved." << endl;
             close(s);
             close(conn);
             break
          }
          //if no command is given, end the call
          else if (strcmp(data,"") == 0){
              cout << "No data received. Ending call." << endl;
              close(s);
              close(conn);
              break
          }
          #if an unknown command is given, print it and close the call
          else{
             cout << "Unformatted data received. Ending call." << endl;
             cout << "Data: " << strlen(data) << endl;
             close(s);
             close(conn);
             break;
          }
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote