I am trying to build a WebSockets client that receives an image sent from a mobile app that is built using (android studio) using the simple example provided by this documentation. but when I replace the IP address and port number with the ones provided by the mobile app I get the following error:
C:\Users\LENOVO\Anaconda3\python.exe "D:/SBME/4th year/1at term/Electronics/Task3/Wclient.py"
Traceback (most recent call last):
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\http.py", line 139, in read_response
status_line = await read_line(stream)
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\http.py", line 219, in read_line
raise EOFError("line without CRLF")
EOFError: line without CRLF
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 101, in read_http_response
status_code, reason, headers = await read_response(self.reader)
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\http.py", line 141, in read_response
raise EOFError("connection closed while reading HTTP status line") from exc
EOFError: connection closed while reading HTTP status line
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:/SBME/4th year/1at term/Electronics/Task3/Wclient.py", line 250, in <module>
asyncio.get_event_loop().run_until_complete(rcv_img())
File "C:\Users\LENOVO\Anaconda3\lib\asyncio\base_events.py", line 573, in run_until_complete
return future.result()
File "D:/SBME/4th year/1at term/Electronics/Task3/Wclient.py", line 10, in rcv_img
async with websockets.connect(uri) as websocket:
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 517, in __aenter__
return await self
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 547, in __await_impl__
extra_headers=protocol.extra_headers,
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 290, in handshake
status_code, response_headers = await self.read_http_response()
File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 103, in read_http_response
raise InvalidMessage("did not receive a valid HTTP response") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP response
I searched what is causing this error but unfortunately, I can not find the answer on the internet nor the documentation.
here is the simple python code
import asyncio
import websockets
async def rcv_img():
uri = "ws://192.168.1.8:8080"
async with websockets.connect(uri) as websocket:
img_rcv = await websocket.recv()
# some image processing code here
asyncio.get_event_loop().run_until_complete(rcv_img())
and the sending part in the mobile app
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
//@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory(), "Frame.jpg");
WriteByteArray.toFile(imagebyte,file);
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0,bytes.length);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(bytes);
oos.flush();
socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
sentMsg,
Toast.LENGTH_LONG).show();
}});
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}