I'm trying to make an app that is being monitored by my Python server; the server can send requests to the app, and then the app would send a message back to the server saying if the request was accepted or denied. I do have to use a Python server and I would like my app to be in Kotlin. For example, the app will inform the server that bluetooth is on, and the server would send a request to turn it off, then the app would send a message back telling the server if the user said yes or no to the request.
I have code for server and client using TCP sockets that I made last year. In this case, the client would be the Android app.
Here is a very small code sample to show where I'm going:
#SERVER
import socket
HOST = '0.0.0.0'
PORT = 10000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print("main socket is listening...")
connection, address = s.accept()
with connection:
print('Connected by', address,'\n')
connection.sendall(b'Thank you for connecting!')
#CLIENT
import socket
HOST = '' # The server's hostname or IP address
PORT = 1000 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
# print thank you message
print(s.recv(1024).decode(encoding='utf-8'))
With that being said, would a be able to work with my Python TCP server, or should I go another direction that uses a Python back-end?
Related
I'm trying to connect to server socket in java using my router's public ip,
first, I tried by simply configuring server socket to localhost, like this,
server = new ServerSocket(5000);
It is working on localhost but not working on trying internal ip 192.168.1.6
then, I tried configuring server socket to the internal Ip (saw this solution), code is as follows,
int backlog = 5;
server = new ServerSocket(5000, backlog, InetAddress.getByName("192.168.1.6"));
and it is working as my devices are connected to same network, I can connect to this Ip 192.168.1.6 from a device with Ip 192.168.1.5 on the same network but when I use public ip of my router from client side, connection is getting timed out, I've done port forwarding,
What am I doing wrong here? any help is appreciated, thanks in advance.
EDIT :
I came to about NAT-loopback (saw this solution) as I was trying to connect using public ip while being on same network so I tried different network but still it is not connecting, connection is getting timed out.
Firstly replace your port number by 8080, which is the default port for HTTP when you are not root. Then you have to open the port 8080 on your router to allow the client to connect.
Your server must be connected to the Internet and have a public IP address. Then the client can connect to this public IP address.
I am testing on java.net.ServerSocket.
What I want is the following.
When connecting to aaa.com, you get aaa.com,
Getting bbb.com when connecting to bbb.com.
My etc/hosts file configuration is as follows.
127.0.0.1 aaa.com
127.0.0.1 bbb.com
I used the following java source.
ServerSocket server = new ServerSocket(port);
Socket request = server.accept();
request.getInetAddress().getHostName();
And when connecting to aaa.com, aaa.com is returned.
When connecting to bbb.com, aaa.com is returned.
How can I get bbb.com when connected to bbb.com?
This code is not connecting to anything. It is accepting connections from ... something.
So ... I presume that you have some client code (not shown) that is connecting to port port using hostnames "aaa.com" and "bbb.com" respectively. And you want this server side to know which hostname that the client side used.
It is not possible.
The client resolves the hostnames to an IP address and then makes the connection using the IP address (and only the IP address). Since the IP address is the same in both cases, the server side cannot distinguish the two cases.
It follows that if the application level of the server needs to know the hostname that the client used to make the connection, then the application protocol must pass this information from the client to the server. (That is what protocols like HTTP, FTP and so on do.)
I am working now on simple program to send file through TCP using Java. I have a problem that I am not able to connect between computers ( I am testing application using router and local IP adresses).
I start connection by:
sendSocket = new Socket(sendIp, port);
and I am trying to recieve connection on next PC by:
servsock = new ServerSocket(port);
recieveSocket = servsock.accept();
where
port is 12222,
sendIp is 169.254.5.47 ( second computer that recieves)
and myIP is 192.168.0.52 ( computer that sends)
What I am doing wrong?
I always use the same port, and I see on TCPView that java app uses that port.
Maybe I assign wrong IP, or my firewall blocks somehow.
Best regards and thanks,
Chris
If this is a Windows network, the IP 169.254.5.47 means the host did not obtain a valid IP address from the DHCP. In a standard local network both addresses should be on the same class C range (192.168.0.*).
You have to first solve this physical issue with your network and test it using ping (each host should be able to ping the other and see replies).
I just have a jsp file, print
request.getRemoteIp();
request.getRemotePort();
And I can get the real client ip, but the port is always wrong.
The Server Environment is IBM Websphere and IBM HTTP Server (IHS60)
From the iptrace, I get the packet data..
From the Client to Server, the port is 13944 to 80 (http port is 80). So the client port is 13944.
Then via HTTP, in the jsp, I invoke an API on another server, the port is 48186 to 9082.
Actually,
request.getRemoteIp(); I really get the client ip.
but
request.getRemotePort(); I get the port number is 48185, it seems be IHS port.
How can I get my real client port, please?
Thanks very much.
I have a very simple client server code written java(server listens on some port and client connects to server port and after connection is established, client ip is displayed on server console). This program is working very well in intranet, but if client and server are on internet, my server cannot detect it.I have no firewall installed on my client and server and port forwarding is done on server(I can see it from canyouseeme.org).
Server is directly connected to modem along with other three computers(they are also connected to modem directly)
Please help me figure out why I am not able to detect client on internet.Thanks in advance.
Client code:
------------
String remoteIP = //remote ip
int port =1888;
try{
new Socket(remoteIp,port);
}catch(Exception e){
System.out.println(e.message());
}
Server code
-----------
ServerSocket serversocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
displayIp(socket);
Check your proxy configurations on the client side, see http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
Which protocol do you use?