黑马程序员技术交流社区
标题:
这题结果总不对
[打印本页]
作者:
酱爆
时间:
2013-10-2 23:26
标题:
这题结果总不对
本帖最后由 酱爆 于 2013-10-3 20:19 编辑
没有抛异常, 不知道是哪不对,大家帮忙看看
import java.io.*;
import java.net.*;
class TextClient
{
public static void main(String[] args)
{
Socket s = null;
BufferedReader br = null;
try
{
s = new Socket("192.168.1.102",10001);
System.out.println("AAAAAAAAAAAAAAAGGGGGGGGGGGGGGGGGGGFFFF");
System.out.println(s.getInetAddress().getHostAddress());
br = new BufferedReader(new FileReader("IPDome.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while ((line = br.readLine()) != null)
{
out.println(line);
}
out.println("over");
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = null;
str = bufIn.readLine();
System.out.println(str);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();
if (s != null)
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
class TextServer
{
public static void main(String[] args)
{
ServerSocket ss = null;
Socket s = null;
PrintWriter pw = null;
try
{
ss = new ServerSocket(10001);
s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
System.out.println(ip);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(new FileWriter("D:\\123.txt"),true);
String str = null;
while ((str = bufIn.readLine()) != null)
{
if ("over".equals(str))
break;
pw.println(str);
}
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("上传成功!");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (pw != null)
pw.close();
if (s != null)
s.close();
if (ss != null)
ss.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
复制代码
作者:
straw
时间:
2013-10-3 12:29
又一个敲代码不写注释的!苦啊 哥们 不过还好你这功能是常见的,要不就不好读了.
1,在客户端里当你写完数据流的时候一定关闭套接字符流s.shutdownOutPut();这步很关键的,如果不执行的话,服务端是受不到数据流的.
2,在服务端里,在向客户端写完数据流时也要关闭套接字符流,否则客户端会认为服务端还有数据要写,所以客户端是接受不到服务端的数据流的.
如果你还看不懂的话,我下面的代码跟你的要求是类似的,你看下就知道了:
package day23;
/*
* 多线程 tcp传输图片
*
* */
import java.net.*;
import java.io.*;
public class TcpImage {
public static void main(String[] ages) {
// 客户端
new Thread(new Runnable() {
//创建服务端套接字
Socket sk = null;
//定义文件读取流
FileInputStream fis = null;
public void run() {
try {
sk = new Socket(InetAddress.getByName("192.168.128.1"),
2008);
OutputStream os = sk.getOutputStream();
//将服务获取的读取流转换成字符流
BufferedReader bisr = new BufferedReader(
new InputStreamReader(sk.getInputStream()));
//获取原文件的读取流
fis = new FileInputStream("E:\\a.txt");
byte[] bt = new byte[1024];
int end;
while ((end = fis.read(bt)) != -1) {
os.write(bt, 0, end);
}
//关闭客服端的写入流 这不非常重要 如果没有关闭该流 则数据不会发送到服务端
sk.shutdownOutput();
String line;
while ((line = bisr.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
throw new RuntimeException("客户端异常退出");
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("客户端服务关闭异常");
}
}
if (sk != null) {
try {
sk.close();
} catch (IOException e) {
throw new RuntimeException("客户端服务关闭异常");
}
}
}
}
}).start();
// 服务端
new Thread(new Runnable(){
//创建一个服务套接字
ServerSocket ssk = null;
FileOutputStream fos = null;
public void run() {
try {
ssk = new ServerSocket(2008);
fos = new FileOutputStream("F:\\a.txt");
//获取服务端接受到的客户套接字
Socket sk = ssk.accept();
//获取客户套接字的读取流对象
InputStream br = sk.getInputStream();
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(sk.getOutputStream()));
byte[] bt = new byte[1024];
int endr;
while ((endr = br.read(bt)) != -1) {
fos.write(bt, 0, endr);
}
bw.write(sk.getInetAddress()+"成功上传");
bw.newLine();
bw.flush();
//关闭服务端接受的客户端套接字的写入流 这个步骤也非常重要 如果没有该步 那么服务端线程将处于线程等待机制
sk.shutdownOutput();
} catch (IOException e) {
throw new RuntimeException("服务端异常退出");
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("服务写入流关闭异常");
}
}
if (ssk != null) {
try {
ssk.close();
} catch (IOException e) {
throw new RuntimeException("服务关闭异常");
}
}
}
}
}).start();
}
}
复制代码
作者:
酱爆
时间:
2013-10-3 20:18
straw 发表于 2013-10-3 12:29
又一个敲代码不写注释的!苦啊 哥们 不过还好你这功能是常见的,要不就不好读了.
1,在客户端里当你写完数据 ...
哈哈,我会改正的!解决了,是ip出了问题,谢谢!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2