zahteva jedan za drugim da obradi.
Sa Klijenta dajemo komandu pruzmi<ime fajla>. Server treba da potrazi fajl u svom lokalnom direktorijumu i
da posalje sadrzaj fajla klijentu tj. ispise sadrzaj na ekranu. U pitanju je jednostavan .txt fajl.
Sa komandom Stop klijent signalizira serveru tj. server prestaje sa radom.
Code:
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
// Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1=s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF("Hi there");
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
}
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
// Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1=s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF("Hi there");
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
}
i klijent
Code:
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1234
Socket s1 = new Socket("localhost",1234);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1234
Socket s1 = new Socket("localhost",1234);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
E sada imam i gotovu klasu ReadFromFile ali mi nije jasno kako da povezem tj. promenim klase Client i Server da
dobijem zeljeni rezultat.
Svaka pomoc dobrodosla!
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFromFile {
public static void main(String[] args) {
ReadFromFile rff=new ReadFromFile();
rff.readFile();
}
public void readFile()
{
File file = new File ("input.txt");
Scanner scanner = null;
try {
scanner = new Scanner (file);
} catch (FileNotFoundException e) {
System.out.println ("File not found!");
System.exit (0);
}
while(scanner.hasNext())
{
String tmp=scanner.nextLine();
System.out.println(tmp);
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFromFile {
public static void main(String[] args) {
ReadFromFile rff=new ReadFromFile();
rff.readFile();
}
public void readFile()
{
File file = new File ("input.txt");
Scanner scanner = null;
try {
scanner = new Scanner (file);
} catch (FileNotFoundException e) {
System.out.println ("File not found!");
System.exit (0);
}
while(scanner.hasNext())
{
String tmp=scanner.nextLine();
System.out.println(tmp);
}
}
}