java - Client socket doesn't take any input from cmd -
i create program server sends list of files client client can request check contents. sends list of files client doesn't take input console. server program
import java.util.*; import java.io.*; import java.net.*; class tcpserver{ public static void main(string args[]) throws exception{ serversocket server = new serversocket(4888); while(true){ socket client = server.accept(); system.out.println(client); dataoutputstream out = new dataoutputstream(client.getoutputstream()); file path = new file("c://testjava"); string[] files = path.list(); string send = ""; for(string file:files){ send = send + file + "\n"; } out.writebytes(send); bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream ())); string search_file = in.readline(); string searching = ""; for(string file:files){ if (file.equals(search_file)){ searching = search_file; } } if(searching.equals("")){ out.writebytes("requested file not exist"); client.close(); } scanner file = new scanner(new filereader(searching)); while(file.hasnextline()){ out.writebytes(file.nextline()); } client.close(); } } }
this client program
import java.util.*; import java.io.*; import java.net.*; class tcpclient{ public static void main(string args[]) throws exception{ socket client = new socket("localhost",4888); bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream())); string display = ""; while ((display = in.readline()) != null) { system.out.println(display); } system.out.println("\nchoose file"); scanner src = new scanner(system.in); string ask_file = src.nextline(); dataoutputstream out = new dataoutputstream(client.getoutputstream()); out.writebytes(ask_file); display = ""; while ((display = in.readline()) != null) { system.out.println(display); } } }
can explain why client isn't accepting input? thanx
in client, in.readline()
blocks until socket closed.
since don't want close socket yet, have server send special message match in loop. when matched, break out of loop.
also, readline/nextline methods gobble newlines, need add @ejp said. edited coded below. tested , seems working now.
tcpserver
import java.util.*; import java.io.*; import java.net.*; class tcpserver{ public static void main(string args[]) throws exception{ serversocket server = new serversocket(4888); while(true){ socket client = server.accept(); system.out.println(client); dataoutputstream out = new dataoutputstream(client.getoutputstream()); file path = new file("c://users/brian/desktop"); string[] files = path.list(); string send = ""; for(string file:files){ send = send + file + "\n"; } send = send + "end\n"; // add ------------------------------> out.writebytes(send); bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream ())); string search_file = in.readline(); string searching = ""; for(string file:files){ if (file.equals(search_file)){ searching = search_file; } } if(searching.equals("")){ out.writebytes("requested file not exist"); client.close(); } scanner file = new scanner(new filereader(searching)); while(file.hasnextline()){ out.writebytes(file.nextline() + "\n"); // add newline here ------------------> } client.close(); } } }
tcpclient
import java.util.*; import java.io.*; import java.net.*; class tcpclient{ public static void main(string args[]) throws exception{ socket client = new socket("localhost",4888); bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream())); string display = ""; // add test "end" here ---------------------------------------------> while ((display = in.readline()) != null && !display.equals("end")) { system.out.println(display); } system.out.println("\nchoose file"); scanner src = new scanner(system.in); string ask_file = src.nextline() + "\n"; // add newline here -----------> dataoutputstream out = new dataoutputstream(client.getoutputstream()); out.writebytes(ask_file); display = ""; while ((display = in.readline()) != null) { system.out.println(display); } } }
Comments
Post a Comment