New Project > SocketServer - Mainclass.kt
fun main(){
// 서버 역할을 하기 위해 객체 생성
val server = ServerSocket(55555)
print("사용자 접속대기")
val socket = server.accept()
println(socket)
// 서버 종료
socket.close()
}
<uses-permission android:name="android.permission.INTERNET"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="서버 접속하기" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
cmd > ipconfig
class MainActivity : AppCompatActivity() {
private var _binding :ActivityMainBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
binding.button.setOnClickListener {
thread {
// 서버 접속
val socket = Socket("172.30.1.254",55555)
Log.d("test","$socket")
socket.close()
}
}
}
}