Simple Socket Creation

Hi,

Following is the client side program:

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.event.*;

import javax.swing.*;

publicclass TestClient{

publicstaticvoid main(String args[]){

try{

Socket sock1 =new Socket("xxx.xxx.xxx.xx", 5000);

InputStreamReader streamReader =new InputStreamReader(sock1.getInputStream());

BufferedReader bw2 =new BufferedReader(streamReader);

while(bw2.readLine()!=null){

System.out.println(bw2.readLine());

}

}

catch(IOException ex){ex.printStackTrace();}

}

}

Following is the server side program:

import java.io.*;

import java.net.*;

import java.util.*;

import java.awt.event.*;

import javax.swing.*;

publicclass TestServer{

publicstaticvoid main(String args[]){

try{

ServerSocket sock =new ServerSocket(5000);

while(true){

Socket socket = sock.accept();

FileReader fe =new FileReader("/home/archimedes/123.txt");

BufferedReader bw =new BufferedReader(fe);

PrintWriter pw =new PrintWriter(socket.getOutputStream());

while(bw.readLine()!=null)

{

pw.write(bw.readLine());

}

pw.close();

bw.close();

}

}catch(IOException ex){ex.printStackTrace();}

}

}

When I try to run these programs (server followed by client), the client console just sits there without reading the contents of the file. Am I doing something wrong here?

Thanks.

[3423 byte] By [Javajockeya] at [2007-11-26 18:47:31]
# 1

Change

// Client

String line;

while((line = bw2.readLine()) != null)

System.out.println(line);

and

// Server

String line;

while((line = bw.readLine()) != null)

pw.println(line);

pw.flush();

Remember to close() all streams and sockets as well.

#

duckbilla at 2007-7-9 6:21:28 > top of Java-index,Java Essentials,Java Programming...