I am having this python server,
import socket
def server_program():
# get the hostname
host = 'localhost' # socket.gethostname()
port = 8091 # initiate port no above 1024
server_socket = socket.socket() # get instance
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True:
# receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode()
if not data:
# if data is not received break
break
print("from connected user: " + str(data))
data = input(' -> ')
conn.send(data.encode()) # send data to the client
conn.close() # close the connection
if __name__ == '__main__':
server_program()
that works with this python client,
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8091))
clientsocket.send('hello'.encode())
but does not with this ktor client,
fun send() {
runBlocking {
val selectorManager = SelectorManager(Dispatchers.IO)
val socket = aSocket(selectorManager).tcp().connect("10.0.2.2", 8091)
val receiveChannel = socket.openReadChannel()
val sendChannel = socket.openWriteChannel(autoFlush = true)
val myMessage = "this" // readln()
sendChannel.writeStringUtf8("$myMessage\n") // ("$args\n")
}
}
The connection is established but there's no print of the string.
It might be very basic but the ktor documentation says little.
With the python client:
Connection from: ('127.0.0.1', 54012)
from connected user: hello`
With ktor client:
Connection from: ('127.0.0.1', 53334)
Solved it with
import java.net.Socket
suspend fun sendStuff_()
{
val message = "text"
val socket = Socket("10.0.2.2", 8091)
socket.outputStream.write(message.toByteArray())
}