客户端- public class TCPClientPic {
- public static void main(String[] args) throws Exception{
- Socket socket = new Socket("127.0.0.1",20001);
- InputStream is = socket.getInputStream();
- OutputStream os = socket.getOutputStream();
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:\\star.jpg")));
- BufferedOutputStream bosout = new BufferedOutputStream(os);
- BufferedInputStream bisin = new BufferedInputStream(is);
- int len = 0;
- while((len = bis.read())!=-1){
- bosout.write(len);
- }
- socket.shutdownOutput();
- byte[] buf = new byte[30];
- len = bisin.read(buf);
- System.out.println(Arrays.toString(buf));
- System.out.println(new String(buf) );
- bis.close();
- bisin.close();
- bosout.close();
- socket.close();
- }
- }
复制代码 服务器端- public class TCPReceivePic {
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(20001);
- Socket socket = ss.accept();
- System.out.println(socket.getInetAddress().getHostAddress());
- InputStream is = socket.getInputStream();
- OutputStream os = socket.getOutputStream();
- BufferedInputStream bisin = new BufferedInputStream(is);
- BufferedOutputStream bosout = new BufferedOutputStream(os);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
- new File("d:\\copyPid.jpg")));
- int len = 0;
- while ((len = bisin.read()) != -1) {
- bos.write(len);
- }
- bosout.write("nihao".getBytes());
- bisin.close();
- bos.close();
- bisin.close();
- socket.close();
- ss.close();
- }
- }
复制代码 客户端结果 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
也就是说我 在服务器端返回的nihao 没有返回 怎么回事 求解!!!!!!
|
|