java - server make a txt file and client write it -


i have project has server creates empty text file.

once client stops writing file, server should read , display results.

my problem client connecting server, whatever text client sending not being written on server side. in addition, when exit, server doesn't quit.

this have far:

the server

import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.datainputstream; import java.io.file; import java.io.filewriter; import java.io.ioexception; import java.io.inputstreamreader; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.outputstream; import java.io.outputstreamwriter; import java.io.printstream; import java.io.printwriter; import java.net.serversocket; import java.net.socket; import java.util.scanner;  public class server {      public static void main(string args[]) throws ioexception {          bufferedwriter out;// = new bufferedwriter(new filewriter("c://users//vagos//desktop//file.txt"));           serversocket echoserver = null;             string line;             datainputstream is;             printstream os;             socket clientsocket = null;      // try open server socket on port 9999            try {                echoserver = new serversocket(55);             }             catch (ioexception e) {                system.out.println(e);             }        // create socket object serversocket listen , accept      // connections.     // open input , output streams      try {                clientsocket = echoserver.accept();                = new datainputstream(clientsocket.getinputstream());                os = new printstream(clientsocket.getoutputstream());                out = new bufferedwriter(new filewriter("c://users//vagos//desktop//file.txt"));     // long receive data, echo data client.                 while (true) {                  line = is.readutf();                  os.println(line);                   os.flush();                  out.write(line);                  out.flush();                }             }        catch (ioexception e) {                system.out.println(e);             }         }     } 

here client:

import java.io.*; import java.net.inetaddress; import java.net.serversocket; import java.net.socket; import java.net.unknownhostexception; import java.util.scanner;  public class client {      public static void main(string[] args) {         socket smtpsocket = null;           dataoutputstream os = null;         datainputstream = null;         string strout;         scanner in = new scanner(system.in);         try {             smtpsocket = new socket("localhost", 55);             os = new dataoutputstream(smtpsocket.getoutputstream());             = new datainputstream(smtpsocket.getinputstream());         } catch (unknownhostexception e) {             system.err.println("don't know host: hostname");         } catch (ioexception e) {             system.err.println("couldn't i/o connection to: hostname");         }      if (smtpsocket != null && os != null && != null) {             try {                 do{                     system.out.print("write client send: ");                  strout = in.nextline();                 os.writebytes(strout);}                 while(!strout.equals("exit"));                 os.close();                 is.close();                 smtpsocket.close();                } catch (unknownhostexception e) {                 system.err.println("trying connect unknown host: " + e);             } catch (ioexception e) {                 system.err.println("ioexception:  " + e);             }         }     }            }; 

try this.

in server

    // open input , output streams      try {         clientsocket = echoserver.accept();         = new datainputstream(clientsocket.getinputstream());         inputstreamreader ir = new inputstreamreader(is);         bufferedreader br = new bufferedreader(ir);         os = new printstream(clientsocket.getoutputstream());         out = new bufferedwriter(new filewriter("c://users//vagos//desktop//file.txt"));         // long receive data, echo data client.          while (true) {             line = br.readline();             system.out.println(line);             os.println(line);             os.flush();             if( line != null ){              out.write(line + '\n');              out.flush();             }         }     } 

and 'exit' part of client try after changing
while(!strout.equals("exit"));

while(!strout.equalsignorecase("exit"));

hope helps !!


Comments