I'm trying to work on multiple AVD in Android and sending data between them using Sockets.
Serverside code-snippet:
ServerSocket ss = new ServerSocket(10000);
Log.v("ReceiverTask", "Receiver waiting for requests");
connectedSocket = ss.accept();
ObjectInputStream ois = new ObjectInputStream(connectedSocket.getInputStream());
Object obj = ois.readObject();
ois.close();
ss.close();
Client side code:
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),njr.sendTo());
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(njr);
Log.d("Client","Object send successfull");
oos.flush();
bos.flush();
oos.reset();
oos.close();
bos.close();
socket.close();
The problem is that The objects that are sent from one AVD (as given by log) is not being received at other AVD. This happens sometimes and not always at a same point. Any hints as to what could be the problem???
use oos.flush() at the end in client.
socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),njr.sendTo());
bos = new BufferedOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(bos);
oos.writeObject(njr);
oos.flush();
oos.close();
Log.d("Client","Object send successfull");
Close the stream after reading, else it will assume that its still open
oos.Close();
Related
im trying to implement a basic chat applcation via sockets but i have problem with ObjectInputStream. Im using both write-read methods in while(true) loop and compiling process got stuck when there is nothing to read and waits for it infinitly. Thus, i need to check wheter is it empty or not before read it.
Here is my codes.
Server:
Socket socket = server.accept();
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject("Server connected!");
oos.flush();
String message = (String) ois.readObject();
System.out.println(ois.available());
txtChat.setText(message);
while(true) {
if(sendMessage) {
oos.writeObject(txtMessage.getText());
oos.flush();
sendMessage = false;
}
while(!sendMessage) {
message = (String) ois.readObject();
txtChat.setText(txtChat.getText()+"\n"+"Client: "+message);
}
Thread.sleep(100);
}
Client:
socket = new Socket(host.getHostName(), 9876);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
oos.flush();
String message = (String) ois.readObject();
txtChat.setText(message);
while(true) {
message = (String) ois.readObject();
txtChat.setText(txtChat.getText()+"\n"+"Server: "+message);
if(sendMessage) {
oos.writeObject(txtMessage.getText());
oos.flush();
sendMessage = false;
}
Thread.sleep(100);
}
The stream is either closed or open.
The input stream isn't aware that it is closed until you read something out of it. This is precisely why there exists no such isClosed() method on the stream.
You can read the bytes from the stream, into an object. At best you'll be able to construct the object. Else, you should handle the IO exception and decide accordingly.
Optionally, you can check how many bytes you can read without blocking. If it's 0, you're looking at an empty stream that is waiting for input.
inputStream.available() != 0 //handle exception
In my server i'm trying to split an image and send it to the clients (here are called resources). The problem is that I get StreamCorruptedException when I read the object.
Server side:
Socket resSocket = resourceQueue.get(k).getSocket();
DataOutputStream dos = new DataOutputStream(resSocket.getOutputStream());
FileInputStream fis = new FileInputStream(chunksList.get(i));
byte[] data = new byte[fis.available()];
fis.read(data);
ObjectOutputStream oos = new ObjectOutputStream(resSocket.getOutputStream());
oos.writeObject(data);
oos.close();
Client side
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // Here i get the error
byte[] buffer = (byte[]) ois.readObject();
String path = "c:/JGCF/temp."+"jpg";
System.out.println(path);
FileOutputStream fos = new FileOutputStream(path);
fos.write(buffer);
I have this client code
dOut = new DataOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
dOut.writeByte(2); <--when readByte on server gives -84
oos.writeObject(rectangle);
if slightly changed
dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(2); <--when readByte on server gives 2
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(rectangle);
why is this happening? case is similar in inputstream as well.
Yes. The data will get hopelessly mixed up.
There's no need for this. ObjectOutputStream already has all the methods of DataOutputStream. You don't need them both.
Am getting set socket option failed exception in Android. What am I doing wrong ?
setsockopt failed: EBADF (Bad file number)
For the following code:
int timeout = 500;
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
Integer.parseInt(m.destPort));
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(m);
oos.close();
socket.setSoTimeout(timeOut);
ObjectInputStream iis = new ObjectInputStream(socket.getInputStream());
iis.readObject();
iis.close();
socket.close();
Closing the input or output stream of a socket closes the other stream and the socket.
Change
oos.close();
to
oos.flush();
(Poor coding in Android. It should throw a SocketException: socket closed.)
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();