Sending an object through a socket in java -
i have 2 java netbeans projects, 1 server other client. have message class have created want pass server , other way client after modification done @ server. have included class message in both projects. use objectoutputstream
, objectinputstream
pass object. connection between server , client ok , object passes through @ server when read object objectinputstream
using readobject()
method type cast message
class. classnotfoundexception thrown @ server. cant find message class. how can resolve this?
code client:
public void startclient() { try { // create socket socket clientsocket = new socket(host, port); // create input & output streams server objectoutputstream outtoserver = new objectoutputstream(clientsocket.getoutputstream()); objectinputstream infromserver = new objectinputstream(clientsocket.getinputstream()); // read modify // todo here /* create message object send */ linkedlist<message> msglist = new linkedlist<>(); message msg = new message(); msg.setmessage("kasun"); msg.setindex(1); msg.setaverage(5.5f); msglist.push(msg); /* send message object server */ outtoserver.writeobject(msglist); /* retrive message object server */ linkedlist<message> infromserverlist = new linkedlist<>(); message msgfrmserver = null; infromserverlist = (linkedlist<message>)infromserver.readobject(); msgfrmserver = infromserverlist.pop(); /* print out recived message */ system.out.println("message: " + msgfrmserver.getmessage()); system.out.println("index: " + msgfrmserver.getindex()); system.out.println("average: " + msgfrmserver.getaverage()); clientsocket.close(); } catch (exception e) { system.err.println("client error: " + e.getmessage()); system.err.println("localized: " + e.getlocalizedmessage()); system.err.println("stack trace: " + e.getstacktrace()); } }
code server
public void startserver() { try { serversocket welcomesocket = new serversocket(port); while (true) { // create client socket socket clientsocket = welcomesocket.accept(); system.out.println("socket extablished..."); // create input , output streams client objectoutputstream outtoclient = new objectoutputstream(clientsocket.getoutputstream()); objectinputstream infromclient = new objectinputstream(clientsocket.getinputstream()); // read modify // todo here /* create message object , retrive information */ linkedlist<message> inlist = new linkedlist<>(); message inmsg = null; inlist = (linkedlist<message>)infromclient.readobject(); inmsg = inlist.pop(); /* modify message object */ inmsg.setmessage(inmsg.getmessage().touppercase()); inmsg.setindex(5); inmsg.setaverage(10.5f); /* send modified message object */ outtoclient.writeobject(inmsg); } } catch (exception e) { system.err.println("server error: " + e.getmessage()); system.err.println("localized: " + e.getlocalizedmessage()); system.err.println("stack trace: " + e.getstacktrace()); system.err.println("to string: " + e.tostring()); } }
the exception thrown @ "server"
java.lang.classnotfoundexception: rusl.online.examination.system.client.utility.message
would have use java rmi? there solution without using rmi?
make sure message on server side rusl.online.examination.system.client.utility.message
. seems not case.
Comments
Post a Comment