This is the code. it seems to be a problem with the threads not the socket connection, when i run it it just uses all the cpu and hangs Help me please i`m loosing my mind over this coz i can`t really find something wrong with it and it is due tomorrow...
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package chat; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * * @author taghack */ class Read extends Thread { BufferedReader in; public Read(BufferedReader in) { this.in=in; } @Override public void run() { try { while(true) { if(in.ready()) { try { //byte[] tmparr=new byte[100000]; //in.read(tmparr); System.out.println(in.readLine()); } catch(Exception e) { e.printStackTrace(); } } } } catch(Exception e) { e.printStackTrace(); } } } class Write extends Thread { PrintWriter out; public Write(PrintWriter out) { this.out=out; } @Override public void run() { //byte[] tmparr=new byte[100000]; while(true) { if(Main.consoleinput.hasNext()) { try { String message; message=Main.consoleinput.next(); out.println(message); out.flush(); } catch(Exception e) { e.printStackTrace(); } } } } } public class Main { public static String nickname; public static Scanner consoleinput=new Scanner(System.in); /** * @param args the command line arguments */ public static void main(String[] args) { try { // TODO code application logic here System.out.println("Please input your nickname:"); nickname=consoleinput.nextLine(); System.out.println("Please input the ip of the other participant in the chat session"); String ip=consoleinput.nextLine(); boolean surverisrunning=false; Socket mainsocket; int numberoftries=0; do { try { numberoftries++; System.out.println("trying to connect "+numberoftries); Socket temp=new Socket(ip,3000); surverisrunning=true; temp.close(); } catch(Exception e) { surverisrunning=false; e.printStackTrace(); } } while(surverisrunning==false && numberoftries<10); if(surverisrunning) { mainsocket=new Socket(ip,3000); System.out.println("CONNECTION ESTABLISHED AS CLIENT!"); } else { System.out.println("PleaseWainForConnctionFromClient"); ServerSocket a=new ServerSocket(3000); mainsocket=a.accept(); System.out.println("CONNECTION ESTABLISHED AS SERVER!"); surverisrunning=true; } BufferedReader in=new BufferedReader(new InputStreamReader(mainsocket.getInputStream())); PrintWriter out=new PrintWriter(new OutputStreamWriter(mainsocket.getOutputStream())); Read read1=new Read(in); Write write1=new Write(out); read1.start(); write1.start(); } catch(Exception e) { e.printStackTrace(); } } }