I don't get what's the problem with this Dictionary created using Arraylist and Socket . I get SocketException error I don't know why also I tried to created a method to read....
ClientCode:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.net.SocketException;
public class Client {
public static void main(String[] args) {
int N;
Scanner sc=new Scanner(System.in);
Socket cs=null;
BufferedReader bfr=null;
PrintWriter pw=null;
try{
cs=new Socket("localhost",5000) ;
InputStreamReader isr=
new InputStreamReader(cs.getInputStream());
bfr=new BufferedReader(isr);
pw=new PrintWriter(cs.getOutputStream());
System.out.println("S-a setat reteaua!");
for(;;){
System.out.print("Cuvantul care trebuie tradus:");
String cuvant=sc.nextLine();
//daca este STOP : se va deconecta
if(cuvant.equals("STOP")) {pw.println(""); pw.flush();break;}
else {pw.println(cuvant); pw.flush();}
//Citim raspuns server:
String textIn=bfr.readLine();
if(textIn==null)break;
System.out.println(textIn);
}//for;;
}catch( IOException e){
e.printStackTrace();
}
System.out.println("Clientul s-a deconectat!");
}//main
}
ClientHandler code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.net.SocketException;
//Clasa firului de execuție pentru tratarea clientului conectat la server:
class FirClient extends Thread {
private Socket cs;
private BufferedReader bfr;
private PrintWriter pw;
ArrayList<String> listacuvinte;//la creare firului incarcam datele
// din fisier intr-un ArrayList
public FirClient(Socket cs) {
try {
this.cs = cs;
pw = new PrintWriter(cs.getOutputStream());
InputStreamReader isr =
new InputStreamReader(cs.getInputStream());
bfr = new BufferedReader(isr);
//incarcam fisierul cuvinte .txt in ArrayList:
listacuvinte = new ArrayList<String>();
System.out.println("Se copiaza fisier in al");
FileReader f = new FileReader("date.txt");
BufferedReader bf = new BufferedReader(f);
for (; ; ) {
String s = bf.readLine();
if (s == null) break;//s-a terminat fisier
listacuvinte.add(s);
}
bf.close();
f.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void run()
{
try{
for(;;){
String cuvant = bfr.readLine();
if(cuvant == null)break;
if(cuvant.equals(""))break;
//cautare cuvant in ArrayList:
boolean gasit=false;
for(int i=0;i<listacuvinte.size();i+=2){
if(listacuvinte.get(i).equals(cuvant)){
String crt= listacuvinte.get(i+1);
gasit=true;
break;
}
}
}//for;;
}catch(final Exception e){e.printStackTrace();}
}//run
}
ServerCode:
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class Server {
public static void main(String[] args) {
System.out.println("Am pornit server...");
try{
ServerSocket ss=new ServerSocket(5000);
for(;;){
Socket cs=ss.accept();
FirClient firPtClient=new FirClient(cs);
firPtClient.start();
System.out.println("Avem o conectare!");
}//for;;
}catch(Exception e){
e.printStackTrace();
}
}
}