大家帮忙看下为什么会报异常啊。。。java.net.SocketException: Connection reset
//客户端
public class TcpUploadClient {
private static Socket socket = null;
private static BufferedReader bufr = null;
public static void main(String args[]) {
try {
socket = new Socket("127.0.0.1", 12000);
bufr = new BufferedReader(
new FileReader(
"F:\\TcpUploadClient.java"));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 往输出流里面写入文本文件名和结束标记
out.println("TcpUploadClient.java,@#$%");
String line;
while ((line = bufr.readLine()) != null) {
out.println(line);
}
socket.shutdownOutput();
// 写出结束标记
// out.println("@#$%");
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String serStr = in.readLine();
System.out.println(serStr);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close();
if (bufr != null)
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
public class TcpUploadServer {
private static ServerSocket serverSocket = null;
private static PrintWriter pw = null;
private static Socket socket = null;
public static void main(String args[]) {
try {
serverSocket = new ServerSocket(12000);
socket = serverSocket.accept();
System.out.println(socket.getInetAddress().getHostAddress()
+ "...connected");
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// 获取文本名和结束标记
String arr[] = in.readLine().split(",");
pw = new PrintWriter(new FileWriter(
"F:\\Test\\"+ arr[0]), true);
String line;
while ((line = in.readLine()) != null) {
// 判断结束标记
// if (line.equals(arr[1]))
// break;
pw.println(line);
}
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("上传文本文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (serverSocket != null)
serverSocket.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
if (pw != null)
pw.close();
}
}
}
|