Home > Net >  Chat Server with Python Socket Server and Android Client
Chat Server with Python Socket Server and Android Client

Time:12-12

I am trying to create a simple Chat Server that runs on Python.I am having difficulties in getting a connection between my Android App and my Python Socket Server. I am not getting any response so I dont really know what the problem is. The output is:

Socket created Socket Bind Success! Socket is now listening

After that there is nothing else coming.

Here is the Python Server:

import socket
import sys

HOST = "1.2.3.4"
PORT = 1234

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('socket created')

# Bind socket to Host and Port
try:
    s.bind((HOST, PORT))
except socket.error as err:
    print("Bind Failed, Error Code: ")   str(err[0])   ', Message: '   err[1]
    sys.exit()

print("Socket Bind Success!")

# listen(): This method sets up and start TCP listener.
s.listen(10)
print("Socket is now listening")

while 1:
    conn, addr = s.accept()
    print
    'Connect with '   addr[0]   ':'   str(addr[1])
    buf = conn.recv(64)
    print
    buf
s.close()

and here are two classes from my Android App (My client):

MainActivity.java:

package com.example.client;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText inputMessage = findViewById(R.id.inputMessage);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SendMessage().execute(inputMessage.getText().toString());
                inputMessage.getText().clear();
            }
        });



    }
}

SendMessage.java:

package com.example.client;

import android.os.AsyncTask;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class SendMessage extends AsyncTask<String, Void, Void> {
    private Exception exception;
    @Override
    protected Void doInBackground(String... params) {
        try {
            try {
                Socket socket = new Socket("1.2.3.4",1234);
                PrintWriter outToServer = new PrintWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream()));
                outToServer.print(params[0]);
                outToServer.flush();


            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        catch (Exception e) {
            this.exception = e;
            return null;
        }
        return null;
    }
}

The idea and the layout of the app is, that I have a field where I can write text in and send it by hitting the send button.

I tried paying around with the host and the port but I still got no response. I checked if the networks are able to communicate with eachother. The notebook where the server runs is connected via LAN and the Smartphone which runs the app is connected to a Wifi network. Both of them seem to be able to communicate with eachother.

CodePudding user response:

I am not the brightest and forgot to add:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

in my AndroidManifest.xml

Now it works :)

  • Related