2

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()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
beehuang
  • 339
  • 2
  • 18
  • TCP sockets absolutely don't care about the programming language on either end. – zapl Oct 13 '15 at 15:23
  • I know but I don't know Swift language how to implement. Can u teach me where can I find in official website? – beehuang Oct 13 '15 at 15:28
  • https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html – Ravindra babu Oct 13 '15 at 15:35
  • thx @ravindra , I read before , but I don't know how to implement now... Can u teach me example code > < By the way I want readline and out.printline PS: I want have simple code like in official website. Like below http://www.tutorialspoint.com/java/java_networking.htm – beehuang Oct 13 '15 at 15:43

1 Answers1

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

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Excuse me , maybe me English is not strong. I want to ask answer is Swift not Java. thx for your reply> – beehuang Oct 13 '15 at 15:21
  • QQ I found that my question is error, where can I edit ,I try to find now – beehuang Oct 13 '15 at 15:24
  • I had tried, but I don't know how to get String form readline (inputStream). And I write outputstream.write(sendString,maxLength: sendString.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 . thank u @ravindra – beehuang Oct 13 '15 at 16:23
  • Can you add your swift client code in the question? – Ravindra babu Oct 13 '15 at 16:33
  • Have a look at : stackoverflow.com/questions/28655425/how-to-test-if-my-remote-socket-nsstream-are-correctly-open – Ravindra babu Oct 14 '15 at 06:40
  • thank u I have tried,but not answer my question. that question is how to test are correctly open. this is not my question. my server have received data, but lost some content in sendString. I want to have like Java function(out.println and in.readline) in my question – beehuang Oct 14 '15 at 12:57
  • But you already have outputstream.write and inputstream.read which are equivalent to out.println and in.readLine – Ravindra babu Oct 14 '15 at 13:08
  • thank u help me, because u said outputstream.write and inputstream.read which are equivalent to out.println and in.readLine. I try to send English and success, but I change to Chinese it error... for example I want to send "哈囉我是中文國喔快收下我吧" But Server just receive 哈囉我是中......... – beehuang Oct 14 '15 at 13:45
  • Good to heat that it is working for english. You have to set character encoding. Look out for that api in swift. – Ravindra babu Oct 14 '15 at 13:48
  • I success reference by this and ravindra http://stackoverflow.com/questions/25554986/how-to-convert-string-to-unsafepointeruint8-and-length thank u again 3Q – beehuang Oct 14 '15 at 14:10
  • By the way, outputstream.write and inputstream.read which are "not" equivalent to out.println and in.readLine. Because println and readline use String in java, but outputstream.write and inputstream.read use byte. – beehuang Oct 16 '15 at 14:28