I set up a client and server sockets. When I use classes ObjectOutputStream and ObjectInputStream and the method readObject/writeObject everything works fine.
It simulates communication with a robot that I know for sure interprets correctly only method
DataOutputStream.writeBytes.
So I set up the new architecture for simulation since the robot is not available for testing on a daily basis.
In the following code where ObjectOutputStream/ObjectInputStream readObject/writeObject were replaced with DataInputStream/DataOutputStream writeBytes and IOutils.toByteArray.
The server socket correctly receives the message but when it tries to write back a response I get a broken pipe as if the connection was closed somewhere.
Notice that I never close sockets or streams because the robot can answer even after 30 seconds.
Any help to make DataOutputStream.writeBytes work would be appreciated.
Here's the non-working code:
Client:
Socket serverSocket = new Socket("server", 9899);
DataOutputStream outputStream = new DataOutputStream(serverSocket.getOutputStream());
//ObjectOutputStream outputStream = new ObjectOutputStream(serverSocket.getOutputStream());
//outputStream.writeObject("\n" + "message" + "\r");
outputStream.writeBytes("\n" + "message" + "\r");
outputStream.flush();
DataInputStream inputStream = new DataInputStream(serverSocket.getInputStream());
//ObjectInputStream inputStream = new ObjectInputStream(serverSocket.getInputStream());
byte [] bytes = IOUtils.toByteArray(inputStream);
String serverResponse = new String(bytes,"UTF-8");
// String serverResponse = (String)inputStream.readObject();
Server:
ServerSocket serverSocket = new ServerSocket(9899);
while (true) {
Socket socket = serverSocket.accept();
//ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
byte [] bytes = IOUtils.toByteArray(inputStream);
String message = new String(bytes,"UTF-8");
//String message = (String) inputStream.readObject();
Thread.sleep(15000);
//ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
//outputStream.writeObject("server response");
outputStream.writeBytes("server response"); //EXCEPTION THROWN HERE FOR BROKEN PIPE
outputStream.flush();
}
Thanks for your time
IOUtils.toString(InputStream) must read the stream until its end, which would imply that the peer has disconnected. So you can't write to it.
If you're exchanging Strings with data streams you should use writeUTF() and readUTF().
Or read and write lines, with a BufferedReader/Writer.
Related
I am unable to send data from the server(python) to the client (android java). Java client socket gets shutdown before it accepts data from the server.
no Error on the Python side it sends data as well but the java client socket is closed and doesn't accept data
Error W/System.err: java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:923) W/System.err: at
com.example.smd.TcpDataHandler.doInBackground(TcpDataHandler.java:66)
at com.example.smd.TcpDataHandler.doInBackground(TcpDataHandler.java:20)
python code
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('ip' , port))
server_socket.listen(10)
print("server is listening")
(client_socket,client_adress) = server_socket.accept()
print("Socket completed")
#client_socket.shutdown(socket.SHUT_WR)
#while True:
print('waitingg.....................')
try:
data2=b''
while True:
buf = client_socket.recv(65535)
data2 += buf
if not buf:
break
client_input = data2
print("data recieve ")
b=base64.b64decode(client_input)
#print(b)
imageStream = io.BytesIO(client_input)
imageFile = Image.open(imageStream)
# i = Image.fromarray(imageFile, mode='L').convert('1')
imageFile.LOAD_TUNCATED_IMAGES=True
print(imageFile.size)
#this is the image ,here you cna verify via image size
modelRes=test(imageFile)
modelRes=modelRes[0]
modelRes= int(modelRes[0])
data = database(decode(imageFile))
print(data)
#json_obj = [{ "model_res": modelRes , "decode_data": data}]
x = { "model_res": modelRes, "decode_res": data}
# convert into JSON:
data = json.dumps(x)
print('data is : '+data)
client_socket.send(data.encode())
print('data sended')
client_socket.shutdown(socket.SHUT_WR)
print('Reciving BYE message')
data3 = client_socket.recv(1024).decode()
print('Program ended : '+data3)
client_socket.close()
server_socket.close()
except Exception as e:
print(e)
JAVA codee
socket=new Socket("ip", port);
System.out.println("Socket donee : ");
byte[] bArray = new byte[(int) file.length()];
try{
fis = new FileInputStream(file);
fis.read(bArray);
fis.close();
}catch(IOException ioExp){
ioExp.printStackTrace();
}
Log.d("Data to bytes"," images has been converted to byte");
out = new DataOutputStream(socket.getOutputStream());
out.write(bArray);
out.flush();
out.close();
Log.d("Data send"," Data has been send Successfully");
Log.d("waiting responce"," Waiting ford data from Python");
InputStreamReader streamReader= new
InputStreamReader(socket.getInputStream());
BufferedReader reader= new BufferedReader(streamReader);
String value= reader.readLine();
System.out.println(value);
Log.d("Data Revieve"," Recieve data Successfully");
reader.close();
dataRec="";
dataRec=value;
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
System.out.println("Sending BYE MSG");
// write the message we want to send
dataOutputStream.writeUTF("Byeee serverrrrrr");
dataOutputStream.flush(); // send the message
dataOutputStream.close(); // close the output stream when we're done.
socket.close();
Log.d("Socket closed "," Socket has been Successfully");
In your java code you have the lines
out = new DataOutputStream(socket.getOutputStream());
out.write(bArray);
out.flush();
out.close();
That last line out.close(); also closes the socket. This may not be obvious. DataOutputStream extends FilterOutputStream and inherits its close method. FilterOutputStream.close() also closes the wrapped OutputStream which in this case is socket.getOutputStream(). The Javadocs for socket.getOutputStream() state
Closing the returned OutputStream will close the associated socket.
Since your python code reads until end-of-stream/end-of-file, you need some way for Java signal to signal to its python peer that it is finished writing data. Java provides this through the socket.shutdownOutput() method. Thus, if you replace out.close(); with socket.shutdownOutput() you should get the effect you need:
out = new DataOutputStream(socket.getOutputStream());
out.write(bArray);
out.flush();
socket.shutdownOutput();
I'm trying to make a video file transfer but am having problems getting the server to start sending bytes.
The first step is for the client to connect, the socket gets accepted. Then the client sends the video file name but the server never reads this.
This is the code for the server up until it blocks:
try(ServerSocket serverSocket = new ServerSocket(4005))
{
Socket socket = serverSocket.accept();
System.out.println("accepted");
OutputStream os = socket.getOutputStream();
BufferedReader receiveReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("This gets printed");
String request = receiveReader.readLine();//never passes this line
System.out.println("This doesn't get printed");
and this is the client up until it blocks waiting for the server to send the video bytes:
try(Socket socket = new Socket(IPAddress, 4005))
{
byte[] messageBytes = new byte[10000];
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes("REQUEST;"+videoPath);//This is the line that should send the bytes for the server to read, so it won't block.
String home = System.getProperty("user.home");
String path = home+"\\Downloads" + videoName;
path = path.trim();
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream is = socket.getInputStream();
int bytesRead = 0;
System.out.println("Downloading file...");
while((bytesRead = is.read(messageBytes))!=-1)//This blocks here
Why on earth isn't the server reading the "Request" + videoPath bytes that the server is sending? I tried outputStream.flush() as well, no luck.
Usual problem. You're reading a line but you aren't writing a line. Add a line terminator to the sent message.
When you fix this you will then discover that you can't mix buffered streams and readers on the same socket. I suggest you do all the I/O via the DataInput/OutputStream classes, using read/writeUTF() for the name.
If you're sending multiple files see my answer there.
I have a project where I will receive data from a TCP port, I need to be constantly waiting to check if I receive data.
My code for now is:
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip,8000));
while(true){
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
InputStream inFromClient = socket.getInputStream();
while(true){
int i = inFromClient.read();
System.out.println("data = " + i);
l++;
}
I don't like this strategy based in while(true), There is some strategy to receive data from a callback or something similar?
You can use java File I/O (Featuring NIO.2). Please follow the link for sample
https://github.com/dublintech/async_nio2_java7_examples/blob/master/echo-nio2-server/src/main/java/com/alex/asyncexamples/server/AsyncEchoServer.java
I am using Sockets to connect using TCP and I want to make different calls. e.g. Get InputValue
I have these type of different requests which I want to make from already running server.
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
String func="Get inputvalue";
byte[] tRequest = encoder.string2bytes(func);
out.write(tRequest);
out.flush();
System.out.println("write done");
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
It says it connected as just connected printout got printed. Write done print is also printed but no data is returned and the program keeps on running.
If I use telnet then this same request call returns data successfully.
So the question is how to make TCP calls in java?
Update: I solved this by:
PrintWriter toServer =
new PrintWriter(client.getOutputStream(),true);
BufferedReader fromServer =
new BufferedReader(
new InputStreamReader(client.getInputStream()));
toServer.println("Get inputvalue\r\n");
String line = "";
System.out.println("Client received: ");
while ((line = fromServer.readLine()) != null) {
System.out.println(line);
}
but the program keeps on running in the while loop and prints nothing. how to check that response is ended?
I have implemented a client/server to send files . When there is no more messages exchanged after sending the file , the code works perfectly , but if the client sends some string to the server directly after the code of receiving the file both client and server stop doing anything and the file is not sent it's something like if they both get stuck in deadlock but I'm not really sure :
Here is the code to send the file without sending anything after it , which works:
Client
String filename;
BufferedReader UIn = new BufferedReader(new InputStreamReader(System.in));
Socket peer = new Socket("localhost",9999);
System.out.print("Enter the file name to download :");
filename= UIn.readLine();
///////////////////////////////
DataOutputStream OutToServer;
OutToServer = new DataOutputStream(peer.getOutputStream());
OutToServer.writeBytes(filename+"\n");
FileOutputStream fos = new FileOutputStream(new File("D:/new.txt"));
BufferedOutputStream out = new BufferedOutputStream(fos);
InputStream in = peer.getInputStream();
buffer = new byte[1024];
while((count=in.read(buffer))>0)
{
fos.write(buffer, 0, count);
System.out.println(buffer);
}
fos.close();
System.out.println("done");
Server:
ServerSocket server =null;
try {
server = new ServerSocket(9999);
while(true)
{
client= server.accept();
//System.out.println("Connection accepted");
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
DataOutputStream outToclient =new DataOutputStream(client.getOutputStream());
String request=inFromClient.readLine();
file = new File(request);
if (file.exists())
{
OutputStream out = client.getOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
while((count =in.read(buffer)) >0)
{
out.write(buffer,0,count);
out.flush();
}
}
// System.out.println(request);
// outToclient.writeBytes("alaa\n");
}
} catch (IOException ex) {
Logger.getLogger(ServerWork.class.getName()).log(Level.SEVERE, null, ex);
}
But if I try to send anything after the loop between client and server it stops working . Is it because I'm using readLine() and writeBytes()?
You are using both DataOutputStream and OutputStream. I think that they
should work together but what I guess you should do is to flush the buffer (or close it).
After you're done writing everything you want to send, some data may still be in the buffer. Now you will have to flush the buffer, to force it to send all the data.(as it is said here).
outToclient.writeBytes("alaa\n");
outToclient.flush();