Odgovorio sam ti na drugom forumu gde si postavio istu temu, al reko ajd i ovde, mozda nekom drugom zatreba..
Posto se nisam do sada bavio java i socketima, a i imao sam par sati slobodnog vremena odradio sam ti program ceo za vezbu sebi i iztestirao ga. Bice ti potrebno paket usluge.Citaj od gospodina Lasla krausa( mrzelo me je da pisem citanje sa konzole iskreno) :P
[url]http://kondor.etf.rs/~kraus/knjige/programi/rzj2.zip[/url]
u zip fajlu imas Folder Usluge, samo ga prevuci u svoj projekat u eclipse..
E ovako evo ti kod,
Server:
Klasa ReadFromFile
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFromFile {
File file;
Scanner scanner;
public ReadFromFile(String f)
{
file = new File (f);
try {
System.out.println("Trying to read file: "+f);
scanner = new Scanner (file);
} catch (FileNotFoundException e) {
System.out.println ("File not found!");
System.exit (0);
}
}
public Scanner GetScanner(){return scanner;}
}
Klasa Sserver
Code:
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Sserver {
ServerSocket s;
Socket s1;
OutputStream s1out;
DataInputStream streamIn;
DataOutputStream dos;
public Sserver(int port){
try {
System.out.println("Binding to port " + port + ", please wait ...");
s=new ServerSocket(port);
System.out.println("Server started: " + s);
System.out.println("Waiting for a client ...");
s1 = s.accept();
System.out.println("Client accepted: " + s1);
dos = new DataOutputStream(s1.getOutputStream());
open();
Boolean stop= false;
String line=null;
while(!stop){
open();
try
{
line = streamIn.readUTF();
System.out.println("File name receved:"+ line);
stop = line.equals(".stop"); //sve dok mu ne posaljes .stop server ce raditi..
}
catch(IOException ioe)
{
stop = true;
close();
}
if(!stop){
ReadFromFile MyFile=new ReadFromFile(line);
Scanner scn=MyFile.GetScanner();
while(scn.hasNext())
{
String temp=scn.nextLine();
dos.writeUTF(temp);
}
dos.writeUTF(".eof");
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public void open() throws IOException
{ streamIn = new DataInputStream(new BufferedInputStream(s1.getInputStream()));
}
public void close() throws IOException
{ if (s1 != null) s1.close();
if (streamIn != null) streamIn.close();
}
}
Klasa main za server
Code:
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to server");
Sserver server1= new Sserver(1225);//1225 port koji osluskuje
}
}
E sad klient:
Klasa Sclient:
Code:
import java.net.*;
import java.io.*;
public class Sclient {
Socket s1;
InputStream s1In;
OutputStream s1Ou;
DataInputStream dis;
DataOutputStream dos;
public Sclient(String host, int port)
{
System.out.println("Establishing connection. Please wait ...");
try{
s1 = new Socket(host,port);
System.out.println("Connected to "+host+":"+port);
s1In = s1.getInputStream();
s1Ou = s1.getOutputStream();
dis = new DataInputStream(s1In);
dos = new DataOutputStream(s1Ou);
}catch(UnknownHostException uhe)
{ System.out.println("Host unknown: " + uhe.getMessage());}
catch(IOException ioe)
{System.out.println("Unexpected exception: " + ioe.getMessage());}
}
public void SendReq(String fname)//Funkcija koja salje ime fajla serveru
{
try {
dos.writeUTF(fname);
} catch (IOException e) {
System.out.println("Error sending request: "+ e.getMessage());
e.printStackTrace();
}
}
public void ReadFile(){ //Funckija koja prima fajl od servera
try {
Boolean eof = false;
while(!eof){ //Gledamo dal smo stigli do kraja fajla
String line=dis.readUTF();
eof = line.equals(".eof");
if(!eof){System.out.println(line);} //Ako nismo primili .eof za kraj odstampaj linuju
else {System.out.println("End of file");}
}
} catch (IOException e) {
System.out.println("Error receving file: "+ e.getMessage());
e.printStackTrace();
}
}
}
i klasa main za klient:
Code:
import usluge.Citaj;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome, enter server address, and port");
String server= Citaj.String();//citamo server i port
int port =Citaj.Int(); // sa glavnog ulaza
Sclient client = new Sclient(server,port); //pravimo objekat klase Sclient
while(true){ //Vrtimo se sve dok ne izdamo narednu .exit
System.out.println("Enter file name or path on server or .exit to exit");
String file=Citaj.String();
if(file.equals(".exit"))System.exit(1);
client.SendReq(file);//saljemo request za file
client.ReadFile(); // Primamo file i ispisujemo
}
}
}
Evo nadam se da je sve jasno.. Koristio sam engleski za poruke, posto tako volim jbg.. :P Kod moze mnogo da se ulepsa ali nisam imao vremena za to..