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();
           }


    }

}

You need to share the exact details of the socket exception

error.JPG

This post has no text-based content.

That's what error I get after I enter the word... in program I need to get the translation which is the next position of the word which I need to translate.

Despite your variable and class names seemingly chosen to make this as hard as possible...

It looks like the server may have closed the connection before the client gets to read a whole line. You don't say whether this happens on the first word or a subsequent one. Some more debug info is needed here. eg trace every string being sent or received at both ends, trace when the loops are exited etc

commented: I found the solution . First of all I didn't look carefully in text file and I put words in text file in a wrong way. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.