Sorry my first question is error; I want to ask for Swift Socket not Java
I am trying to write a Swift client app and connect to Java Server.
I don't know how to use Socket in Swift client.
I want to have like below Java function(out.println and in.readline)
THanks
The Java code was taken mostly from an Oracle example.
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
out.println("Hello World");
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
System.out.flush();
out.println("test");
if (inputLine.equals("Bye")) {
break;
}
}
out.close();
in.close();
socket.close();
Sorry for typing error question first time
Can u teach me where can I find how to implement by Swift in official website? or teach me how to implement ? thx much ><
my swift code is below, and I don't know how to get String form readline (inputStream).
And I write
outputstream.write(queryString,maxLength: queryString.characters.count)
and try to send to server.
But server always receives lost some content in sendString.
My server is ok in Android with Socket
let addr = "xxooo"
let port = 10009
var inp : NSInputStream?
var out : NSOutputStream?
NSStream.getStreamsToHostWithName(addr, port: port, inputStream: &inp, outputStream: &out)
print("test1")
let inputstream = inp!
let outputstream = out!
inputstream.open()
outputstream.open()
print("test2 queryString=\(queryString)")
outputstream.write(queryString,maxLength:queryString.characters.count)
print("test3 \(queryString.characters.count)")
let buffersize = 1024
var buffer = Array<UInt8>(count : buffersize,repeatedValue :0)
let bytesRead = inputstream.read(&buffer, maxLength: buffersize)
var getString : NSString?
print("test4")
if(bytesRead>0){
getString = NSString(bytes: &buffer, length: bytesRead, encoding: NSUTF8StringEncoding)
print("getString = \(getString!)")
}else{
}
inputstream.close()
outputstream.close()