SendString() public static method

Send a string along a socket
public static SendString ( Socket socket, String str ) : bool
socket System.Net.Sockets.Socket
str String
return bool
示例#1
0
        private void requestFile(String name)
        {
            writeMessage("Requesting file " + name);

            try {
                sendCommandToServer(Signal.clientWants);
                Common.SendString(socket, name);
            } catch (Exception e) {
                writeMessage("error requesting file: " + e.Message);
            }

            incommingFiles.Add(name);
        }
示例#2
0
        private void createDirectoryOnServer(String name)
        {
            writeMessage("Telling server to create directory " + name);

            try {
                sendCommandToServer(Signal.createDirectoryOnServer);
                Common.SendString(socket, name);
                // TODO: wait for reply to know that it was created on server before updating index
                fileIndex.Update(new MyFile(name, 'd', Common.GetModTime(dataDir + name), 0, "0"));
            } catch (Exception e) {
                writeMessage("error requesting server directory create: " + e.Message);
            }
        }
示例#3
0
        private void deleteOnServer(String name)
        {
            writeMessage("Telling server to delete item " + name);

            try {
                sendCommandToServer(Signal.deleteOnServer);
                Common.SendString(socket, name);

                // TODO: wait for reply before updating index
                fileIndex.Remove(name);
            } catch (Exception e) {
                writeMessage("error requesting server item delete " + e.Message);
            }
        }
示例#4
0
 /// <summary>
 /// Send catchup operation to the client based on the original inputOperation
 /// </summary>
 /// <param name="inputOperation">the initial operation for which to determine an output operation</param>
 /// <param name="arg">additional arguments for the input/output operation</param>
 public void SendCatchup(Signal inputOperation, String arg)
 {
     if (inputOperation == Signal.c2s)
     {
         try {
             Console.WriteLine("catchup s2c to client (" + handle + "): " + arg);
             if (File.Exists(dataDir + arg))
             {
                 sendCommandToClient(Signal.s2c);
                 Common.SendFile(arg, socket, dataDir);
             }
         }
         catch (Exception e) {
             Console.WriteLine("catchup s2c to client failed: " + e.Message);
         }
     }
     else if (inputOperation == Signal.deleteOnServer) // handles files and directories?
     {
         try {
             Console.WriteLine("catchup delete to client (" + handle + "): " + arg);
             sendCommandToClient(Signal.deleteOnClient);
             Common.SendString(socket, arg);
         }
         catch (Exception e) {
             Console.WriteLine("catchup delete to client failed: " + e.Message);
         }
     }
     else if (inputOperation == Signal.createDirectoryOnServer)
     {
         try {
             Console.WriteLine("catchup createDirectoryOnClient (" + handle + "): " + arg);
             sendCommandToClient(Signal.createDirectoryOnClient);
             Common.SendString(socket, arg);
         }
         catch (Exception e) {
             Console.WriteLine("catchup createDirectoryOnClient failed: " + e.Message);
         }
     }
     else
     {
         Console.WriteLine("unknown command: " + inputOperation);
     }
 }
示例#5
0
        /// <summary>
        /// Sends a message to the server and then periodically polls for the expected response.
        /// </summary>
        /// <param name="messageToServer"></param>
        /// <param name="expectedReturnCommand"></param>
        /// <param name="argument">Optional additional string to send along with the signal</param>
        /// <returns>true if the expected response returned before the wait expired</returns>
        private bool serverDiscussion(Signal messageToServer, Signal expectedReturnCommand, String argument)
        {
            int pollseconds = 1;  // TODO: make readonly globals
            int pollcount   = 30; // amount of times to poll

            writeMessage("serverDiscussion starting with expected return: " + expectedReturnCommand.ToString());
            writeMessage("serverDiscussion sending message to server: " + messageToServer.ToString());

            sendCommandToServer(messageToServer);
            if (argument != null)
            {
                //try {
                Common.SendString(socket, argument);
                //}
                //catch (Exception e) {
                //
                //}
            }

            for (int i = 0; i < pollcount; i++)
            {
                Thread.Sleep(pollseconds * 1000);

                if (expectedReturnCommand == lastReceivedOperation)
                {
                    writeMessage("serverDiscussion returning true with: " + lastReceivedOperation);
                    lastReceivedOperation = Signal.empty;
                    return(true);
                }
            }

            writeMessage("serverDiscussion returning false");
            lastReceivedOperation = Signal.empty;

            return(false);
        }
示例#6
0
        /// <summary>
        /// Handle input signals from the client
        /// </summary>
        /// <param name="signal"></param>
        private void handleInput(Signal signal)
        {
            Console.WriteLine("Handling input for signal " + signal);

            switch (signal)
            {
            case Signal.c2s:

                MyFile newFile = Common.ReceiveFile(socket, dataDir);

                if (newFile != null)
                {
                    server.serverDB.UpdateFile(User, newFile);
                }

                server.SpanCatchupOperation(handle, User.id, signal, newFile.name);
                break;

            //case Signal.clientWantsToSend:
            //  String relPath = Common.ReceiveString(socket);
            //  long timestamp = Common.ReceiveTimestamp(socket);

            //  sendCommandToClient(Signal.clientWantsToSend_response);

            //  Common.SendString(socket, relPath);

            //  // reply 'yes' if it refers to a file that does not exist or if the times do not match
            //  if (File.Exists(dataDir + relPath) && Common.GetModTime(dataDir + relPath) == timestamp) {
            //    Common.SendString(socket, "no");
            //  }
            //  else {
            //    Common.SendString(socket, "yes");
            //  }
            //  break;

            case Signal.clientWants:
                String relPath = Common.ReceiveString(socket);
                if (File.Exists(dataDir + relPath))
                {
                    outQueue.Enqueue(relPath);
                    processOutQueue();
                }
                break;

            case Signal.deleteOnServer:
                relPath = Common.ReceiveString(socket);

                if (Common.DeleteLocal(dataDir + relPath))
                {
                    server.serverDB.RemoveFile(User, relPath);
                }
//            index.Remove(relPath);  // TODO: check return value

                server.SpanCatchupOperation(handle, User.id, signal, relPath);
                break;

            case Signal.createDirectoryOnServer:
                relPath = Common.ReceiveString(socket);

                if (Common.CreateLocalDirectory(dataDir + relPath))
                {
                    server.serverDB.UpdateFile(User, new MyFile(relPath, 'd', Common.GetModTime(dataDir + relPath)
                                                                , 0, "0"));
                }

                server.SpanCatchupOperation(handle, User.id, signal, relPath);
                break;

            case Signal.requestServerFileList:

                List <List <string> > fileListToSerialize = server.serverDB.GetFileListSerializable(User);

                String jsonOutStringFiles = JsonConvert.SerializeObject(fileListToSerialize);

                Console.WriteLine("sending json file list: " + jsonOutStringFiles);

                try {
                    sendCommandToClient(Signal.requestServerFileList_response);
                    Common.SendString(socket, jsonOutStringFiles);
                }
                catch (Exception e) {
                    Console.WriteLine("Error during " + Signal.requestServerFileList_response + e.Message);
                    Common.ExitError();
                }

                break;

            case Signal.attachaccount:

                String args = Common.ReceiveString(socket);

                Console.WriteLine("received " + args);

                List <string> attachInput = JsonConvert.DeserializeObject <List <string> >(args);

                String userName = attachInput[0];
                String password = attachInput[1];

                Dictionary <string, string> jsonOut = new Dictionary <string, string>();
                jsonOut.Add("serverMyboxVersion", Common.AppVersion);

                if (attachUser(userName, password))
                {
                    jsonOut.Add("status", "success");
                    //jsonOut.Add("quota", Account.quota.ToString());
                    //jsonOut.Add("salt", Account.salt);

                    server.AddToMultiMap(User.id, handle);
                }
                else
                {
                    jsonOut.Add("status", "failed");
                    jsonOut.Add("error", "login invalid");

                    close();
                    // TODO: disconnect the client here
                }

                String jsonOutString = JsonConvert.SerializeObject(jsonOut);

                try {
                    sendCommandToClient(Signal.attachaccount_response);
                    Common.SendString(socket, jsonOutString);
                }
                catch (Exception e) {
                    Console.WriteLine("Error during " + Signal.attachaccount_response + e.Message);
                    Common.ExitError();
                }

                Console.WriteLine("attachaccount_response: " + jsonOutString);

                break;

            default:
                Console.WriteLine("Unknown command");
                break;
            }
        }