generated from justuser-31/code_projects_template
Выгрузка всех файлов
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package org.client
|
||||
|
||||
import com.beust.klaxon.Klaxon
|
||||
import org.server.Person
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintWriter
|
||||
import java.net.Socket
|
||||
|
||||
data class Person(
|
||||
val name: String,
|
||||
val age: Int,
|
||||
val city: String
|
||||
)
|
||||
|
||||
const val serverPort: Int = 12345
|
||||
const val rawJson = """{
|
||||
"name": "Иван Иванов",
|
||||
"age": 25,
|
||||
"city": "Москва"
|
||||
}"""
|
||||
val json = Klaxon().parse<Person>(rawJson)
|
||||
|
||||
fun main() {
|
||||
val socket = Socket("localhost", serverPort)
|
||||
val writer = PrintWriter(socket.getOutputStream(), true)
|
||||
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
|
||||
|
||||
writer.println(Klaxon().toJsonString(json))
|
||||
val response = reader.readLine()
|
||||
println("Received from server: $response")
|
||||
|
||||
socket.close()
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.server
|
||||
|
||||
import com.beust.klaxon.Klaxon
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintWriter
|
||||
import java.net.ServerSocket
|
||||
|
||||
data class Person(
|
||||
val name: String,
|
||||
val age: Int,
|
||||
val city: String
|
||||
)
|
||||
|
||||
const val serverPort: Int = 12345
|
||||
|
||||
fun main() {
|
||||
val server = ServerSocket(serverPort)
|
||||
println("Server started on port $serverPort...")
|
||||
|
||||
while (true) {
|
||||
val client = server.accept()
|
||||
println("Client connected: ${client.inetAddress.hostAddress}:${client.port}")
|
||||
|
||||
val reader = BufferedReader(InputStreamReader(client.getInputStream()))
|
||||
val writer = PrintWriter(client.getOutputStream(), true)
|
||||
|
||||
val rawReceived = reader.readLine()
|
||||
//println("Received from client: $received")
|
||||
val json = Klaxon().parse<Person>(rawReceived)
|
||||
println("""
|
||||
|
||||
Parsed data:
|
||||
name: ${json?.name}
|
||||
age: ${json?.age}
|
||||
city: ${json?.city}
|
||||
|
||||
""".trimIndent())
|
||||
|
||||
val response = "OK"
|
||||
writer.println(response)
|
||||
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
plugins {
|
||||
kotlin("jvm") version "2.1.10"
|
||||
application
|
||||
}
|
||||
|
||||
group = "org.example"
|
||||
version = "1.0-SNAPSHOT"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation(kotlin("test"))
|
||||
implementation("com.beust:klaxon:5.5")
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
}
|
||||
|
||||
// "run" task
|
||||
application {
|
||||
mainClass.set("org.example.MainKt") // Replace with your actual main class
|
||||
}
|
||||
|
||||
tasks.register<JavaExec>("runWithInput") {
|
||||
group = "application"
|
||||
mainClass.set("org.server.MainKt")
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
standardInput = System.`in` // 👈 enables readln() or readLine()
|
||||
}
|
||||
tasks.register<JavaExec>("runWithInputClient") {
|
||||
group = "application"
|
||||
mainClass.set("org.client.ClientKt")
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
standardInput = System.`in` // 👈 enables readln() or readLine()
|
||||
}
|
||||
Reference in New Issue
Block a user