The Server receives String requests from the Client and replies with a String message to the Client.
I think the Server has to send the reply differently from how it is right now, so I think this line should be altered: c.sendall('Server: '+str.encode(reply))
Python Server:
# This Python file uses the following encoding: utf-8
import socket
def setupServer(port):
serv=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("socket ist da")
try:
# https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
host=socket.gethostbyname(socket.gethostname())
print('Host ip: '+host)
serv.bind((host, port))
except socket.error as msg:
print(msg)
print("socket bind fertig")
return serv
def setupConnection(s):
# Erlaubt 1 Verbindung gleichzeitig
s.listen(1)
connection, address=s.accept()
print("Verbunden zu: "+address[0]+":"+str(address[1]))
return connection
def gET():
reply='GET ausgefuehrt'
return reply
def rEPEAT(te):
reply=te[1]
return reply
def dataTransfer(c,s):
# sendet und erhält Daten bis es stoppen soll
while True:
# Daten erhalten
data=c.recv(1024)#daten erhalten 1024 Buffer size
data=data.decode('utf-8')#nötig?
# Teilt die Datei in Befehl/Command und data auf
dataMessage=data.split(' ',1)
command=dataMessage[0]
if command=='GET'or command=='get':
reply=str(gET())
elif command=='REPEAT'or command=='repeat':
reply=str(rEPEAT(dataMessage))
elif command=='EXIT'or command=='exit':
print("Er ist geganngen")
break
elif command=='KILL'or command=='kill':
print("Server wird geschlossen!")
s.close()
break
else:
print("was?")
reply="Nicht Vorhandener Befehl"
# NAchricht senden
c.sendall('Server: '+str.encode(reply))
print(reply)
print('Klint: '+data)
print("Daten wurden geschickt")
c.close()
def main():
#print("hello")
#host='192.168.1.120'
#host='192.168.2.110'
#print('Host ip: '+str(host))
port=8999
print('Port: '+str(port))
s=setupServer(port)
while True:
try:
conn=setupConnection(s)
dataTransfer(conn,s)
except:
break
if __name__=="__main__":
main()
Java Client Thread:
public class SentThread extends Thread {
Socket socket;
Context cmain;
//boolean stop=false;
SentThread(Socket s,Context c) {
socket = s;
cmain=c;
}
@Override
public void run() {
Log.i("Roman", "run->");
socket=new Socket();
try{
socket.connect(new InetSocketAddress("192.168.2.110", 8999),5000);
}catch (Exception e) {
Log.e("Roman", e.toString());
Toast.makeText(cmain, e.toString(), Toast.LENGTH_SHORT).show();
}
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
out.print("Try");
out.flush();
System.out.println("Message sent");
System.out.println("Trying to read...");
String in = stdIn.readLine();
System.out.println(in);
}
} catch (Exception e) {
// TODO: handle exception
Log.e("Roman", e.toString());
}
}
}
The program gets stuck at String in = stdIn.readLine();
I can't figure out a way in which the java application is able to receive the Message from the server, even though the Java program is able to send messages to the server.
Thank you in advance