Swift Socket readline writeline

2019-02-20 12:18发布

问题:

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()

回答1:

You have to flush() PrintWriter to receive data on other end of Socket. You are calling System.out.flush() and not PrintWriter out.flush()

out.flush() // This will do the trick for you.

Have a look this article regarding socket programming

EDIT:

In any programming language, the socket communication mechanism remains same except change in function names.

Server:

1) Create a ServerSocket

2) Accept a Client Socket in a infinite loop

3) Open OutputStream on Client Socket and write data.

4) Open InputStream to read the data

Client

1) Open a Socket to Server on a particular IP & port

2) Open OutputStream on the Socket

3) Write Data to the Stream (on wrapper of the Stream with Writers)

4) Open Input Stream to read the data

Have a look at this Question for swift client implementation of socket.

Have a look at Swift client and Java server article and SE question