sockets - File Management Server in JAVA -
i have application(swt) need manage file @ server end. managing mean 3 things, writing contents file, applying read/write lock mechanism , displying same in textarea
. need create multithreaded server achieve application(which eclipse based plugin) accepts multiple users. i'm new client-server thing , socket programming , i've read few tutorials still havent found optimum solution it. not need code(there plenty on internet), rather need way or steps it. thanks.
also, found server code works fine. not displaying desired results.
what rellay want file maintain author's name. revision number , related destils svn on server side.
server program:
public void onserver() throws exception { string sentbyclient; string line1 = null; serversocket socket = new serversocket(6789); while(true) { system.out.println("listening..."); socket connectionsocket = socket.accept(); bufferedreader infromclient = new bufferedreader(new inputstreamreader(connectionsocket.getinputstream())); dataoutputstream outtoclient = new dataoutputstream(connectionsocket.getoutputstream()); sentbyclient = infromclient.readline(); system.out.println("received: " + sentbyclient); file file=new file("historyfile.txt");//------------------server file bufferedwriter writ=new bufferedwriter(new filewriter(file)); writ.write(sentbyclient); writ.close(); bufferedreader read=new bufferedreader(new filereader(file)); while((line1=read.readline())!=null) { system.out.println(line1); } outtoclient.writebytes(line1); } }
client code:
public void onclient(string param) throws exception { socket clientsocket = new socket("localhost", 6789); dataoutputstream outtoserver = new dataoutputstream(clientsocket.getoutputstream()); bufferedreader infromserver = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); //sentence = infromuser.readline(); sentence=param; // here i'm sending author name, revision details, etc svn server outtoserver.writebytes(sentence); newsentence = infromserver.readline(); system.out.println("from server: " + newsentence); historyarea.append(newsentence); }
what need maintaining file on server , displaying file contents on textarea(historyarea). i'm sending history data svn file.
desired ouptut:
revision number: 1 author: time:xxxx changed path:xxxx ------------------- revision number: 2 author: time:xxxx changed path:xxxx ------------------ revision number: 3 author: time:xxxx changed path:xxxx
ouptut i'm getting first revision:
revision number: 1 author: time:xxxx changed path:xxxx
here in line
bufferedwriter writ=new bufferedwriter(new filewriter(file));
you opening file in write mode, write operation overwrite existing contents. instead
bufferedwriter writ=new bufferedwriter(new filewriter(file,true));
this open file in append mode.
Comments
Post a Comment