Im trying to send an object from client to server and one of the object states is a vector and the other is a string. I can access the string on the Server side, but the vector contents is zero on the server side..Can someone help me out please..
// Server
import java.net.*;
import java.util.Vector;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) {
int port = 2002;
try {
System.out.println("Hello");
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
System.out.println("Hello 2");
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to = (testobject)ois.readObject();
System.out.println("Vector size : " + to.vectorX.size() + " and object.id : "
+ to.id);
/* if (to != null) {
for(int i = 0; i < to.vectorX.size(); ++i )
System.out.println("Output 1 : " + to.vectorX.elementAt(i));
} */
is.close();
s.close();
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Client
import java.net.*;
import java.io.* ;
import java.util.Vector;
public class SimpleClient {
protected static Vector<String> vectorX = new Vector<String>();
public SimpleClient(){
vectorX.addElement("hello");
vectorX.add("goodbye");
vectorX.add("finally");
}
public static void main(String args[]) {
try {
new SimpleClient();
Socket s = new Socket("localhost", 2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to = new testobject(1, "theID", vectorX );
System.out.println(vectorX.size());
oos.writeObject(to);
// oos.writeObject(new String("another object from the client"));
oos.close();
os.close();
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
//testobject
import java.net.*;
import java.io. * ;
import java.util.Vector;
class testobject implements Serializable {
int value;
String id;
Vector<String> vectorX;
public testobject(int v, String s, Vector<String> vector) {
this.value = v;
this.id = s;
this.vectorX = new Vector<String>();
}
}
The constructor for your testobject is not using the vector argument. It is rather assigning the vectorX ivar to a new instance, ignoring the parameter provided by the caller:
public testobject(int v, String s, Vector<String> vector) {
this.value = v;
this.id = s;
this.vectorX = new Vector<String>(); // This is bad
}
You should instead use:
public testobject(int v, String s, Vector<String> vector) {
this.value = v;
this.id = s;
this.vectorX = vector;
}
Note: It's not common for class names in Java to be all lowercase, or to start with a lowercase letter. As an aside, I think you should rename your class to TestObject.
Related
i have to do a practice in my uni, it must create a blockchain using sockets and serialization in a "simple way". But when exiting the loop (typing "NO") It creates a EOF exception that i cannot solve, while closing the socket(s.close()). i would appreciate some help, i am not vry good at java. here are my classes.
Client
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client implements Runnable{
public static void main(String[] args) throws Exception {
(new Thread(new Client())).start();
}
public static MedicalReport createReport(){
return new MedicalReport(10,"pepe","id","record");
}
#Override
public void run() {
// int port = 12345;
// String computer = "localhost";
try{
Socket s = new Socket("localhost", 12348);
ObjectOutputStream p = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
/* PrintWriter print = new PrintWriter(s.getOutputStream());
print.println("ready");
print.flush();*/
//manda informe al servidor serializado y espera respuesta
boolean stop = false;
while(!stop){
try{
MedicalReport report = createReport();
p.writeObject(report);
p.flush();
p.reset();
System.out.println("Do you want to continue? Yes or No");
Scanner in1 = new Scanner (System.in);
String answer="";
if(in1.hasNextLine())
answer = in1.nextLine();
if(!answer.equalsIgnoreCase("yes")){
System.out.println(report);
stop = true;
}
}
catch(Exception e){
System.out.println(e);
}
}
try{
s.close();
}
catch(Exception e){
System.out.println(e);
}
}catch(Exception e){
System.out.println(e);
}
}
}
SERVER
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class Server {
public static void main(String[] args)
throws Exception
{
ArrayList<Block> blockChain = new ArrayList<>();
try{
ServerSocket ss = new ServerSocket(12348);
Socket s = ss.accept();
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
/* Scanner scanner = new Scanner(s.getInputStream());
String text = scanner.nextLine();*/
int i = 0;
int previousHash = 0;
while (i != 20){
MedicalReport rp = (MedicalReport)in.readObject();
Block block = new Block(rp,previousHash);
blockChain.add(block);
System.out.println("Block " + blockChain.size() + " added to blockchain");
System.out.println(blockChain.get(i));
previousHash = block.getBlockHash();
System.out.println(blockChain);
i++;
}
try{
ss.close();
}
catch(Exception e){
System.out.println(e);
}
}catch(Exception e){
System.out.println(e);
}
}
}
It looks like the error is while closing the socket, any idea?
EDIT REST OF THE CODE
MEDICAL REPORT
import java.io.Serializable;
public class MedicalReport implements Serializable {
private int age;
private String name;
private String id;
private String record;
private static final long serialVersionUID = 1L;
public MedicalReport(){super();}
public MedicalReport(int age, String name, String id, String record) {
super();
this.age = age;
this.name = name;
this.id = id;
this.record = record;
}
public String getRecord(){
return this.record;
}
public String toString(){
return this.name + ". \n" + this.age + ". \n" + this.id + ". \n" + this.record;
}
}
BLOCK
public class Block {
private int blockHash;
private int previousHash;
private MedicalReport report;
//Block Constructor.
public Block(MedicalReport report,int previousHash ) {
this.previousHash = previousHash;
this.report = report;
this.blockHash = report.hashCode();
}
public int getPreviousHash() {
return previousHash;
}
public MedicalReport getReport() {
return report;
}
public int getBlockHash() {
return blockHash;
}
}
EDIT 2
FIRST QUESTION SOLVED. Now i get this error when exiting the loop:
java.net.SocketException: Connection reset by peer: socket write error
readObject() throws EOFEzception when the peer has closed the connection. This is normal. Catch it and stop reading. There is no problem here to solve.
IMPORTANT: As EJP said EOFException is normal and you can control the flow of your code with it but if you still want to know how to do in the way you asked here it is. REMEMBER THIS IS JUST FULFILL YOUR QUESTION AND NOT ADVISED TO DO SO.
On Server Class
Replace
MedicalReport rp = (MedicalReport)in.readObject();
With
MedicalReport rp;
if((rp = (MedicalReport)in.readObject())==null) break;
On Client Class
ADD
p.writeObject(null);
Just above the s.close(); statement
You must know that when a peer close the connection normally then
read() returns -1,
readLine() returns null,
readXXX() throws EOFException for any other XXX
And A write will throw an IOException
Can someone please resolve this issue.
Using JDK 1.8, I am trying to build a very simple chat application in Java using Sockets. In my client class as soon as following line executes
Message returnMessage = (Message) objectInputStream.readObject();
it throws exception.
Exception in thread "main" java.io.OptionalDataException
I am writing only objects of type Message to the stream and reading objects of type Message, since i wrote once, i dont think i am doing anything wrong in reading them in sequence.
Q. Also please let me know what is the best way to debug this type of application, how to hit the breakpoint in server while running client ?
Client
package com.company;
import sun.misc.SharedSecrets;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException{
Socket socket = new Socket("localhost", Server.PORT);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String readerInput = bufferedReader.readLine();
String[] readerInputTokens = readerInput.split("\u0020");
if(readerInputTokens.length != 2) {
System.out.println("Usage: Client <integer> <integer>");
} else {
Integer firstNumber = Integer.decode(readerInputTokens[0]);
Integer secondNumber = Integer.decode(readerInputTokens[1]);
Message message = new Message(firstNumber, secondNumber);
objectOutputStream.writeObject(message);
System.out.println("Reading Object .... ");
Message returnMessage = (Message) objectInputStream.readObject();
System.out.println(returnMessage.getResult());
socket.close();
}
}
public static boolean isInteger(String value) {
boolean returnValue = true;
try{Integer.parseInt(value);}
catch (Exception ex){ returnValue = false; }
return returnValue;
}
}
Server
package com.company;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public final static int PORT = 4446;
public static void main(String[] args) throws IOException, ClassNotFoundException {
new Server().runServer();
}
public void runServer() throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server up & ready for connections ...");
// This while loop is necessary to make this server able to continuously in listning mode
// So that whenever a client tries to connect, it let it connect.
while (true){
Socket socket = serverSocket.accept(); // Server is ready to accept connectiosn;.
// Initialize Server Thread.
new ServerThread(socket).start();
}
}
}
Sever Thread
package com.company;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class ServerThread extends Thread {
private Socket socket = null;
ServerThread(Socket socket){
this.socket = socket;
}
public void run() {
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeChars("\n");
objectOutputStream.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
Message message = (Message) objectInputStream.readObject();
multiplyNumbers(message);
System.out.println("Writing: "+message.toString());
objectOutputStream.writeObject(message);
System.out.println("Message Written");
socket.close();
} catch( IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
private void multiplyNumbers(Message message) {
message.setResult(message.getFirstNumber().intValue() * message.getSecondNumber().intValue());
}
}
Message Class
package com.company;
import java.io.Serializable;
public class Message implements Serializable {
private static final long serialVersionUID = -72233630512719664L;
Integer firstNumber = null;
Integer secondNumber = null;
Integer result = null;
public Message(Integer firstNumber, Integer secondNumber) {
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
}
public Integer getFirstNumber() {
return this.firstNumber;
}
public Integer getSecondNumber() {
return this.secondNumber;
}
public Integer getResult() {
return this.result;
}
public void setResult(Integer result) {
this.result = result;
}
#Override
public String toString() {
return "Message{" +
"firstNumber=" + firstNumber +
", secondNumber=" + secondNumber +
", result=" + result +
'}';
}
}
objectOutputStream.writeChars("\n");
Why are you writing a newline to an ObjectOutputStream? You're never reading it. Don't do that. Remove this wherever encountered.
I want to create a server that can accept multiple connections and then bind 2 clients as a pair and forward the data between these 2 clients. But it is about multiple pairs of clients. I already have multithread server that can create a new thread for each new connected client. The problem for me is that these threads dont know of each other and somehow I have to connect 2 clients to a connection pair.
For now I just create these pair connection as this: I wait for the first client, then I wait for the second client and then open a thread for the input of client 1 that gets forwarded to client 2 and the other way around. This is not usable for multiple clients.
How can I do this decent?
The way I see it, a client would need to
establish a TCP(?) connection with your server,
identify itself
give the ID of the other client it wishes to talk to
The first that connects would have to be kept on hold (in some global table in your server) until the second client connects.
Once a pair of clients would have been recognized as interlocutors, you would create a pair of threads to forward the data sent by each client to the other one.
UPDATE: Example
ClientSocket.java
package matchmaker;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientSocket implements Closeable {
private final Socket socket;
private final InputStream in;
private final OutputStream out;
private final String ownId;
private final String peerId;
public ClientSocket(Socket socket) throws IOException {
this.socket = socket;
this.in = socket.getInputStream();
this.out = socket.getOutputStream();
DataInputStream din = new DataInputStream(in);
this.ownId = din.readUTF();
this.peerId = din.readUTF();
}
public ClientSocket(String server, int port, String ownId, String peerId)
throws IOException {
this.socket = new Socket(server, port);
this.socket.setTcpNoDelay(true);
this.in = socket.getInputStream();
this.out = socket.getOutputStream();
this.ownId = ownId;
this.peerId = peerId;
DataOutputStream dout = new DataOutputStream(out);
dout.writeUTF(ownId);
dout.writeUTF(peerId);
}
public String getOwnId() {
return ownId;
}
public String getPeerId() {
return peerId;
}
public InputStream getInputStream() {
return in;
}
public OutputStream getOutputStream() {
return out;
}
#Override
public void close() throws IOException {
socket.close();
}
}
Matchmaker.java: the server
package matchmaker;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Matchmaker extends Thread {
private static final Logger LOG
= Logger.getLogger(Matchmaker.class.getName());
private final int port;
private final Map<ClientPair,ClientSocket> waiting = new HashMap<>();
public static void main(String[] args) {
try {
int port = 1234;
int st = 0;
for (String arg: args) {
switch (st) {
case 0:
switch (arg) {
case "-p":
st = 1;
break;
default:
System.out.println("Unknown option: " + arg);
return;
}
break;
case 1:
port = Integer.parseInt(arg);
st = 0;
break;
}
}
Matchmaker server = new Matchmaker(port);
server.start();
server.join();
} catch (InterruptedException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
private Matchmaker(int port) {
this.port = port;
setDaemon(true);
}
#Override
public void run() {
try {
ServerSocket server = new ServerSocket(port);
while (true) {
ClientSocket socket = new ClientSocket(server.accept());
ClientPair pair = new ClientPair(
socket.getOwnId(), socket.getPeerId());
ClientSocket other;
synchronized(this) {
other = waiting.remove(pair.opposite());
if (other == null) {
waiting.put(pair, socket);
}
}
if (other != null) {
LOG.log(Level.INFO, "Establishing connection for {0}",
pair);
establishConnection(socket, other);
} else {
LOG.log(Level.INFO, "Waiting for counterpart {0}", pair);
}
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
private void establishConnection(ClientSocket socket, ClientSocket other)
throws IOException {
Thread thread = new StreamCopier(
socket.getInputStream(), other.getOutputStream());
thread.start();
thread = new StreamCopier(
other.getInputStream(), socket.getOutputStream());
thread.start();
}
}
StreamCopier.java: a thread that reads from an InputStream and writes to an OutputStream
package matchmaker;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StreamCopier extends Thread {
private static final Logger LOG
= Logger.getLogger(StreamCopier.class.getName());
private final InputStream in;
private final OutputStream out;
public StreamCopier(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
setDaemon(true);
}
#Override
public void run() {
LOG.info("Start stream copier");
try {
for (int b = in.read(); b != -1; b = in.read()) {
out.write(b);
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
} finally {
LOG.info("End stream copier");
try {
out.close();
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
}
}
ClientPair.java: a pair of client IDs
package matchmaker;
public class ClientPair {
private final String client1;
private final String client2;
public ClientPair(String client1, String client2) {
this.client1 = client1;
this.client2 = client2;
}
public String getClient1() {
return client1;
}
public String getClient2() {
return client2;
}
public ClientPair opposite() {
return new ClientPair(client2, client1);
}
#Override
public int hashCode() {
int hash = 5;
hash = 73 * hash + client1.hashCode();
hash = 73 * hash + client2.hashCode();
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ClientPair other = (ClientPair) obj;
return client1.equals(other.client1) && client2.equals(other.client2);
}
#Override
public String toString() {
return "[" + client1 + "," + client2 + "]";
}
}
ReaderClient.java: a sample client that reads from the socket and writes to standard output
package matchmaker;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReaderClient {
private static final Logger LOG = Logger.getLogger(ReaderClient.class.getName());
public static void main(String[] args) {
try (ClientSocket client
= new ClientSocket("localhost", 1234, "reader", "writer")) {
Reader reader
= new InputStreamReader(client.getInputStream(), "UTF-8");
BufferedReader in = new BufferedReader(reader);
for (String s = in.readLine(); s != null; s = in.readLine()) {
System.out.println(s);
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
}
WriterClient.java: a sample client that writes to the socket
package matchmaker;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WriterClient {
private static final Logger LOG = Logger.getLogger(ReaderClient.class.getName());
public static void main(String[] args) {
try (ClientSocket client
= new ClientSocket("localhost", 1234, "writer", "reader")) {
Writer writer
= new OutputStreamWriter(client.getOutputStream(), "UTF-8");
PrintWriter out = new PrintWriter(writer);
for (int i = 0; i < 30; ++i) {
out.println("Message line " + i);
}
out.flush();
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
}
I'm trying to send an ArrayList from a server to a client, but it doesn't work. The server does send ints. If the client send the list, it works too.
Here is the object i try to send (only the fields)
public class DrawingPoint implements Serializable
{
private double x;
private double y;
private boolean paint;
Color c;
private int dikte;
boolean gum;
public DrawingPoint(double x, double y, boolean paint, Color c, int dikte,
boolean gum) {
super();
this.x = x;
this.y = y;
this.paint = paint;
this.c = c;
this.dikte = dikte;
this.gum = gum;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public boolean isPaint() {
return paint;
}
public int getDikte() {
return dikte;
}
//getters and setters
}
Here is the code for the server and client
server (this isn't the server but this class recieves and sends stuff. The server makes an array with these object and it let's it send.)
package MultiplayerPaint.socket.server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.ArrayList;
import MultiplayerPaint.DrawingPoint;
public class ThreadClass extends Thread implements Runnable, Serializable{
transient Socket socket;
transient Server server;
private transient ObjectInputStream inputFromClient;
private transient ObjectOutputStream outputToClient;
public ArrayList<DrawingPoint> list = new ArrayList<>();
String name;
public int nummer;
transient private boolean changed = false;
public ThreadClass(Socket socket, Server server, int nummer)
{
this.server = server;
this.nummer = nummer;
try {
inputFromClient = new ObjectInputStream(socket.getInputStream());
outputToClient = new ObjectOutputStream(socket.getOutputStream());
runOnce();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while(true)
{
try {
Thread.sleep(1000/5);
ArrayList<DrawingPoint> l = (ArrayList<DrawingPoint>) inputFromClient.readObject();
list = l;
changed = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
public void runOnce()
{
try {
outputToClient.writeInt(nummer);
outputToClient.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isChanged() {
return changed;
}
public void setChanged(boolean changed) {
this.changed = changed;
}
public void sending(ThreadClass[] sturen) {
try {
for(ThreadClass t : sturen)
{
outputToClient.writeObject(t);
}
outputToClient.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is the client
package MultiplayerPaint.socket;
import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import MultiplayerPaint.DrawingPoint;
import MultiplayerPaint.PaintModel;
import MultiplayerPaint.socket.server.Server;
import MultiplayerPaint.socket.server.ThreadClass;
public class Client
{
private ObjectInputStream inputFromClient;
private ObjectOutputStream outputToClient;
int aantal= -1;
int nummer;
public Client(final PaintModel m)
{
try {
Socket socket = new Socket(Server.HOST, Server.PORT);
outputToClient = new ObjectOutputStream(socket.getOutputStream());
inputFromClient = new ObjectInputStream(socket.getInputStream());
nummer = inputFromClient.readInt();
m.nummer = nummer;
} catch (IOException e1) {
e1.printStackTrace();
}
Thread sturen = new Thread(new Runnable() {
#Override
public void run() {
try {
while(true)
{
ArrayList<DrawingPoint> l = new ArrayList<>();
l.addAll(m.getPoints());
outputToClient.writeObject(l);
outputToClient.flush();
aantal = m.getPoints().size();
Thread.sleep(1000/5);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
sturen.start();
Thread ontvangen = new Thread(new Runnable() {
#Override
public void run() {
while(true)
{
try {
ArrayList<ThreadClass> l = new ArrayList<>();
for(int i = 0; i< 4; i++)
{
l.add((ThreadClass) inputFromClient.readObject());
}
Iterator<ThreadClass> it = l.iterator();
while(it.hasNext())
{
ThreadClass t = it.next();
if(t.nummer == nummer)
{
System.out.println(t.nummer + " " + t.list.size());
for(DrawingPoint p: t.list)
{
if(p == null) System.out.println("null");
else System.out.println(t.nummer + " X " + p.getX() + " Y " + p.getY());
}
continue;
}
System.out.println(t.nummer + " " + t.list.size());
m.otherPoints.put(t.nummer, t.list);
Thread.sleep(1000/5);
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
});
ontvangen.start();
}
}
To send objects over sockets I would reckommend you to serialize your objects first and send them, after that when your objects are received they are deserialized.
For example I convert my objects to JSON format and save it in a string, send the string to server, the server deserializes it to a java object then answers again with a serialized object and so on.
//Client side
Person person;
String stringJSON = serializer.serialize( person );
socket.getOutputStream().println(stringJSON);
//Server Side
String stringJSON = socket.getInputStream().readLine();
Person person = JSONDeserializer.deserialize( stringJSON )
I find this way much easier, if intrested I use this to serialize/desrialize http://flexjson.sourceforge.net/
By the way you have to provide setters and getters for your objects.
I am working on a homework which i have to send back and forth an object in serialized mode.I have a Client3.java file which sends an object to Server3.java.And Server3.java do the exact same thing.But when i try to read the object in Client3 he throws a java.io.IOException: Premature EOF error.From an extensive search i found that my program runs faster than the InputStream reading but i dont know how to fix this
here my 2 files
package server3;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
public class Client3 {
public static void main(String[] args) throws Exception {
try {
URL url = new URL("http://localhost:3333");
HttpURLConnection s = (HttpURLConnection) url.openConnection();
s.setDoOutput(true);
s.setDoInput(true);
s.setRequestMethod("POST");
s.setUseCaches(false);
//here we send an serialized object
Send obj = new Send();
ObjectOutputStream objOut = new
ObjectOutputStream(s.getOutputStream());
objOut.writeObject(obj);
/*********this is the chunk of code that makes the error*****************/
//here we read the serialized object
InputStream in = s.getInputStream();
ObjectInputStream ios = new ObjectInputStream(in);
SendResponse oin = (SendResponse) ios.readObject();
String gabim=oin.getGabim();
String gabimNr =oin.getGabimNr();
System.out.println(gabim);
System.out.println(gabimNr);
ios.close();
s.disconnect();
/***************************************************************************/
//catch all the possible errors
} catch (IOException ex) {
System.err.println("Gabim ne klient"+ex);
ex.printStackTrace();
}
}
}
class Send implements Serializable {
// the object to be send to the server
private static final long serialVersionUID = 1L;
int id = 1;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getPaid() {
return paid;
}
public void setPaid(int paid) {
this.paid = paid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
int amount = 2000;
int paid = 800;
String name = "Andi Domi";
}
And here it is the Server3.java file
package server3;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class Server3 {
public static void main(String[] args) throws Exception {
//create the server
HttpServer server = HttpServer.create(new InetSocketAddress(3333), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
/*******we start to read the object send from the client here*******/
//create the sream
ObjectInputStream ios = new ObjectInputStream(t.getRequestBody());
//variables we need to connect to the database
final String url = "jdbc:mysql://localhost/httpServer";
final String user = "root";
final String password = "";
try {
//create the object to be read
Send oin = (Send) ios.readObject();
int id = oin.getId();
String emri = oin.getName();
int amount = oin.getAmount();
int paid = oin.getPaid();
//jdbc try
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user,password);
//insert values into the first table
PreparedStatement s = con
.prepareStatement("INSERT INTO person(ID,Name,Amount,Paid) VALUES (?,?,?,?)");
s.setInt(1, id);
s.setString(2, emri);
s.setInt(3, amount);
s.setInt(4, paid);
s.executeUpdate();
//read the values from a table and add them to an array list
ResultSet rs = s.executeQuery("SELECT * from personat ORDER BY ID");
ArrayList<Personat> perList = new ArrayList<Personat>();
if (rs != null) {
while (rs.next()) {
Personat per = new Personat();
per.setID(rs.getInt("ID"));
per.setName(rs.getString("Name"));
per.setAmount(rs.getInt("Amount"));
perList.add(per);
}
}
//send the serialized object
String gabim="Ska asnje gabim";
String gabimNr="1";
t.sendResponseHeaders(200,0);
OutputStream os = t.getResponseBody();
SendResponse obj = new SendResponse(gabim,gabimNr,perList);
ObjectOutputStream objOut = new ObjectOutputStream(os);
objOut.writeObject(obj);
objOut.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
//ketu cojme nje object me keto te dhena
String gabim=("Dicka nuk shkoi mire me marrjen e objektit");
String gabimNr="3";
rrayList<Personat> perList = null;
t.sendResponseHeaders(200,0);
OutputStream os = t.getResponseBody();
SendResponse obj = new SendResponse(gabim,gabimNr,perList);
ObjectOutputStream objOut = new ObjectOutputStream(os);
objOut.writeObject(obj);
objOut.close();
} catch (SQLException e) {
//ketu cojme nje objekt me keto te dhena
e.printStackTrace();
String gabim=("Dicka nuk shkoi mire ne lidhje me databasin");
String gabimNr="4";
ArrayList<Personat> perList = null;
t.sendResponseHeaders(200,0);
OutputStream os = t.getResponseBody();
SendResponse obj = new SendResponse(gabim,gabimNr,perList);
ObjectOutputStream objOut = new ObjectOutputStream(os);
objOut.writeObject(obj);
objOut.close();
}
}
}
}
class Personat {
int ID;
String Name;
int Amount;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAmount() {
return Amount;
}
public void setAmount(int amount) {
Amount = amount;
}
}
class SendResponse implements Serializable {
private static final long serialVersionUID = 1L;
String gabim;
String gabimNr;
ArrayList<Personat> personList;
public SendResponse(String gabim, String gabimNr,
ArrayList<Personat> personList) {
super();
this.gabim = gabim;
this.gabimNr = gabimNr;
this.personList = personList;
}
public String getGabim() {
return gabim;
}
public void setGabim(String gabim) {
this.gabim = gabim;
}
public String getGabimNr() {
return gabimNr;
}
public void setGabimNr(String gabimNr) {
this.gabimNr = gabimNr;
}
public ArrayList<Personat> getPersonList() {
return personList;
}
public void setPersonList(ArrayList<Personat> personList) {
this.personList = personList;
}
}
after i run it from the ex.printStackTrace(); in the Client3 file i get this :
java.io.IOException: Premature EOF
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at server3.Client3.main(Client3.java:29)
i dont know hot to read the file faster or make my program slower ,do someone have any idea ?
I don't think it's due to the speed of your program, I would expect readObject() to block if there wasn't anything to read. Instead I think you are missing a terminator in the data you are trying to read, you need to take a look at the data that is being sent to your program. A similar issue was resolved here, though it is using readLine()