[ miros21k @ 14.03.2005. 13:55 ] @
Cao narode, imam mali problemcic sa Java programiranjem (veliki zaprave mrzim programiranje, ali sad necemo o tome). Na faksu moramo da napisemo TCP Echo Client koji ce da posalje neku poruku perma TCP Echo Serveru i tad ce taj Server da "odjekne" (ili ti echo na engleskom) poruku nazad. Znaci server drzi nastavnik a ja kao student moram da napisem Client program kojeg kad pokrenem koristeci "command line" u formatu: javac Client.java (da se compile) java Client IPadresa PORTbroj "poruka" (da posalje poruku) znaci args[0]=server args[1]=broj porta args[2]=poruka Ne mora da bude bas u tom redosledu al je "pozeljno". E sad ja posto jako malo znam Java Programming ja sam lepo otisao na GOOGLE i ukucao TCPEchoClient.java i nasao neke programcice i izabrao jedan koji najvise razumijem ali i dalje ne razumem sve. Pa bi zamolio citaoce da mi objasne sta TACNO svaka linija programa radi. Code: import java.net.*; // for Socket import java.io.*; // for IOException and Input/OutputStream public class Client { public static void main(String[] args) throws IOException { if ((args.length < 2) || (args.length > 3)) // Test for correct # of args throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]"); String server = args[0]; // Server name or IP address // Convert input String to bytes using the default character encoding byte[] byteBuffer = args[1].getBytes(); int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7; // Create socket that is connected to server on specified port Socket socket = new Socket(server, servPort); System.out.println("Connected to server...sending echo string"); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); out.write(byteBuffer); // Send the encoded string to the server // Receive the same string back from the server int totalBytesRcvd = 0; // Total bytes received so far int bytesRcvd; // Bytes received in last read while (totalBytesRcvd < byteBuffer.length) { if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd, byteBuffer.length - totalBytesRcvd)) == -1) throw new SocketException("Connection close prematurely"); totalBytesRcvd += bytesRcvd; } System.out.println("Received: " + new String(byteBuffer)); socket.close(); // Close the socket and its streams } } Razumijem da treba da se spojim na server putem socketa zvan "sock", razumijem da mora neki buffer da ima koji ce da spremi ukucana slova sa tastature, i razumijem da onda to mora da posalje i razumijem da mora da tu poruku primi od servera i pokaze na ekran (isto putem nekog buffera). Kao sto vidite vec ima //comments ali ako bi neko mogao jos detaljnije da mi objasni sve to, POGOTOVO int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7; i if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd, byteBuffer.length - totalBytesRcvd)) == -1) throw new SocketException("Connection close prematurely"); totalBytesRcvd += bytesRcvd; Pliz narode pomagaj. HVALA UNAPRED !!! |