import socket
listensocket = socket.socket()
Port = 4444
maxConnections = 999
IP = socket.gethostname()
listensocket.bind(('', Port))
listensocket.listen(maxConnections)
print('server started at ' + IP + ' on port ' + str(Port))
while True:
(clientsocket, address) = listensocket.accept()
print('New connection!')
message = clientsocket.recv(1024).decode()
if message == 'Вход':
print("Вход")
elif message == 'Выход':
print("Выход")
if message != '':
print(message)package com.smokingdevil.client;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
// declaring required variables
private Socket client;
private PrintWriter printwriter;
// private EditText textField;
private Button button;
private Button button1;
private String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// reference to the text field
// textField = (EditText) findViewById(R.id.editText1);
// reference to the send button
button1 = (Button) findViewById(R.id.button1);
button = (Button) findViewById(R.id.button);
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get the text message on the text field
message = button.getText().toString();
// start the Thread to connect to server
new Thread(new ClientThread(message)).start();
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get the text message on the text field
message = button1.getText().toString();
// start the Thread to connect to server
new Thread(new ClientThread(message)).start();
}
});
}
// the ClientThread class performs
// the networking operations
class ClientThread implements Runnable {
private final String message;
ClientThread(String message) {
this.message = message;
}
@Override
public void run() {
try {
// the IP and port should be correct to have a connection established
// Creates a stream socket and connects it to the specified port number on the named host.
client = new Socket("ВАШ IP", 4444); // connect to server
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(message); // write the message to output stream
printwriter.flush();
printwriter.close();
// closing the connection
client.close();
} catch (IOException e) {
e.printStackTrace();
}
// updating the UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// textField.setText("");
}
});
}
}
}
activity_main.xml в android studio :<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/screenBackground"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="364dp"
android:backgroundTint="#68C50C"
android:text="@string/Вход"
android:textAllCaps="true"
android:textColor="#FF000000"
android:textSize="60sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="366dp"
android:backgroundTint="#FF0000"
android:text="@string/Выход"
android:textAllCaps="true"
android:textColor="@color/black"
android:textSize="60sp"
android:textStyle="bold" />
</LinearLayout>