Question : Java TCP - Decaprecated Code

OK so I got a Client and Server. The code however is fixed up and it compiles without errors. I just replace DataStreamInput with BufferedReader however The Output does not work. The Server does Not seem to send to the clients.... can you guys help?

Server.java:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
import java.io.*;
import java.net.*;
import java.util.*;

public class Server{

    static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

	static  clientThread t[] = new clientThread[10];


    public static void main(String args[]) {

	// The default port



	int port_number=2222;

	if (args.length < 1)
	    {
		System.out.println("Usage: java Server \n"+
				   "Now using port number="+port_number);
	    } else {
		port_number=Integer.valueOf(args[0]).intValue();
	    }

        try {
	    serverSocket = new ServerSocket(port_number);
        }
        catch (IOException e)
	    {System.out.println(e);}

	while(true){
	    try {
		clientSocket = serverSocket.accept();
		for(int i=0; i<=9; i++){
		    if(t[i]==null)
			{
			    (t[i] = new clientThread(clientSocket,t)).start();
			    break;
			}
		}
	    }
	    catch (IOException e) {
		System.out.println(e);}
	}
    }
}

class clientThread extends Thread{

    BufferedReader is = null;
    PrintStream os = null;
    Socket clientSocket = null;
    clientThread t[];
    Random roll = new Random();

    public clientThread(Socket clientSocket, clientThread[] t){
	this.clientSocket=clientSocket;
        this.t=t;
    }

    public void run()
    {
		System.out.println("Check 1");
	String line;
        String name;
        int py;
	try{
		System.out.println("Check 2");
	    is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	    System.out.println("Check 3");
	    os = new PrintStream(clientSocket.getOutputStream());
	    System.out.println("Check 4");
	    int px = roll.nextInt(6);
	    os.println("Enter your name.");
	    System.out.println("Check 5");
	    name = is.readLine();
	    System.out.println("Check 6");

	    os.println("Hello "+name+" to our chat room. Your attack: " + px + "\nTo leave enter /quit in a new line");
	    for(int i=0; i<=9; i++)
		if (t[i]!=null && t[i]!=this)
		    t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
	    while (true) {
		line = is.readLine();
                if(line.startsWith("/quit")) break;
		for(int i=0; i<=9; i++)
		    if (t[i]!=null)  t[i].os.println("<"+name+":"+px+"> "+line);
	    }
	    for(int i=0; i<=9; i++)
		if (t[i]!=null && t[i]!=this)
		    t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );

	    os.println("*** Bye "+name+" ***");

	    for(int i=0; i<=9; i++)
		if (t[i]==this) t[i]=null;

	    is.close();
	    os.close();
	    clientSocket.close();
	}
	catch(IOException e){};
    }
}


For this file... it worked but it was deprecated so I changed

1:
is = new DataInputStream(clientSocket.getInputStream());

to
1:
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))


and DataInputStream is == null to BufferedReader is == null.

Client.java

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
import java.io.*;
import java.net.*;

public class Client implements Runnable{

    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;

    public static void main(String[] args) {

	// The default port

	int port_number=2222;
        String host="localhost";

	if (args.length < 2)
	    {
		System.out.println("Usage: java Client  \n"+
				   "Now using host="+host+", port_number="+port_number);
	    } else {
		host=args[0];
		port_number=Integer.valueOf(args[1]).intValue();
	    }

	try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
        }

        if (clientSocket != null && os != null && is != null) {
            try {

                new Thread(new Client()).start();

		while (!closed) {
                    os.println(inputLine.readLine());
                }

		os.close();
		is.close();
		clientSocket.close();
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }

    public void run() {
	String responseLine;

	// Keep on reading from the socket till we receive the "Bye" from the server,
	// once we received that then we want to break.
	try{
		System.out.println("Tryin.g...");
	    while ((responseLine = inputLine.readLine()) != null) {
		System.out.println(responseLine);
		if (responseLine.indexOf("*** Bye") != -1) break;
	    }
            closed=true;
	} catch (IOException e) {
	    System.err.println("IOException:  " + e);
	}
    }
}


For Client.java, also had decaprecated code as well so I removed:

1:
is = new DataInputStream(clientSocket.getInputStream());


and replaced any line with is to inputLine.

Can you fix this?

Answer : Java TCP - Decaprecated Code

Nine times out of ten these problems are due to lack of coordination between client writes and server reads and vice versa. For instance, because of streams blocking, your program will lock up if it tries to read when nothing has been written nor will be.

I suggest you go through your code with a fine toothed comb for this sort of problem
Random Solutions  
 
programming4us programming4us