Understood with SwiftSocket library, I send request to the server, I receive the answer. But there is one thing! I tried to run the program several times in a row and saw that sometimes incomplete JSON . I know that 385 objects should come to me, in most cases this is what happens, but sometimes the last piece is missing - for example, 340 can come and that's all. Here is a piece of code where I access the server and read the answer:
var client:TCPClient = TCPClient(addr: "myIP", port: myPort) var (success, errmsg) = client.connect(timeout: 1) if success { var (success, errmsg) = client.send(str:"<Command=GetStudentList>\n" ) if success { while(true) { var data = client.read(2048*10) if (data == nil) { break } if let d = data { if let str = String(bytes: d, encoding: NSUTF8StringEncoding){ print(str) } } } }else { print(errmsg) } } else { print(errmsg) } } What could be the problem?
Here is the server code:
int port = 8005; TcpListener listener = new TcpListener(IPAddress.Any, port); bool fl = true; try { listener.Start(); Console.WriteLine("Server is running, waiting connections..."); while (fl) { const int bufferSize = 8192; TcpClient client = listener.AcceptTcpClient(); NetworkStream clientStream = client.GetStream(); Console.WriteLine("Have a client!!"); byte[] buffer = new byte[bufferSize]; int readBytes = 0; MemoryStream memStream = new MemoryStream(); do { readBytes = clientStream.Read(buffer, 0, bufferSize); memStream.Write(buffer, 0, readBytes); } while (clientStream.DataAvailable); buffer = memStream.GetBuffer(); string message = Encoding.UTF8.GetString(buffer); Console.WriteLine(message); string command = message.Substring(message.IndexOf('<'), message.IndexOf('>') + 1); message = message.Remove(0, message.IndexOf('>') + 1); string answer = ""; switch (command) { case "<Command=GetStudentList>": { Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Request received {0}. Doing...", command); answer = DataBaseRequest.GetStudentList(); //fl = false; break; } case "<Command=SendEvent>": { Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Request received {0}. Doing...", command); answer = DataBaseRequest.SendEventToDataBase(message); //fl = false; break; } case "<Command=SendMessage>": { Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Request received {0}. Doing...", command); answer = DataBaseRequest.SendMessageToDataBase(message); break; } case "<Command=SendDeadline>": { Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Request received {0}. Doing...", command); answer = DataBaseRequest.SendDeadlineToDataBase(message); break; } case "<Command=SendTask>": { Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Request received {0}. Doing...", command); answer = DataBaseRequest.SendTaskToDataBase(message); break; } case "<Command=CheckUser>": { Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Request received {0}. Doing...", command); answer = DataBaseRequest.CheckUser(message); break; } } Console.WriteLine(DateTime.Now.ToShortTimeString() + ":" + "Using the database is successfully completed, there is a sending client response."); byte[] responseBuffer = Encoding.UTF8.GetBytes(answer); clientStream.Write(responseBuffer, 0, responseBuffer.Length); clientStream.Close(); Console.WriteLine(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } The server is spinning on virtualke from Amazon - EC2
if let str = String(bytes: d, encoding: NSUTF8StringEncoding)Moreover, I have a WinForms client, which sends exactly the same request when it starts up, and that's where the data always comes all the way. - Andrey Morozovdata, further actions take place. As soon as there was nothing counted - exit. What do you mean by "Need to read correctly"? - Andrey Morozov