See modified client and server code. I removed stuff not required for my
test.
import java.net.*;
import java.io.*;
public class strtest_client
{
public static void main(
String[] args) throws IOException {
String IP = args[0];
String port = args[1];
// String IP = "192.168.1.3";
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(IP, 4444);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket
.getInputStream()));
} catch (Exception ex) {
System.out.println("error");
ex.printStackTrace();
System.exit(1);
}
try {
/* DataInputStream inStream = new DataInputStream(new FileInputStream(
"myFile.dat")); */
// convert to
java input format
BufferedReader infile = new BufferedReader(new FileReader(new File("myFile.dat")));
DataOutputStream outfile = new DataOutputStream(new FileOutputStream(new File("myFile.java.dat")));
while (true) {
String line = infile.readLine();
if (line == null) break; outfile.writeInt(Integer.parseInt(line.trim()));
}
infile.close();
outfile.close();
DataInputStream inStream = new DataInputStream(new FileInputStream(
"myFile.java.dat"));
try {
while (true) {
int num = inStream.readInt();
System.out.println(num);
out.print(num);
}
} catch (IOException e) {
System.out.println("finish reading the file");
} finally {
inStream.close();
out.close();
in.close();
echoSocket.close();
// add these
inStream = null;
out = null;
in = null;
}
//problem.make sure that in stream is open
if (in != null) {
System.out.println("echo: " + in.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
=== Server code ====
import java.io.*;
import java.net.*;
public class strtest_server {
public static void main(String[] args) throws IOException {
int port = Integer.parseInt(args[0]);
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Could not listen on port: "+port);
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
int [] arr = new int[3];
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
out.println(" ");
int i =0;
while ((inputLine = in.readLine()) != null) {
Integer num = Integer.parseInt(inputLine);
arr[i]=num;
i++;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
To run on same machine, I start server as:
java strtest_server 4444 client as
java strtest_client 10.0.40.103 4444 Hope this help