Simple network chat program kotlin

This is a Kotlin language completed, based on the simple chat program socket

A kotlin use of socket:

Create socket:

Passive monitoring: variable host is the port number, client is a Socket

val server = ServerSocket(host)
val client = server.accept()

Initiative to send: variable mHost is the IP address, mPort is the port number

val socket = Socket(mHost, mPort)

java kotlin and to manipulate data streams transmitted and received through socket

Obtain an output stream, and transmits the stream data is written: (sc.nextLine () encodeToByteArray () is to obtain input lines and converted into bytes.)

getOutputStream().write(sc.nextLine().encodeToByteArray())

Obtaining input stream, to write data Byte array: (bts is Byte array)

getInputStream().read(bts)

Two programs work and code:

main.kt:

Start typing the recipient mode after entering 0 

Message may be received in the reply message and recipient mode

1 are input into the transmission mode

The sender can send a message to the specified IP address mode

After entering 1 will be asked to enter the destination IP address

Classes in java.util * Import. 

Val SC = Scanner (System.`in`)
Fun main () {
the println (
"Please select mode: \ n-" +
"0 mode receiver (can receive and reply message) \ n-" +
" sender mode 1 (can send messages and receive replies "
)
When (sc.nextInt ()) {
0 -> {
sc.nextLine ()
the println (" mode has entered the recipient ")
The Listener (4096)
}
1 -> {
println ( "enter a destination the IP")
sc.nextLine ()
Speaker (sc.nextLine ())
}
}
}

The sender mode Speaker.kt

Repeatedly to create a target socket, write keyboard input, read and print the reply cycle work

Once the method (string) is transmitted and printed reply string time

init process is repeated many times to send a message to call once

import java.net.Socket

class Speaker(host: String = "127.0.0.1", port: Int = 4096) {
private val mHost = host
private val mPort = port
private val mBytes = ByteArray(1024)
private fun once(string: String = "你好") {
try {
Socket(mHost, mPort).apply {
getOutputStream().write(string.encodeToByteArray())
getInputStream().read(mBytes)
println(String(mBytes))
close()
}
} catch (e: Exception) {
println(e)
throw e
}
}

init {
println("已进入发送者模式")
try {
while (true) {
once(sc.nextLine())
}
} catch (e: Exception) {
}
}
}

Receiver mode Listener.kt

Create a socket repeated listening, reading the input stream and prints, then the keyboard input to the output stream and send

Once the method (string) is transmitted and printed reply string time

init process is repeated many times to send a message to call once

the java.net.ServerSocket Import 

class The Listener (Host: Int) {
Private Val = the ServerSocket Server (Host)
Private BTS Val = the ByteArray (20480)

the init {
the Thread {
the while (to true) {
. server.accept () {Apply
the getInputStream () .read (BTS)
the println (String (BTS))
the getOutputStream () Write (sc.nextLine () encodeToByteArray ().).
}
}
} .start ()
}
}

procedure results show:
(green keyboard input)
sender:

j

Guess you like

Origin www.cnblogs.com/Miliapus/p/12024809.html