0

This is the code in Java to make the socket call, but I want to know how can I replicate this or something similar in iOS (Swift or Objective-C)

public String MakeSocketRequest() {
    DataInputStream inputSt;
    DataOutputStream outputSt;
    Socket socket = new Socket(InetAddress.getByName("socketurl.io"), 40008);
    String jsonStr = "{\"id\":1,\"method\":\"themethod\"}";
    inputSt = new DataInputStream(socket.getInputStream());
    outputSt = new DataOutputStream(socket.getOutputStream());
    PrintWriter pw = new PrintWriter(outputSt);
    pw.println(string);
    Log.d("PrintWriter", jsonStr);
    pw.flush();
    BufferedReader bfr = new BufferedReader(new InputStreamReader(inputSt));
    JSONObject json = new JSONObject(bfr.readLine());
    Log.d("Json", json.toString());
    inputSt.close();
    outputSt.close();
    return json.toString();}
Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
Fertos
  • 63
  • 1
  • 10
  • https://stackoverflow.com/a/50062606/5461400 – Harshal Valanda May 05 '18 at 07:24
  • Personally, I'd prefer to use [CocoaAsyncSocket](https://github.com/robbiehanson/CocoaAsyncSocket) - it can take a little getting your head around, but it's also extremely powerful – MadProgrammer May 05 '18 at 07:43
  • RocketSoket,Soket.io,etc libraries are there to help us out https://github.com/facebook/SocketRocket#readme , https://github.com/socketio/socket.io-client-swift – ketaki Damale May 05 '18 at 10:42
  • Thank you so much for your recommendations guys!!. I got it work with CocoaAsyncSocket. – Fertos May 05 '18 at 21:52

1 Answers1

1

If you want to do it natively without 3rd-party libraries, then you can use CFStreamCreatePairWithSocketToHost function to create input and output streams (no socket object is needed).

Here's some example code to set this up
And the search shows many more

On iOS you can't write or read the streams immediately, and you have to wait until the socket is connected, and you get a permission to read/write. This is done by implementing NSStreamDelegate.

If you get NSStreamEventHasSpaceAvailable event there, you can write your string to the output stream. You don't need a PrintWriter to just write a string, because it is easy to convert NSString to NSData, and write NSData.

If you get NSStreamEventHasBytesAvailable event, means you can try to read data from the input stream to some buffer (like NSMutableData). There's no builtin BufferedReader with a readLine method, so you will have to buffer the data yourself and detect when a new line character appears there. After that you can cut a part of the buffer until the new line, and convert NSData to NSString (or a JSON object by using NSJSONSerialization).

Note: scheduleInRunLoop calls might look confusing, but they are required to start receiving events via the delegate. It kind of tells the system on which thread you want to receive them.

P.S. I agree with commenters that if you have control over the server code, it's better to use a standard protocol like Socket IO or msgpack instead of inventing your own, because they have better and nicer libraries and wider community support.

battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • This is what I was expecting by answer, thank you so much. Finally I got it work with CocoaAsyncSocket and your excellent explanation. – Fertos May 05 '18 at 21:50